GitHub Action
push-to-ghcr
This action simplifies pushes of Docker images to the GitHub Containers Registry at ghcr.io.
Dockerfile
from your repository is build and published on:
release
event (releases namedvx.y.z
) your image will be tagged withx.y.z
push
event your image will be tagged withlatest
Images built for private repositories will be published as private containers to ghcr.io. Please refer to GitHub's documentation on how to set up access to them via personal access token (also known as PAT). PAT can be created in "Developer settings" panel.
Create a new GitHub Actions workflow as follows:
name: Build and publish a Docker image to ghcr.io
on:
# publish on releases, e.g. v2.1.13 (image tagged as "2.1.13" - "v" prefix is removed)
release:
types: [ published ]
# publish on pushes to the main branch (image tagged as "latest")
push:
branches:
- master
jobs:
docker_publish:
runs-on: "ubuntu-20.04"
steps:
- uses: actions/checkout@v2
# https://github.com/marketplace/actions/push-to-ghcr
- name: Build and publish a Docker image for ${{ github.repository }}
uses: macbre/push-to-ghcr@master
with:
image_name: ${{ github.repository }}
github_token: ${{ secrets.GITHUB_TOKEN }}
# optionally push to the Docker Hub (docker.io)
# docker_io_token: ${{ secrets.DOCKER_IO_ACCESS_TOKEN }} # see https://hub.docker.com/settings/security
This action assumes that your Dockerfile
is in the root directory of your repository.
The image that is pushed is labelled with org.label-schema
and org.opencontainers
schema. For instance:
{
"org.label-schema.build-date": "2021-07-01T14:28:46Z",
"org.label-schema.vcs-ref": "6f51d3d7bb7d46959a26594cb2b807573e34c546",
"org.label-schema.vcs-url": "https://github.com/macbre/push-to-ghcr.git",
"org.opencontainers.image.created": "2021-07-01T14:28:46Z",
"org.opencontainers.image.revision": "6f51d3d7bb7d46959a26594cb2b807573e34c546",
"org.opencontainers.image.source": "https://github.com/macbre/push-to-ghcr.git"
}
Additonally, BUILD_DATE
and GITHUB_SHA
build args are set resulting with these env variables being present in the container:
BUILD_DATE=2021-07-01T12:52:03Z
GITHUB_SHA=26b095f37cdf56a632aa2235345d4174b26e1d66
On 18th June 2021 Docker Hub discontinued Autobuilds on the free tier. However, you can use this action to additionally push to docker.io repository.
- You will need an access tokens created via https://hub.docker.com/settings/security.
- Store it in your GitHub repository secrets, e.g. as
DOCKER_IO_ACCESS_TOKEN
. - Provide additional option in
with
section in action invocation:
# (...)
- name: Build and publish a Docker image for ${{ github.repository }}
uses: macbre/push-to-ghcr@master
with:
image_name: ${{ github.repository }}
github_token: ${{ secrets.GITHUB_TOKEN }}
docker_io_token: ${{ secrets.DOCKER_IO_ACCESS_TOKEN }} # optionally push to the Docker Hub (docker.io)\
Your image will be pushed to both ghcr.io and docker.io repositories using the name provided as image_name
.