Skip to content

Instantly share code, notes, and snippets.

Last active Apr 2, 2022
What would you like to do?
Automatically build a publish a Docker container image to GitHub Container Registry on every push for free, like how Docker Hub used to work


Replace the awesome build-and-push functionality we lost with Docker Hub automatically building images from GitHub repos and publishing them.



Local configuration

It appears that the only way to initialize container images on a repo is by building and pushing one locally first. To do that, you need to authenticate your local Docker install with

  1. Create a Personal Access Token (PAT) in your GitHub settings -> Developer settings -> Personal Access Token
  2. Run docker login -u <username> and use the PAT as the password


For each repo

  1. Build the package locally and push it with docker build . --tag<username>/<repo> and docker push<username>/<repo>
  2. Go to<username>/<repo/ in the browser which will redirect you to the page for that package
  3. Click "Connect Repository" and connect the package to the repo
  4. Click "Package settings"
    1. Under "Actions Repository access" click "Add repository", add the repo
    2. Change the Actions Repository access permission to "Write"
    3. Under "Danger Zone", change the visibility to Public
  5. On the repo page, click "Actions" -> "set up a workflow yourself" -> copy and paste main.yml below into the new action and commit it
  6. On the repo page, click the gear next to "About" and make sure packages are shown
  7. Optionally include a build badge in the README like [![build status](](
name: Create and publish a Docker image
branches: ["main"]
IMAGE_NAME: ${{ github.repository }}
runs-on: ubuntu-latest
contents: read
packages: write
- name: Checkout repository
uses: actions/checkout@v3
- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
registry: ${{ env.REGISTRY }}
username: ${{ }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }}
- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment