Skip to content

Instantly share code, notes, and snippets.

@statico
Last active Apr 2, 2022
Embed
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

Goal

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

Example:

How

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 gchr.io.

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

See: https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry

For each repo

  1. Build the package locally and push it with docker build . --tag ghcr.io/<username>/<repo> and docker push ghcr.io/<username>/<repo>
  2. Go to https://ghcr.io/<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](https://img.shields.io/github/workflow/status/statico/xxx/Create%20and%20publish%20a%20Docker%20image.svg?style=flat-square)](https://ghcr.io/statico/xxx)
name: Create and publish a Docker image
on:
push:
branches: ["main"]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
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
with:
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