GitHub Action to build and push Docker images with Buildx
with full support of the features provided by Moby BuildKit
builder toolkit. This includes multi-platform build, secrets, remote cache, etc.
and different builder deployment/namespacing options.
In the examples below we are also using 3 other actions:
setup-buildx action will
create and boot a builder using by default the docker-container driver.
This is not required but recommended using it to be able to build
multi-platform images, export cache, etc.
setup-qemu action can be
useful if you want to add emulation support with QEMU to be able to build
against more platforms.
login action will take care to
log in against a Docker registry.
Git context
By default, this action uses the Git context,
so you don’t need to use the actions/checkout
action to check out the repository as this will be done directly by BuildKit.
The git reference will be based on the event that triggered your workflow
and will result in the following context: https://github.com/<owner>/<repo>.git#<ref>.
name: ci
on:
push:
jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Set up QEMU
uses: docker/setup-qemu-action@v3
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
-
name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
uses: docker/build-push-action@v6
with:
push: true
tags: user/app:latest
Be careful because any file mutation in the steps that precede the build step
will be ignored, including processing of the .dockerignore file since
the context is based on the Git reference. However, you can use the
Path context using the context input alongside
the actions/checkout action to remove
this restriction.
Default Git context can also be provided using the Handlebars template
expression {{defaultContext}}. Here we can use it to provide a subdirectory
to the default Git context:
Building from the current repository automatically uses the GitHub Token,
so it does not need to be passed. If you want to authenticate against another
private repository, you have to use a secret
named GIT_AUTH_TOKEN to be able to authenticate against it with Buildx:
This action generates a job summary
that provides a detailed overview of the build execution. The summary shows an
overview of all the steps executed during the build, including the build inputs
and eventual errors.
The summary also includes a link for downloading the build record with
additional details about the build, including build stats, logs, outputs, and
more. The build record can be imported to Docker Desktop for inspecting the
build in greater detail.
Summaries are enabled by default, but can be disabled with the
DOCKER_BUILD_SUMMARYenvironment variable.
For more information about summaries, refer to the
documentation.
Customizing
inputs
The following inputs can be used as step.with keys:
About
GitHub Action to build and push Docker images with Buildx with full support of the features provided by Moby BuildKit builder toolkit. This includes multi-platform build, secrets, remote cache, etc. and different builder deployment/namespacing options.
Usage
In the examples below we are also using 3 other actions:
setup-buildx
action will create and boot a builder using by default thedocker-container
driver. This is not required but recommended using it to be able to build multi-platform images, export cache, etc.setup-qemu
action can be useful if you want to add emulation support with QEMU to be able to build against more platforms.login
action will take care to log in against a Docker registry.Git context
By default, this action uses the Git context, so you don’t need to use the
actions/checkout
action to check out the repository as this will be done directly by BuildKit.The git reference will be based on the event that triggered your workflow and will result in the following context:
https://github.com/<owner>/<repo>.git#<ref>
.Be careful because any file mutation in the steps that precede the build step will be ignored, including processing of the
.dockerignore
file since the context is based on the Git reference. However, you can use the Path context using thecontext
input alongside theactions/checkout
action to remove this restriction.Default Git context can also be provided using the Handlebars template expression
{{defaultContext}}
. Here we can use it to provide a subdirectory to the default Git context:Building from the current repository automatically uses the GitHub Token, so it does not need to be passed. If you want to authenticate against another private repository, you have to use a secret named
GIT_AUTH_TOKEN
to be able to authenticate against it with Buildx:Path context
Examples
Summaries
This action generates a job summary that provides a detailed overview of the build execution. The summary shows an overview of all the steps executed during the build, including the build inputs and eventual errors.
The summary also includes a link for downloading the build record with additional details about the build, including build stats, logs, outputs, and more. The build record can be imported to Docker Desktop for inspecting the build in greater detail.
Summaries are enabled by default, but can be disabled with the
DOCKER_BUILD_SUMMARY
environment variable.For more information about summaries, refer to the documentation.
Customizing
inputs
The following inputs can be used as
step.with
keys:add-hosts
docker:10.180.0.1
)allow
network.host,security.insecure
)annotations
attests
type=sbom,generator=image
)builder
build-args
build-contexts
name=path
)cache-from
type=local,src=path/to/dir
)cache-to
type=local,dest=path/to/dir
)call
check
)cgroup-parent
context
PATH
orURL
(default Git context)file
{context}/Dockerfile
)labels
load
--output=type=docker
(defaultfalse
)network
RUN
instructions during buildno-cache
false
)no-cache-filters
outputs
type=local,dest=path
)platforms
provenance
--attest=type=provenance
)pull
false
)push
--output=type=registry
(defaultfalse
)sbom
--attest=type=sbom
)secrets
key=string
,GIT_AUTH_TOKEN=mytoken
)secret-envs
key=envname
,MY_SECRET=MY_ENV_VAR
)secret-files
key=filename
,MY_SECRET=./secret.txt
)shm-size
/dev/shm
(e.g.,2g
)ssh
tags
target
ulimit
nofile=1024:1024
)github-token
${{ github.token }}
)outputs
The following outputs are available:
imageid
digest
metadata
environment variables
DOCKER_BUILD_CHECKS_ANNOTATIONS
true
false
, GitHub annotations are not generated for build checksDOCKER_BUILD_SUMMARY
true
false
, build summary generation is disabledDOCKER_BUILD_RECORD_UPLOAD
true
false
, build record upload as GitHub artifact is disabledDOCKER_BUILD_RECORD_RETENTION_DAYS
0
Troubleshooting
See TROUBLESHOOTING.md
Contributing
Want to contribute? Awesome! You can find information about contributing to this project in the CONTRIBUTING.md