2020-08-19 15:46:12 -04: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-08 18:03:42 -04: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)
2020-08-19 15:46:12 -04:00
[![CI workflow ](https://img.shields.io/github/workflow/status/docker/build-push-action/ci?label=ci&logo=github&style=flat-square )](https://github.com/docker/build-push-action/actions?workflow=ci)
[![Test workflow ](https://img.shields.io/github/workflow/status/docker/build-push-action/test?label=test&logo=github&style=flat-square )](https://github.com/docker/build-push-action/actions?workflow=test)
2020-09-08 17:34:53 -04: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 15:46:12 -04:00
2020-08-15 18:36:41 -04:00
## About
2020-03-05 11:28:11 -05:00
2022-10-07 13:16:42 -04: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 11:13:04 -04:00
2020-08-19 15:46:12 -04:00
![Screenshot ](.github/build-push-action.png )
2020-08-15 18:36:41 -04:00
___
2020-03-05 11:28:11 -05:00
2020-08-15 18:36:41 -04:00
* [Usage ](#usage )
2020-09-02 04:07:11 -04:00
* [Git context ](#git-context )
* [Path context ](#path-context )
2020-09-12 14:40:12 -04:00
* [Advanced usage ](#advanced-usage )
2021-02-17 12:53:15 -05:00
* [Multi-platform image ](docs/advanced/multi-platform.md )
* [Secrets ](docs/advanced/secrets.md )
* [Isolated builders ](docs/advanced/isolated-builders.md )
* [Push to multi-registries ](docs/advanced/push-multi-registries.md )
2022-10-07 12:20:56 -04:00
* [Copy between registries ](docs/advanced/copy-between-registries.md )
2021-02-17 12:53:15 -05:00
* [Cache ](docs/advanced/cache.md )
* [Local registry ](docs/advanced/local-registry.md )
* [Export image to Docker ](docs/advanced/export-docker.md )
2021-05-21 11:05:16 -04:00
* [Share built image between jobs ](docs/advanced/share-image-jobs.md )
2021-09-01 10:47:54 -04:00
* [Test your image before pushing it ](docs/advanced/test-before-push.md )
2022-10-07 12:20:56 -04:00
* [Named contexts ](docs/advanced/named-contexts.md )
2021-02-17 12:53:15 -05:00
* [Handle tags and labels ](docs/advanced/tags-labels.md )
2022-10-07 13:16:42 -04:00
* [Update Docker Hub repo description ](docs/advanced/dockerhub-desc.md )
2020-08-15 18:36:41 -04:00
* [Customizing ](#customizing )
* [inputs ](#inputs )
* [outputs ](#outputs )
2020-09-21 12:22:47 -04:00
* [Troubleshooting ](#troubleshooting )
2022-10-07 13:16:42 -04:00
* [Contributing ](#contributing )
2020-03-05 11:28:11 -05:00
2020-08-15 18:36:41 -04:00
## Usage
2020-03-05 11:28:11 -05:00
2022-01-30 12:12:55 -05:00
In the examples below we are also using 3 other actions:
2020-09-02 04:07:11 -04:00
2022-10-07 13:16:42 -04: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 12:53:15 -05:00
### Git context
2020-09-12 14:40:12 -04:00
2022-09-02 06:24:08 -04: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 12:12:55 -05:00
2022-10-07 13:16:42 -04: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 12:12:55 -05:00
and will result in the following context: `https://github.com/<owner>/<repo>.git#<ref>` .
2020-09-22 14:49:18 -04:00
```yaml
2020-08-15 18:36:41 -04:00
name: ci
on:
push:
2021-02-17 12:53:15 -05:00
branches:
2022-01-18 08:57:27 -05:00
- 'main'
2020-08-15 18:36:41 -04:00
jobs:
2021-02-17 12:53:15 -05:00
docker:
2020-08-17 16:18:15 -04:00
runs-on: ubuntu-latest
steps:
2020-09-04 23:18:00 -04:00
-
name: Set up QEMU
2022-05-05 13:24:32 -04:00
uses: docker/setup-qemu-action@v2
2020-08-17 16:18:15 -04:00
-
name: Set up Docker Buildx
2022-05-05 13:24:32 -04:00
uses: docker/setup-buildx-action@v2
2020-08-17 16:18:15 -04:00
-
2022-10-07 13:16:42 -04:00
name: Login to Docker Hub
2022-05-05 13:24:32 -04:00
uses: docker/login-action@v2
2020-08-17 16:18:15 -04:00
with:
2020-09-10 12:46:50 -04:00
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
2020-08-17 16:18:15 -04:00
-
name: Build and push
2022-05-05 13:24:32 -04:00
uses: docker/build-push-action@v3
2020-08-17 16:18:15 -04:00
with:
push: true
2020-08-18 16:54:44 -04:00
tags: user/app:latest
2020-09-22 14:49:18 -04:00
```
2020-08-17 16:18:15 -04:00
2022-01-30 12:12:55 -05: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 06:24:08 -04: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
uses: docker/setup-buildx-action@v2
2022-01-30 12:12:55 -05:00
-
name: Build and push
2022-05-05 13:24:32 -04:00
uses: docker/build-push-action@v3
2022-01-30 12:12:55 -05:00
with:
context: "{{defaultContext}}:mysubdir"
push: true
tags: user/app:latest
```
2022-09-02 06:24:08 -04: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 13:16:42 -04: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 12:12:55 -05:00
so it does not need to be passed. If you want to authenticate against another
private repository, you have to use a [secret ](docs/advanced/secrets.md ) named
2022-10-07 13:16:42 -04:00
`GIT_AUTH_TOKEN` to be able to authenticate against it with Buildx:
2020-09-02 04:07:11 -04:00
```yaml
-
name: Build and push
2022-05-05 13:24:32 -04:00
uses: docker/build-push-action@v3
2020-09-02 04:07:11 -04:00
with:
push: true
tags: user/app:latest
secrets: |
2020-09-22 14:49:18 -04:00
GIT_AUTH_TOKEN=${{ secrets.MYTOKEN }}
2020-09-02 04:07:11 -04:00
```
### Path context
2020-09-22 14:49:18 -04:00
```yaml
name: ci
2020-09-21 12:22:47 -04:00
2020-09-22 14:49:18 -04:00
on:
push:
2021-02-17 12:53:15 -05:00
branches:
2022-01-18 08:57:27 -05:00
- 'main'
2020-09-21 12:22:47 -04:00
2020-09-22 14:49:18 -04:00
jobs:
2021-02-17 12:53:15 -05:00
docker:
2020-09-22 14:49:18 -04:00
runs-on: ubuntu-latest
steps:
-
name: Checkout
2022-05-28 12:36:30 -04:00
uses: actions/checkout@v3
2020-09-22 14:49:18 -04:00
-
name: Set up QEMU
2022-05-05 13:24:32 -04:00
uses: docker/setup-qemu-action@v2
2020-09-22 14:49:18 -04:00
-
name: Set up Docker Buildx
2022-05-05 13:24:32 -04:00
uses: docker/setup-buildx-action@v2
2020-09-22 14:49:18 -04:00
-
2022-10-07 13:16:42 -04:00
name: Login to Docker Hub
2022-05-05 13:24:32 -04:00
uses: docker/login-action@v2
2020-09-22 14:49:18 -04:00
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
2022-05-05 13:24:32 -04:00
uses: docker/build-push-action@v3
2020-09-22 14:49:18 -04:00
with:
context: .
push: true
tags: user/app:latest
```
2020-09-02 04:07:11 -04:00
2020-09-12 14:40:12 -04:00
## Advanced usage
2021-02-17 12:53:15 -05:00
* [Multi-platform image ](docs/advanced/multi-platform.md )
* [Secrets ](docs/advanced/secrets.md )
* [Isolated builders ](docs/advanced/isolated-builders.md )
* [Push to multi-registries ](docs/advanced/push-multi-registries.md )
2021-05-12 06:23:06 -04:00
* [Copy between registries ](docs/advanced/copy-between-registries.md )
2021-02-17 12:53:15 -05:00
* [Cache ](docs/advanced/cache.md )
* [Local registry ](docs/advanced/local-registry.md )
* [Export image to Docker ](docs/advanced/export-docker.md )
2021-05-21 11:05:16 -04:00
* [Share built image between jobs ](docs/advanced/share-image-jobs.md )
2021-09-01 10:47:54 -04:00
* [Test your image before pushing it ](docs/advanced/test-before-push.md )
2022-10-07 12:20:56 -04:00
* [Named contexts ](docs/advanced/named-contexts.md )
2021-02-17 12:53:15 -05:00
* [Handle tags and labels ](docs/advanced/tags-labels.md )
2022-10-07 13:16:42 -04:00
* [Update Docker Hub repo description ](docs/advanced/dockerhub-desc.md )
2020-09-09 19:14:41 -04:00
2020-08-15 18:36:41 -04:00
## Customizing
2020-03-05 11:28:11 -05:00
2020-08-15 18:36:41 -04:00
### inputs
2020-03-05 11:28:11 -05:00
2020-08-15 18:36:41 -04:00
Following inputs can be used as `step.with` keys
2020-03-05 11:28:11 -05:00
2020-10-22 15:50:22 -04: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
> ```
2022-10-07 13:16:42 -04: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` ) |
| `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 |
| `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` ) |
| `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` ) |
| `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 08:57:27 -05:00
2022-10-07 12:31:06 -04:00
> **Note**
>
> * ¹ multiple `outputs` are [not yet supported](https://github.com/moby/buildkit/issues/1555)
2020-08-15 18:36:41 -04:00
### outputs
2020-03-05 11:28:11 -05:00
2020-08-15 18:36:41 -04:00
Following outputs are available
2020-03-05 11:28:11 -05:00
2022-10-07 13:16:42 -04:00
| Name | Type | Description |
|------------|---------|-----------------------|
| `imageid` | String | Image ID |
| `digest` | String | Image digest |
| `metadata` | JSON | Build result metadata |
2020-03-05 11:28:11 -05:00
2020-09-21 12:22:47 -04:00
## Troubleshooting
2020-09-21 15:06:04 -04:00
See [TROUBLESHOOTING.md ](TROUBLESHOOTING.md )
2020-09-21 12:22:47 -04:00
2022-10-07 13:16:42 -04:00
## Contributing
2020-08-20 11:25:55 -04:00
2022-10-07 13:16:42 -04:00
Want to contribute? Awesome! You can find information about contributing to
this project in the [CONTRIBUTING.md ](/.github/CONTRIBUTING.md )