This action simplifies pushes of container images to the GitHub Containers Registry at ghcr.io.
Dockerfile from your repository is build and published on:
releaseevent (releases namedvx.y.z) your image will be tagged withx.y.zpushevent your image will be tagged withlatest
Additionally, this action will also automatically lowercase the repository name to be able to push an image to the repository.
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 }} # it will be lowercased internally 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 # customize the username to be used when pushing to the Docker Hub # docker_io_user: foobar # see https://github.com/macbre/push-to-ghcr/issues/14This action assumes that your Dockerfile is in the root directory of your repository.
However, you can use dockerfile input to specify a different path (relative to the root directory of your repository). Additionaly, context input can also be provided. Docker docs should provide more context on context ;).
github_token(required): Yoursecrets.GITHUB_TOKENimage_name(required): Image name, e.g.my-user-name/my-repo(will be lowercased internally)dockerfile(defaults to./Dockerfile): A path to the Dockerfile (if it's not in the repository's root directory)context(defaults to.): A path to the context in which the build will happen, see https://docs.docker.com/engine/reference/commandline/build/repository(defaults toghcr.io): containers repository to push an image todocker_io_user: A username to use when pushing an image todocker.io(defaults to thegithub.actor)docker_io_token: Yourdocker.iotoken created via https://hub.docker.com/settings/securityimage_tag: Image tag, e.g.latest. Will overwrite the tag latest tag on a push, and have no effect on a releaseextra_args: additinal arguments to pass todocker build, you can for instance pass here additional tags to be used for an image, e.g. (extra_args: "--tag foo:latest --tag app:bar")
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 passed. They can be used to set env variables:
# these two are passed as build args ARG BUILD_DATE ARG GITHUB_SHA ENV BUILD_DATE=$BUILD_DATE ENV GITHUB_SHA=$GITHUB_SHAFor instance:
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
withsection 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.