2020-08-20 03:46:12 +08:00
[![GitHub release ](https://img.shields.io/github/release/docker/build-push-action.svg?style=flat-square )](https://github.com/docker/build-push-action/releases/latest)
2020-09-09 06:03:42 +08:00
[![GitHub marketplace ](https://img.shields.io/badge/marketplace-build--and--push--docker--images-blue?logo=github&style=flat-square )](https://github.com/marketplace/actions/build-and-push-docker-images)
2022-12-20 03:17:13 +08:00
[![CI workflow ](https://img.shields.io/github/actions/workflow/status/docker/build-push-action/ci.yml?branch=master&label=ci&logo=github&style=flat-square )](https://github.com/docker/build-push-action/actions?workflow=ci)
[![Test workflow ](https://img.shields.io/github/actions/workflow/status/docker/build-push-action/test.yml?branch=master&label=test&logo=github&style=flat-square )](https://github.com/docker/build-push-action/actions?workflow=test)
2020-09-09 05:34:53 +08:00
[![Codecov ](https://img.shields.io/codecov/c/github/docker/build-push-action?logo=codecov&style=flat-square )](https://codecov.io/gh/docker/build-push-action)
2020-08-20 03:46:12 +08:00
2020-08-16 06:36:41 +08:00
## About
2020-03-06 00:28:11 +08:00
2022-10-08 01:16:42 +08:00
GitHub Action to build and push Docker images with [Buildx ](https://github.com/docker/buildx )
with full support of the features provided by [Moby BuildKit ](https://github.com/moby/buildkit )
builder toolkit. This includes multi-platform build, secrets, remote cache, etc.
and different builder deployment/namespacing options.
2020-03-16 23:13:04 +08:00
2020-08-20 03:46:12 +08:00
![Screenshot ](.github/build-push-action.png )
2020-08-16 06:36:41 +08:00
___
2020-03-06 00:28:11 +08:00
2020-08-16 06:36:41 +08:00
* [Usage ](#usage )
2020-09-02 16:07:11 +08:00
* [Git context ](#git-context )
* [Path context ](#path-context )
2022-11-04 16:17:25 +08:00
* [Examples ](#examples )
2023-02-24 00:16:49 +08:00
* [Multi-platform image ](https://docs.docker.com/build/ci/github-actions/multi-platform/ )
* [Secrets ](https://docs.docker.com/build/ci/github-actions/secrets/ )
* [Push to multi-registries ](https://docs.docker.com/build/ci/github-actions/push-multi-registries/ )
* [Manage tags and labels ](https://docs.docker.com/build/ci/github-actions/manage-tags-labels/ )
* [Cache management ](https://docs.docker.com/build/ci/github-actions/cache/ )
* [Export to Docker ](https://docs.docker.com/build/ci/github-actions/export-docker/ )
* [Test before push ](https://docs.docker.com/build/ci/github-actions/test-before-push/ )
* [Local registry ](https://docs.docker.com/build/ci/github-actions/local-registry/ )
* [Share built image between jobs ](https://docs.docker.com/build/ci/github-actions/share-image-jobs/ )
* [Named contexts ](https://docs.docker.com/build/ci/github-actions/named-contexts/ )
* [Copy image between registries ](https://docs.docker.com/build/ci/github-actions/copy-image-registries/ )
* [Update Docker Hub repo description ](https://docs.docker.com/build/ci/github-actions/update-dockerhub-desc/ )
2024-04-26 17:12:45 +08:00
* [SBOM and provenance attestations ](https://docs.docker.com/build/ci/github-actions/attestations/ )
* [Annotations ](https://docs.docker.com/build/ci/github-actions/annotations/ )
* [Reproducible builds ](https://docs.docker.com/build/ci/github-actions/reproducible-builds/ )
2020-08-16 06:36:41 +08:00
* [Customizing ](#customizing )
* [inputs ](#inputs )
* [outputs ](#outputs )
2024-05-15 15:40:14 +08:00
* [environment variables ](#environment-variables )
2020-09-22 00:22:47 +08:00
* [Troubleshooting ](#troubleshooting )
2022-10-08 01:16:42 +08:00
* [Contributing ](#contributing )
2020-03-06 00:28:11 +08:00
2020-08-16 06:36:41 +08:00
## Usage
2020-03-06 00:28:11 +08:00
2022-01-31 01:12:55 +08:00
In the examples below we are also using 3 other actions:
2020-09-02 16:07:11 +08:00
2022-10-08 01:16:42 +08:00
* [`setup-buildx` ](https://github.com/docker/setup-buildx-action ) action will
create and boot a builder using by default the [`docker-container` driver ](https://docs.docker.com/build/building/drivers/docker-container/ ).
This is **not required but recommended** using it to be able to build
multi-platform images, export cache, etc.
* [`setup-qemu` ](https://github.com/docker/setup-qemu-action ) action can be
useful if you want to add emulation support with QEMU to be able to build
against more platforms.
* [`login` ](https://github.com/docker/login-action ) action will take care to
log in against a Docker registry.
2021-02-18 01:53:15 +08:00
### Git context
2020-09-13 02:40:12 +08:00
2022-09-02 18:24:08 +08:00
By default, this action uses the [Git context ](https://docs.docker.com/engine/reference/commandline/build/#git-repositories ),
so you don't need to use the [`actions/checkout` ](https://github.com/actions/checkout/ )
action to check out the repository as this will be done directly by [BuildKit ](https://github.com/moby/buildkit ).
2022-01-31 01:12:55 +08:00
2022-10-08 01:16:42 +08:00
The git reference will be based on the [event that triggered your workflow ](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows )
2022-01-31 01:12:55 +08:00
and will result in the following context: `https://github.com/<owner>/<repo>.git#<ref>` .
2020-09-23 02:49:18 +08:00
```yaml
2020-08-16 06:36:41 +08:00
name: ci
on:
push:
2021-02-18 01:53:15 +08:00
branches:
2022-01-18 21:57:27 +08:00
- 'main'
2020-08-16 06:36:41 +08:00
jobs:
2021-02-18 01:53:15 +08:00
docker:
2020-08-18 04:18:15 +08:00
runs-on: ubuntu-latest
steps:
2020-09-05 11:18:00 +08:00
-
name: Set up QEMU
2023-09-12 16:12:12 +08:00
uses: docker/setup-qemu-action@v3
2020-08-18 04:18:15 +08:00
-
name: Set up Docker Buildx
2023-09-12 16:12:12 +08:00
uses: docker/setup-buildx-action@v3
2020-08-18 04:18:15 +08:00
-
2022-10-08 01:16:42 +08:00
name: Login to Docker Hub
2023-09-12 16:12:12 +08:00
uses: docker/login-action@v3
2020-08-18 04:18:15 +08:00
with:
2020-09-11 00:46:50 +08:00
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
2020-08-18 04:18:15 +08:00
-
name: Build and push
2024-06-17 17:46:56 +08:00
uses: docker/build-push-action@v6
2020-08-18 04:18:15 +08:00
with:
push: true
2020-08-19 04:54:44 +08:00
tags: user/app:latest
2020-09-23 02:49:18 +08:00
```
2020-08-18 04:18:15 +08:00
2022-01-31 01:12:55 +08:00
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 ](#path-context ) using the [`context` input ](#inputs ) alongside
the [`actions/checkout` ](https://github.com/actions/checkout/ ) action to remove
this restriction.
Default Git context can also be provided using the [Handlebars template ](https://handlebarsjs.com/guide/ )
expression `{{defaultContext}}` . Here we can use it to provide a subdirectory
to the default Git context:
```yaml
2022-09-02 18:24:08 +08:00
-
# Setting up Docker Buildx with docker-container driver is required
# at the moment to be able to use a subdirectory with Git context
name: Set up Docker Buildx
2023-09-12 16:12:12 +08:00
uses: docker/setup-buildx-action@v3
2022-01-31 01:12:55 +08:00
-
name: Build and push
2024-06-17 17:46:56 +08:00
uses: docker/build-push-action@v6
2022-01-31 01:12:55 +08:00
with:
context: "{{defaultContext}}:mysubdir"
push: true
tags: user/app:latest
```
2022-10-08 01:16:42 +08:00
Building from the current repository automatically uses the [GitHub Token ](https://docs.github.com/en/actions/security-guides/automatic-token-authentication ),
2022-01-31 01:12:55 +08:00
so it does not need to be passed. If you want to authenticate against another
2023-05-31 17:36:48 +08:00
private repository, you have to use a [secret ](https://docs.docker.com/build/ci/github-actions/secrets )
2022-11-04 16:17:25 +08:00
named `GIT_AUTH_TOKEN` to be able to authenticate against it with Buildx:
2020-09-02 16:07:11 +08:00
```yaml
-
name: Build and push
2024-06-17 17:46:56 +08:00
uses: docker/build-push-action@v6
2020-09-02 16:07:11 +08:00
with:
push: true
tags: user/app:latest
secrets: |
2020-09-23 02:49:18 +08:00
GIT_AUTH_TOKEN=${{ secrets.MYTOKEN }}
2020-09-02 16:07:11 +08:00
```
### Path context
2020-09-23 02:49:18 +08:00
```yaml
name: ci
2020-09-22 00:22:47 +08:00
2020-09-23 02:49:18 +08:00
on:
push:
2021-02-18 01:53:15 +08:00
branches:
2022-01-18 21:57:27 +08:00
- 'main'
2020-09-22 00:22:47 +08:00
2020-09-23 02:49:18 +08:00
jobs:
2021-02-18 01:53:15 +08:00
docker:
2020-09-23 02:49:18 +08:00
runs-on: ubuntu-latest
steps:
-
name: Checkout
2024-01-26 13:16:15 +08:00
uses: actions/checkout@v4
2020-09-23 02:49:18 +08:00
-
name: Set up QEMU
2023-09-12 16:12:12 +08:00
uses: docker/setup-qemu-action@v3
2020-09-23 02:49:18 +08:00
-
name: Set up Docker Buildx
2023-09-12 16:12:12 +08:00
uses: docker/setup-buildx-action@v3
2020-09-23 02:49:18 +08:00
-
2022-10-08 01:16:42 +08:00
name: Login to Docker Hub
2023-09-12 16:12:12 +08:00
uses: docker/login-action@v3
2020-09-23 02:49:18 +08:00
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
2024-06-17 17:46:56 +08:00
uses: docker/build-push-action@v6
2020-09-23 02:49:18 +08:00
with:
context: .
push: true
tags: user/app:latest
```
2020-09-02 16:07:11 +08:00
2022-11-04 16:17:25 +08:00
## Examples
2023-02-24 00:16:49 +08:00
* [Multi-platform image ](https://docs.docker.com/build/ci/github-actions/multi-platform/ )
* [Secrets ](https://docs.docker.com/build/ci/github-actions/secrets/ )
* [Push to multi-registries ](https://docs.docker.com/build/ci/github-actions/push-multi-registries/ )
* [Manage tags and labels ](https://docs.docker.com/build/ci/github-actions/manage-tags-labels/ )
* [Cache management ](https://docs.docker.com/build/ci/github-actions/cache/ )
* [Export to Docker ](https://docs.docker.com/build/ci/github-actions/export-docker/ )
* [Test before push ](https://docs.docker.com/build/ci/github-actions/test-before-push/ )
* [Local registry ](https://docs.docker.com/build/ci/github-actions/local-registry/ )
* [Share built image between jobs ](https://docs.docker.com/build/ci/github-actions/share-image-jobs/ )
* [Named contexts ](https://docs.docker.com/build/ci/github-actions/named-contexts/ )
* [Copy image between registries ](https://docs.docker.com/build/ci/github-actions/copy-image-registries/ )
* [Update Docker Hub repo description ](https://docs.docker.com/build/ci/github-actions/update-dockerhub-desc/ )
2024-04-26 17:12:45 +08:00
* [SBOM and provenance attestations ](https://docs.docker.com/build/ci/github-actions/attestations/ )
* [Annotations ](https://docs.docker.com/build/ci/github-actions/annotations/ )
* [Reproducible builds ](https://docs.docker.com/build/ci/github-actions/reproducible-builds/ )
2020-09-10 07:14:41 +08:00
2020-08-16 06:36:41 +08:00
## Customizing
2020-03-06 00:28:11 +08:00
2020-08-16 06:36:41 +08:00
### inputs
2020-03-06 00:28:11 +08:00
2020-08-16 06:36:41 +08:00
Following inputs can be used as `step.with` keys
2020-03-06 00:28:11 +08:00
2020-10-23 03:50:22 +08:00
> `List` type is a newline-delimited string
> ```yaml
> cache-from: |
> user/app:cache
> type=local,src=path/to/dir
> ```
> `CSV` type is a comma-delimited string
> ```yaml
> tags: name/app:latest,name/app:1.0.0
> ```
2023-01-11 19:12:09 +08:00
| Name | Type | Description |
|--------------------|-------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `add-hosts` | List/CSV | List of [customs host-to-IP mapping ](https://docs.docker.com/engine/reference/commandline/build/#add-entries-to-container-hosts-file---add-host ) (e.g., `docker:10.180.0.1` ) |
| `allow` | List/CSV | List of [extra privileged entitlement ](https://docs.docker.com/engine/reference/commandline/buildx_build/#allow ) (e.g., `network.host,security.insecure` ) |
2023-10-24 13:23:44 +08:00
| `annotations` | List | List of annotation to set to the image |
2023-01-11 19:12:09 +08:00
| `attests` | List | List of [attestation ](https://docs.docker.com/build/attestations/ ) parameters (e.g., `type=sbom,generator=image` ) |
| `builder` | String | Builder instance (see [setup-buildx ](https://github.com/docker/setup-buildx-action ) action) |
| `build-args` | List | List of [build-time variables ](https://docs.docker.com/engine/reference/commandline/buildx_build/#build-arg ) |
| `build-contexts` | List | List of additional [build contexts ](https://docs.docker.com/engine/reference/commandline/buildx_build/#build-context ) (e.g., `name=path` ) |
| `cache-from` | List | List of [external cache sources ](https://docs.docker.com/engine/reference/commandline/buildx_build/#cache-from ) (e.g., `type=local,src=path/to/dir` ) |
| `cache-to` | List | List of [cache export destinations ](https://docs.docker.com/engine/reference/commandline/buildx_build/#cache-to ) (e.g., `type=local,dest=path/to/dir` ) |
| `cgroup-parent` | String | Optional [parent cgroup ](https://docs.docker.com/engine/reference/commandline/build/#use-a-custom-parent-cgroup---cgroup-parent ) for the container used in the build |
| `context` | String | Build's context is the set of files located in the specified [`PATH` or `URL` ](https://docs.docker.com/engine/reference/commandline/build/ ) (default [Git context ](#git-context )) |
| `file` | String | Path to the Dockerfile. (default `{context}/Dockerfile` ) |
| `labels` | List | List of metadata for an image |
| `load` | Bool | [Load ](https://docs.docker.com/engine/reference/commandline/buildx_build/#load ) is a shorthand for `--output=type=docker` (default `false` ) |
| `network` | String | Set the networking mode for the `RUN` instructions during build |
| `no-cache` | Bool | Do not use cache when building the image (default `false` ) |
| `no-cache-filters` | List/CSV | Do not cache specified stages |
2024-04-15 15:50:33 +08:00
| `outputs` | List | List of [output destinations ](https://docs.docker.com/engine/reference/commandline/buildx_build/#output ) (format: `type=local,dest=path` ) |
2023-01-11 19:12:09 +08:00
| `platforms` | List/CSV | List of [target platforms ](https://docs.docker.com/engine/reference/commandline/buildx_build/#platform ) for build |
| `provenance` | Bool/String | Generate [provenance ](https://docs.docker.com/build/attestations/slsa-provenance/ ) attestation for the build (shorthand for `--attest=type=provenance` ) |
| `pull` | Bool | Always attempt to pull all referenced images (default `false` ) |
| `push` | Bool | [Push ](https://docs.docker.com/engine/reference/commandline/buildx_build/#push ) is a shorthand for `--output=type=registry` (default `false` ) |
| `sbom` | Bool/String | Generate [SBOM ](https://docs.docker.com/build/attestations/sbom/ ) attestation for the build (shorthand for `--attest=type=sbom` ) |
| `secrets` | List | List of [secrets ](https://docs.docker.com/engine/reference/commandline/buildx_build/#secret ) to expose to the build (e.g., `key=string` , `GIT_AUTH_TOKEN=mytoken` ) |
2023-10-12 20:32:49 +08:00
| `secret-envs` | List/CSV | List of [secret env vars ](https://docs.docker.com/engine/reference/commandline/buildx_build/#secret ) to expose to the build (e.g., `key=envname` , `MY_SECRET=MY_ENV_VAR` ) |
2023-01-11 19:12:09 +08:00
| `secret-files` | List | List of [secret files ](https://docs.docker.com/engine/reference/commandline/buildx_build/#secret ) to expose to the build (e.g., `key=filename` , `MY_SECRET=./secret.txt` ) |
| `shm-size` | String | Size of [`/dev/shm` ](https://docs.docker.com/engine/reference/commandline/buildx_build/#shm-size ) (e.g., `2g` ) |
| `ssh` | List | List of [SSH agent socket or keys ](https://docs.docker.com/engine/reference/commandline/buildx_build/#ssh ) to expose to the build |
| `tags` | List/CSV | List of tags |
| `target` | String | Sets the target stage to build |
| `ulimit` | List | [Ulimit ](https://docs.docker.com/engine/reference/commandline/buildx_build/#ulimit ) options (e.g., `nofile=1024:1024` ) |
| `github-token` | String | GitHub Token used to authenticate against a repository for [Git context ](#git-context ) (default `${{ github.token }}` ) |
2022-01-18 21:57:27 +08:00
2020-08-16 06:36:41 +08:00
### outputs
2020-03-06 00:28:11 +08:00
2023-09-12 16:12:12 +08:00
The following outputs are available:
2020-03-06 00:28:11 +08:00
2022-10-08 01:16:42 +08:00
| Name | Type | Description |
|------------|---------|-----------------------|
| `imageid` | String | Image ID |
| `digest` | String | Image digest |
| `metadata` | JSON | Build result metadata |
2020-03-06 00:28:11 +08:00
2024-05-15 15:40:14 +08:00
### environment variables
2024-06-24 16:14:14 +08:00
| Name | Type | Description |
|--------------------------------------|--------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `DOCKER_BUILD_NO_SUMMARY` | Bool | If `true` , [build summary ](https://docs.docker.com/build/ci/github-actions/build-summary/ ) generation is disabled |
| `DOCKER_BUILD_EXPORT_RETENTION_DAYS` | Number | Duration after which build export artifact will expire in days. Defaults to repository/org [retention settings ](https://docs.github.com/en/actions/learn-github-actions/usage-limits-billing-and-administration#artifact-and-log-retention-policy ) if unset or `0` |
2024-05-15 15:40:14 +08:00
2020-09-22 00:22:47 +08:00
## Troubleshooting
2020-09-22 03:06:04 +08:00
See [TROUBLESHOOTING.md ](TROUBLESHOOTING.md )
2020-09-22 00:22:47 +08:00
2022-10-08 01:16:42 +08:00
## Contributing
2020-08-20 23:25:55 +08:00
2022-10-08 01:16:42 +08:00
Want to contribute? Awesome! You can find information about contributing to
this project in the [CONTRIBUTING.md ](/.github/CONTRIBUTING.md )