[GitHub Action] Use GitHub Action build push action v2 to build and push docker image

GitHub Action build-push-action V2

build-push-action is a 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.

Basic usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
name: ci

on:
push:
branches:
- 'main'

jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/[email protected]
-
name: Set up QEMU
uses: docker/[email protected]
-
name: Set up Docker Buildx
uses: docker/[email protected]
-
name: Login to DockerHub
uses: docker/[email protected]
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
uses: docker/[email protected]
with:
context: .
push: true
tags: user/app:latest

Advanced usage

Multi-platform image

You can build multi-platform images using the platforms input as described below.

💡 List of available platforms will be displayed and available through our setup-buildx - https://github.com/docker/setup-buildx-action#about action.

💡 If you want support for more platforms, you can use QEMU with our setup-qemu - https://github.com/docker/setup-qemu-action action.

1
2
3
4
5
6
7
8
      -
name: Build and push
uses: docker/[email protected]
with:
context: .
+ platforms: linux/amd64,linux/arm64
push: true
tags: user/app:latest

See build-push-action/multi-platform.md at master · docker/build-push-action - https://github.com/docker/build-push-action/blob/master/docs/advanced/multi-platform.md to learn more.

Handle tags and labels

If you want an “automatic” tag management and OCI Image Format Specification for labels, you can do it in a dedicated step. The following workflow will use the Docker metadata action to handle tags and labels based on GitHub actions events and Git metadata.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/[email protected]
+ -
+ name: Docker meta
+ id: meta
+ uses: docker/[email protected]
+ with:
+ # list of Docker images to use as base name for tags
+ images: |
+ name/app
+ # generate Docker tags based on the following events/attributes
+ tags: |
+ type=schedule
+ type=ref,event=branch
+ type=ref,event=pr
+ type=semver,pattern={{version}}
+ type=semver,pattern={{major}}.{{minor}}
+ type=semver,pattern={{major}}
+ type=sha
-
name: Set up QEMU
uses: docker/[email protected]
-
name: Set up Docker Buildx
uses: docker/[email protected]
-
name: Login to DockerHub
if: github.event_name != 'pull_request'
uses: docker/[email protected]
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
uses: docker/[email protected]
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
+ tags: ${{ steps.meta.outputs.tags }}
+ labels: ${{ steps.meta.outputs.labels }}

See build-push-action/tags-labels.md at master · docker/build-push-action - https://github.com/docker/build-push-action/blob/master/docs/advanced/tags-labels.md to learn more.

References

[1] docker/build-push-action: GitHub Action to build and push Docker images with Buildx - https://github.com/docker/build-push-action

[2] Build and push Docker images · Actions · GitHub Marketplace - https://github.com/marketplace/actions/build-and-push-docker-images

[3] build-push-action/UPGRADE.md at master · docker/build-push-action - https://github.com/docker/build-push-action/blob/master/UPGRADE.md

[4] Learn GitHub Actions - GitHub Docs - https://docs.github.com/en/actions/learn-github-actions

[5] Encrypted secrets - GitHub Docs - https://docs.github.com/en/actions/reference/encrypted-secrets

[6] Github - https://github.com/

[7] Docker Hub - https://hub.docker.com/