Introduction
This guide shows you how to create a workflow that performs a Docker build, and then publishes Docker images to Docker Hub or GitHub Packages. With a single workflow, you can publish images to a single registry or to multiple registries.
Note: If you want to push to another third-party Docker registry, the example in the "Publishing images to GitHub Packages" section can serve as a good template.
Prerequisites
We recommend that you have a basic understanding of workflow configuration options and how to create a workflow file. For more information, see "Learn GitHub Actions."
You might also find it helpful to have a basic understanding of the following:
- "Encrypted secrets"
- "Authentication in a workflow"
- "Configuring Docker for use with GitHub Packages"
About image configuration
This guide assumes that you have a complete definition for a Docker image stored in a GitHub repository. For example, your repository must contain a Dockerfile, and any other files needed to perform a Docker build to create an image.
In this guide, we will use the Docker build-push-action
action to build the Docker image and push it to one or more Docker registries. For more information, see build-push-action
.
Publishing images to Docker Hub
Each time you create a new release on GitHub, you can trigger a workflow to publish your image. The workflow in the example below runs when the release
event triggers with the created
activity type. For more information on the release
event, see "Events that trigger workflows.
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.
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
andpassword
: 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."repository
: Your Docker Hub repository in the formatDOCKER-HUB-NAMESPACE/DOCKER-HUB-REPOSITORY
.
name: Publish Docker image
on:
release:
types: [published]
jobs:
push_to_registry:
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v2
- name: Push to Docker Hub
uses: docker/build-push-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
repository: my-docker-hub-namespace/my-docker-hub-repository
tag_with_ref: true
The above workflow checks out the GitHub repository, and uses the build-push-action
action to build and push the Docker image. It sets the build-push-action
option tag_with_ref
to automatically tag the built Docker image with the Git reference of the workflow event. This workflow is triggered on publishing a GitHub release, so the reference will be the Git tag for the release.
Publishing images to GitHub Packages
Each time you create a new release on GitHub, you can trigger a workflow to publish your image. The workflow in the example below runs when the release
event triggers with the created
activity type. For more information on the release
event, see "Events that trigger workflows.
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 GitHub Packages.
The build-push-action
options required for GitHub Packages are:
username
: You can use the${{ github.actor }}
context to automatically use the username of the user that triggered the workflow run. For more information, see "Context and expression syntax for GitHub Actions."password
: You can use the automatically-generatedGITHUB_TOKEN
secret for the password. For more information, see "Authenticating with the GITHUB_TOKEN."registry
: Must be set todocker.pkg.github.com
.repository
: Must be set in the formatOWNER/REPOSITORY/IMAGE_NAME
. For example, for an image namedocto-image
stored on GitHub athttp://github.com/octo-org/octo-repo
, therepository
option should be set toocto-org/octo-repo/octo-image
.
name: Publish Docker image
on:
release:
types: [published]
jobs:
push_to_registry:
name: Push Docker image to GitHub Packages
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v2
- name: Push to GitHub Packages
uses: docker/build-push-action@v1
with:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
registry: docker.pkg.github.com
repository: my-org/my-repo/my-image
tag_with_ref: true
The above workflow checks out the GitHub repository, and uses the build-push-action
action to build and push the Docker image. It sets the build-push-action
option tag_with_ref
to automatically tag the built Docker image with the Git reference of the workflow event. This workflow is triggered on publishing a GitHub release, so the reference will be the Git tag for the release.
Publishing images to Docker Hub and GitHub Packages
In a single workflow, you can publish your Docker image to multiple registries by using the build-push-action
action for each registry.
The following example workflow uses the build-push-action
steps from the previous sections ("Publishing images to Docker Hub" and "Publishing images to GitHub Packages") to create a single workflow that pushes to both registries.
name: Publish Docker image
on:
release:
types: [published]
jobs:
push_to_registries:
name: Push Docker image to multiple registries
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v2
- name: Push to Docker Hub
uses: docker/build-push-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
repository: my-docker-hub-namespace/my-docker-hub-repository
tag_with_ref: true
- name: Push to GitHub Packages
uses: docker/build-push-action@v1
with:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
registry: docker.pkg.github.com
repository: my-org/my-repo/my-image
tag_with_ref: true
The above workflow checks out the GitHub repository, and uses the build-push-action
action twice to build and push the Docker image to Docker Hub and GitHub Packages. For both steps, it sets the build-push-action
option tag_with_ref
to automatically tag the built Docker image with the Git reference of the workflow event. This workflow is triggered on publishing a GitHub release, so the reference for both registries will be the Git tag for the release.