2020-08-19 21:46:12 +02: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 00:03:42 +02: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-19 20:17:13 +01: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-08 23:34:53 +02: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-19 21:46:12 +02:00
2020-08-16 00:36:41 +02:00
## About
2020-03-05 16:28:11 +00:00
2022-10-07 19:16:42 +02: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 15:13:04 +00:00
2020-08-19 21:46:12 +02:00
![Screenshot ](.github/build-push-action.png )
2020-08-16 00:36:41 +02:00
___
2020-03-05 16:28:11 +00:00
2020-08-16 00:36:41 +02:00
* [Usage ](#usage )
2020-09-02 10:07:11 +02:00
* [Git context ](#git-context )
* [Path context ](#path-context )
2022-11-04 09:17:25 +01:00
* [Examples ](#examples )
2023-02-23 17:16:49 +01: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/ )
2020-08-16 00:36:41 +02:00
* [Customizing ](#customizing )
* [inputs ](#inputs )
* [outputs ](#outputs )
2020-09-21 18:22:47 +02:00
* [Troubleshooting ](#troubleshooting )
2022-10-07 19:16:42 +02:00
* [Contributing ](#contributing )
2020-03-05 16:28:11 +00:00
2020-08-16 00:36:41 +02:00
## Usage
2020-03-05 16:28:11 +00:00
2022-01-30 18:12:55 +01:00
In the examples below we are also using 3 other actions:
2020-09-02 10:07:11 +02:00
2022-10-07 19:16:42 +02: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-17 18:53:15 +01:00
### Git context
2020-09-12 20:40:12 +02:00
2022-09-02 12:24:08 +02: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-30 18:12:55 +01:00
2022-10-07 19:16:42 +02: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-30 18:12:55 +01:00
and will result in the following context: `https://github.com/<owner>/<repo>.git#<ref>` .
2020-09-22 20:49:18 +02:00
```yaml
2020-08-16 00:36:41 +02:00
name: ci
on:
push:
2021-02-17 18:53:15 +01:00
branches:
2022-01-18 14:57:27 +01:00
- 'main'
2020-08-16 00:36:41 +02:00
jobs:
2021-02-17 18:53:15 +01:00
docker:
2020-08-17 22:18:15 +02:00
runs-on: ubuntu-latest
steps:
2020-09-05 05:18:00 +02:00
-
name: Set up QEMU
2023-09-12 10:12:12 +02:00
uses: docker/setup-qemu-action@v3
2020-08-17 22:18:15 +02:00
-
name: Set up Docker Buildx
2023-09-12 10:12:12 +02:00
uses: docker/setup-buildx-action@v3
2020-08-17 22:18:15 +02:00
-
2022-10-07 19:16:42 +02:00
name: Login to Docker Hub
2023-09-12 10:12:12 +02:00
uses: docker/login-action@v3
2020-08-17 22:18:15 +02:00
with:
2020-09-10 18:46:50 +02:00
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
2020-08-17 22:18:15 +02:00
-
name: Build and push
2023-09-12 10:12:12 +02:00
uses: docker/build-push-action@v5
2020-08-17 22:18:15 +02:00
with:
push: true
2020-08-18 22:54:44 +02:00
tags: user/app:latest
2020-09-22 20:49:18 +02:00
```
2020-08-17 22:18:15 +02:00
2022-01-30 18:12:55 +01: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 12:24:08 +02: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 10:12:12 +02:00
uses: docker/setup-buildx-action@v3
2022-01-30 18:12:55 +01:00
-
name: Build and push
2023-09-12 10:12:12 +02:00
uses: docker/build-push-action@v5
2022-01-30 18:12:55 +01:00
with:
context: "{{defaultContext}}:mysubdir"
push: true
tags: user/app:latest
```
2022-09-02 12:24:08 +02:00
> **Warning**
>
> Subdirectory for Git context is available from [BuildKit v0.9.0](https://github.com/moby/buildkit/releases/tag/v0.9.0).
> If you're using the `docker` builder (default if `setup-buildx-action` not used),
> then BuildKit in Docker Engine will be used. As Docker Engine < v22.x.x embeds
> Buildkit 0.8.2 at the moment, it does not support this feature. It's therefore
> required to use the `setup-buildx-action` at the moment.
2022-10-07 19:16:42 +02:00
Building from the current repository automatically uses the [GitHub Token ](https://docs.github.com/en/actions/security-guides/automatic-token-authentication ),
2022-01-30 18:12:55 +01:00
so it does not need to be passed. If you want to authenticate against another
2023-05-31 11:36:48 +02:00
private repository, you have to use a [secret ](https://docs.docker.com/build/ci/github-actions/secrets )
2022-11-04 09:17:25 +01:00
named `GIT_AUTH_TOKEN` to be able to authenticate against it with Buildx:
2020-09-02 10:07:11 +02:00
```yaml
-
name: Build and push
2023-09-12 10:12:12 +02:00
uses: docker/build-push-action@v5
2020-09-02 10:07:11 +02:00
with:
push: true
tags: user/app:latest
secrets: |
2020-09-22 20:49:18 +02:00
GIT_AUTH_TOKEN=${{ secrets.MYTOKEN }}
2020-09-02 10:07:11 +02:00
```
### Path context
2020-09-22 20:49:18 +02:00
```yaml
name: ci
2020-09-21 18:22:47 +02:00
2020-09-22 20:49:18 +02:00
on:
push:
2021-02-17 18:53:15 +01:00
branches:
2022-01-18 14:57:27 +01:00
- 'main'
2020-09-21 18:22:47 +02:00
2020-09-22 20:49:18 +02:00
jobs:
2021-02-17 18:53:15 +01:00
docker:
2020-09-22 20:49:18 +02:00
runs-on: ubuntu-latest
steps:
-
name: Checkout
2024-01-26 14:16:15 +09:00
uses: actions/checkout@v4
2020-09-22 20:49:18 +02:00
-
name: Set up QEMU
2023-09-12 10:12:12 +02:00
uses: docker/setup-qemu-action@v3
2020-09-22 20:49:18 +02:00
-
name: Set up Docker Buildx
2023-09-12 10:12:12 +02:00
uses: docker/setup-buildx-action@v3
2020-09-22 20:49:18 +02:00
-
2022-10-07 19:16:42 +02:00
name: Login to Docker Hub
2023-09-12 10:12:12 +02:00
uses: docker/login-action@v3
2020-09-22 20:49:18 +02:00
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
2023-09-12 10:12:12 +02:00
uses: docker/build-push-action@v5
2020-09-22 20:49:18 +02:00
with:
context: .
push: true
tags: user/app:latest
```
2020-09-02 10:07:11 +02:00
2022-11-04 09:17:25 +01:00
## Examples
2023-02-23 17:16:49 +01: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/ )
2020-09-10 01:14:41 +02:00
2020-08-16 00:36:41 +02:00
## Customizing
2020-03-05 16:28:11 +00:00
2020-08-16 00:36:41 +02:00
### inputs
2020-03-05 16:28:11 +00:00
2020-08-16 00:36:41 +02:00
Following inputs can be used as `step.with` keys
2020-03-05 16:28:11 +00:00
2020-10-22 21:50:22 +02: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 12:12:09 +01: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 07:23:44 +02:00
| `annotations` | List | List of annotation to set to the image |
2023-01-11 12:12:09 +01: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 |
| `outputs` ¹ | List | List of [output destinations ](https://docs.docker.com/engine/reference/commandline/buildx_build/#output ) (format: `type=local,dest=path` ) |
| `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 14:32:49 +02: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 12:12:09 +01: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 14:57:27 +01:00
2022-10-07 18:31:06 +02:00
> **Note**
>
> * ¹ multiple `outputs` are [not yet supported](https://github.com/moby/buildkit/issues/1555)
2020-08-16 00:36:41 +02:00
### outputs
2020-03-05 16:28:11 +00:00
2023-09-12 10:12:12 +02:00
The following outputs are available:
2020-03-05 16:28:11 +00:00
2022-10-07 19:16:42 +02:00
| Name | Type | Description |
|------------|---------|-----------------------|
| `imageid` | String | Image ID |
| `digest` | String | Image digest |
| `metadata` | JSON | Build result metadata |
2020-03-05 16:28:11 +00:00
2020-09-21 18:22:47 +02:00
## Troubleshooting
2020-09-21 21:06:04 +02:00
See [TROUBLESHOOTING.md ](TROUBLESHOOTING.md )
2020-09-21 18:22:47 +02:00
2022-10-07 19:16:42 +02:00
## Contributing
2020-08-20 17:25:55 +02:00
2022-10-07 19:16:42 +02:00
Want to contribute? Awesome! You can find information about contributing to
this project in the [CONTRIBUTING.md ](/.github/CONTRIBUTING.md )