Use GitHub action to publishing Docker image to Docker Hub repository
GitHub action
Each time you push the code or create a new release on GitHub, you can trigger a workflow to publish your image. The workflow in the example below runs when the push event triggers.
In the example workflow below, we use the Docker build-push-action action to build the Docker image and, if the build succeeds, push the built image to Docker Hub.
Prerequisites
-
Github Account
GitHub is where over 56 million developers shape the future of software, together. Contribute to the open source community, manage your Git repositories, review code like a pro, track bugs and features, power your CI/CD and DevOps workflows, and secure code before you commit it.
-
Docker Hub Account
To push to Docker Hub, you will need to have a Docker Hub account, and have a Docker Hub repository created. For more information, see “Pushing a Docker container image to Docker Hub” in the Docker documentation.
The build-push-action options required for Docker Hub are:
username and password: This is your Docker Hub username and password. We recommend storing your Docker Hub username and password as secrets so they aren’t exposed in your workflow file. For more information, see “Creating and using encrypted secrets.” Encrypted secrets - GitHub Docs - https://docs.github.com/en/actions/reference/encrypted-secrets.
repository: Your Docker Hub repository in the format DOCKER-HUB-NAMESPACE/DOCKER-HUB-REPOSITORY.
Configuration
build-push-action options
Create the .github/workflows/publish.yml
file.
Remember to replace cloudolife/col-github-action-example
with your prefer Docker Hub repository.
1 | # .github/workflows/publish.yml |
GitHub action will run automatically after your push the code to GitHub.
References
[3] Learn GitHub Actions - GitHub Docs - https://docs.github.com/en/actions/learn-github-actions
[4] Encrypted secrets - GitHub Docs - https://docs.github.com/en/actions/reference/encrypted-secrets