This GitHub Action automatically builds Docker images and pushes them to the GitHub Container Registry (GHCR) whenever a Dockerfile is committed to a repository and a branch or tag is pushed.
To use this action in your workflow, add the following step to your .github/workflows/main.yml
file:
name: 'Test to publish Docker image'
on:
push:
branches:
- main
tags:
- '*'
jobs:
test:
runs-on: ubuntu-22.04
steps:
- name: 'Test'
uses: kengo-k/actions-docker-build@v1
with:
token: ${{ secrets.GHCR_TOKEN }}
timezone: 'Asia/Tokyo'
args: |
- path: <path-to-your-Dockerfile>
name: <your-image-name>
on_branch_pushed: true
on_branch_changed: true
on_tag_pushed: true
include_branch_name: true
include_timestamp: true
include_commit_sha: true
token
(required): A token with permissions to access the GitHub repository information and push images to GHCR.timezone
(optional): The timezone used for generating timestamps in the image tags. Default is UTC.args
(required): An array of objects specifying the configuration values for building Dockerfiles. Each object should contain the following properties:path
(required): The path to the Dockerfile.name
(required): The name of the built image.on_branch_pushed
(optional): Whether to build the image when a branch is pushed. Default istrue
.on_branch_changed
(optional): Ifon_branch_pushed
istrue
, only build the image if the Dockerfile is included in the commit. Default istrue
.on_tag_pushed
(optional): Whether to build the image when a tag is pushed. Default istrue
.include_branch_name
(optional): Whether to include the branch name in the image tag when a branch is pushed. Default istrue
.include_timestamp
(optional): Whether to include a timestamp in the image tag when a branch is pushed. Default istrue
.include_commit_sha
(optional): Whether to include the commit ID in the image tag when a branch is pushed. Default istrue
.
When a branch or tag is pushed to the repository, this action will:
- Check if a Dockerfile is present in the repository based on the provided
path
in theargs
configuration. - If
on_branch_pushed
istrue
and the push event is for a branch, build the Docker image. - If
on_branch_changed
istrue
, only build the image if the Dockerfile is included in the commit. - If
on_tag_pushed
istrue
and the push event is for a tag, build the Docker image. - Generate an image tag based on the specified options (
include_branch_name
,include_timestamp
,include_commit_sha
). - Build the Docker image using the provided Dockerfile and image name.
- Push the built image to the GitHub Container Registry (GHCR) using the generated tag.
This action simplifies the process of building and pushing Docker images to GHCR, making it easier to automate your container deployment workflow.