Skip to content

Commit 6efc2b0

Browse files
authored
Merge pull request #206 from crazy-max/docker-meta
Simplify "Complete workflow" with Docker meta action
2 parents 2e36e43 + 953dc85 commit 6efc2b0

File tree

4 files changed

+34
-114
lines changed

4 files changed

+34
-114
lines changed

.github/workflows/example.yml

+16-48
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# This workflow is provided just as an usage example and not for repo testing/verification
2-
# See https://github.com/docker/build-push-action#complete-workflow
32
name: example
43

54
on:
@@ -12,6 +11,9 @@ on:
1211
- 'v*.*.*'
1312
pull_request:
1413

14+
env:
15+
DOCKER_IMAGE: localhost:5000/name/app
16+
1517
jobs:
1618
docker:
1719
runs-on: ubuntu-latest
@@ -25,34 +27,12 @@ jobs:
2527
name: Checkout
2628
uses: actions/[email protected]
2729
-
28-
name: Prepare
29-
id: prep
30-
run: |
31-
DOCKER_IMAGE=localhost:5000/name/app
32-
VERSION=noop
33-
if [ "${{ github.event_name }}" = "schedule" ]; then
34-
VERSION=nightly
35-
elif [[ $GITHUB_REF == refs/tags/* ]]; then
36-
VERSION=${GITHUB_REF#refs/tags/}
37-
elif [[ $GITHUB_REF == refs/heads/* ]]; then
38-
VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g')
39-
if [ "${{ github.event.repository.default_branch }}" = "$VERSION" ]; then
40-
VERSION=edge
41-
fi
42-
elif [[ $GITHUB_REF == refs/pull/* ]]; then
43-
VERSION=pr-${{ github.event.number }}
44-
fi
45-
TAGS="${DOCKER_IMAGE}:${VERSION}"
46-
if [[ $VERSION =~ ^v[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
47-
MINOR=${VERSION%.*}
48-
MAJOR=${MINOR%.*}
49-
TAGS="$TAGS,${DOCKER_IMAGE}:${MINOR},${DOCKER_IMAGE}:${MAJOR},${DOCKER_IMAGE}:latest"
50-
elif [ "${{ github.event_name }}" = "push" ]; then
51-
TAGS="$TAGS,${DOCKER_IMAGE}:sha-${GITHUB_SHA::8}"
52-
fi
53-
echo ::set-output name=version::${VERSION}
54-
echo ::set-output name=tags::${TAGS}
55-
echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ')
30+
name: Docker meta
31+
id: docker_meta
32+
uses: crazy-max/ghaction-docker-meta@v1
33+
with:
34+
images: ${{ env.DOCKER_IMAGE }} # list of Docker images to use as base name for tags
35+
tag-sha: true # add git short SHA as Docker tag
5636
-
5737
name: Set up Docker Buildx
5838
uses: docker/setup-buildx-action@v1
@@ -64,39 +44,27 @@ jobs:
6444
with:
6545
context: ./test
6646
file: ./test/Dockerfile
67-
outputs: type=docker
68-
tags: ${{ steps.prep.outputs.tags }}
69-
labels: |
70-
org.opencontainers.image.created=${{ steps.prep.outputs.created }}
71-
org.opencontainers.image.url=${{ github.event.repository.html_url }}
72-
org.opencontainers.image.source=${{ github.event.repository.clone_url }}
73-
org.opencontainers.image.version=${{ steps.prep.outputs.version }}
74-
org.opencontainers.image.revision=${{ github.sha }}
75-
org.opencontainers.image.licenses=${{ github.event.repository.license.spdx_id }}
47+
load: true
48+
tags: ${{ steps.docker_meta.outputs.tags }}
49+
labels: ${{ steps.docker_meta.outputs.labels }}
7650
-
7751
name: Build and push to local registry
7852
uses: ./
7953
with:
8054
context: ./test
8155
file: ./test/Dockerfile
8256
push: ${{ github.event_name != 'pull_request' }}
83-
tags: ${{ steps.prep.outputs.tags }}
84-
labels: |
85-
org.opencontainers.image.created=${{ steps.prep.outputs.created }}
86-
org.opencontainers.image.url=${{ github.event.repository.html_url }}
87-
org.opencontainers.image.source=${{ github.event.repository.clone_url }}
88-
org.opencontainers.image.version=${{ steps.prep.outputs.version }}
89-
org.opencontainers.image.revision=${{ github.sha }}
90-
org.opencontainers.image.licenses=${{ github.event.repository.license.spdx_id }}
57+
tags: ${{ steps.docker_meta.outputs.tags }}
58+
labels: ${{ steps.docker_meta.outputs.labels }}
9159
-
9260
name: Inspect image
9361
run: |
94-
docker image inspect localhost:5000/name/app:${{ steps.prep.outputs.version }}
62+
docker image inspect ${{ env.DOCKER_IMAGE }}:${{ steps.docker_meta.outputs.version }}
9563
-
9664
name: Check manifest
9765
if: github.event_name != 'pull_request'
9866
run: |
99-
docker buildx imagetools inspect localhost:5000/name/app:${{ steps.prep.outputs.version }}
67+
docker buildx imagetools inspect ${{ env.DOCKER_IMAGE }}:${{ steps.docker_meta.outputs.version }}
10068
-
10169
name: Dump context
10270
if: always()

README.md

+13-65
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ ___
3939
* [Local registry](#local-registry)
4040
* [Export image to Docker](#export-image-to-docker)
4141
* [Leverage GitHub cache](#leverage-github-cache)
42-
* [Complete workflow](#complete-workflow)
42+
* [Handle tags and labels](#handle-tags-and-labels)
4343
* [Update DockerHub repo description](#update-dockerhub-repo-description)
4444
* [Customizing](#customizing)
4545
* [inputs](#inputs)
@@ -472,17 +472,12 @@ using [actions/cache](https://github.com/actions/cache) with this action:
472472
> If you want to [export layers for all stages](https://github.com/docker/buildx#--cache-tonametypetypekeyvalue),
473473
> you have to specify `mode=max` attribute in `cache-to`.
474474

475-
### Complete workflow
475+
### Handle tags and labels
476476

477477
If you come from [`v1`](https://github.com/docker/build-push-action/tree/releases/v1#readme) and want an
478-
"automatic" tag management through Git reference and [OCI Image Format Specification](https://github.com/opencontainers/image-spec/blob/master/annotations.md)
479-
for labels, you will have to do it in a dedicated step.
480-
481-
The following workflow with the `Prepare` step will generate some [outputs](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjobs_idoutputs)
482-
to handle tags and labels based on GitHub actions events.
483-
484-
This is just an example to show many cases that you might want to use and that you will have to adapt according
485-
to your needs:
478+
"automatic" tag management and [OCI Image Format Specification](https://github.com/opencontainers/image-spec/blob/master/annotations.md)
479+
for labels, you can do it in a dedicated step. The following workflow will use the [Docker meta action](https://github.com/crazy-max/ghaction-docker-meta)
480+
to handle tags and labels based on GitHub actions events and Git metadata.
486481

487482
<details>
488483
<summary><b>Show workflow</b></summary>
@@ -508,42 +503,12 @@ to your needs:
508503
name: Checkout
509504
uses: actions/checkout@v2
510505
-
511-
name: Repo metadata
512-
id: repo
513-
uses: actions/github-script@v3
506+
name: Docker meta
507+
id: docker_meta
508+
uses: crazy-max/ghaction-docker-meta@v1
514509
with:
515-
script: |
516-
const repo = await github.repos.get(context.repo)
517-
return repo.data
518-
-
519-
name: Prepare
520-
id: prep
521-
run: |
522-
DOCKER_IMAGE=name/app
523-
VERSION=noop
524-
if [ "${{ github.event_name }}" = "schedule" ]; then
525-
VERSION=nightly
526-
elif [[ $GITHUB_REF == refs/tags/* ]]; then
527-
VERSION=${GITHUB_REF#refs/tags/}
528-
elif [[ $GITHUB_REF == refs/heads/* ]]; then
529-
VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g')
530-
if [ "${{ github.event.repository.default_branch }}" = "$VERSION" ]; then
531-
VERSION=edge
532-
fi
533-
elif [[ $GITHUB_REF == refs/pull/* ]]; then
534-
VERSION=pr-${{ github.event.number }}
535-
fi
536-
TAGS="${DOCKER_IMAGE}:${VERSION}"
537-
if [[ $VERSION =~ ^v[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
538-
MINOR=${VERSION%.*}
539-
MAJOR=${MINOR%.*}
540-
TAGS="$TAGS,${DOCKER_IMAGE}:${MINOR},${DOCKER_IMAGE}:${MAJOR},${DOCKER_IMAGE}:latest"
541-
elif [ "${{ github.event_name }}" = "push" ]; then
542-
TAGS="$TAGS,${DOCKER_IMAGE}:sha-${GITHUB_SHA::8}"
543-
fi
544-
echo ::set-output name=version::${VERSION}
545-
echo ::set-output name=tags::${TAGS}
546-
echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ')
510+
images: name/app # list of Docker images to use as base name for tags
511+
tag-sha: true # add git short SHA as Docker tag
547512
-
548513
name: Set up QEMU
549514
uses: docker/setup-qemu-action@v1
@@ -566,28 +531,11 @@ to your needs:
566531
file: ./Dockerfile
567532
platforms: linux/amd64,linux/arm64,linux/386
568533
push: ${{ github.event_name != 'pull_request' }}
569-
tags: ${{ steps.prep.outputs.tags }}
570-
labels: |
571-
org.opencontainers.image.title=${{ fromJson(steps.repo.outputs.result).name }}
572-
org.opencontainers.image.description=${{ fromJson(steps.repo.outputs.result).description }}
573-
org.opencontainers.image.url=${{ fromJson(steps.repo.outputs.result).html_url }}
574-
org.opencontainers.image.source=${{ fromJson(steps.repo.outputs.result).clone_url }}
575-
org.opencontainers.image.version=${{ steps.prep.outputs.version }}
576-
org.opencontainers.image.created=${{ steps.prep.outputs.created }}
577-
org.opencontainers.image.revision=${{ github.sha }}
578-
org.opencontainers.image.licenses=${{ fromJson(steps.repo.outputs.result).license.spdx_id }}
534+
tags: ${{ steps.docker_meta.outputs.tags }}
535+
labels: ${{ steps.docker_meta.outputs.labels }}
579536
```
580537
</details>
581538

582-
| Event | Ref | Commit SHA | Docker Tag | Pushed |
583-
|-----------------|-------------------------------|------------|------------------------------------|--------|
584-
| `schedule` | | | `nightly` | Yes |
585-
| `pull_request` | `refs/pull/2/merge` | `a123b57` | `pr-2` | No |
586-
| `push` | `refs/heads/<default_branch>` | `676cae2` | `sha-676cae2`, `edge` | Yes |
587-
| `push` | `refs/heads/dev` | `cf20257` | `sha-cf20257`, `dev` | Yes |
588-
| `push` | `refs/heads/my/branch` | `a5df687` | `sha-a5df687`, `my-branch` | Yes |
589-
| `push tag` | `refs/tags/v1.2.3` | | `v1.2.3`, `v1.2`, `v1`, `latest` | Yes |
590-
591539
### Update DockerHub repo description
592540

593541
You can update the [DockerHub repository description](https://docs.docker.com/docker-hub/repos/) using
@@ -673,7 +621,7 @@ Following inputs can be used as `step.with` keys
673621
| `cache-from` | List | List of [external cache sources](https://github.com/docker/buildx#--cache-fromnametypetypekeyvalue) (eg. `type=local,src=path/to/dir`) |
674622
| `cache-to` | List | List of [cache export destinations](https://github.com/docker/buildx#--cache-tonametypetypekeyvalue) (eg. `type=local,dest=path/to/dir`) |
675623
| `secrets` | List | List of secrets to expose to the build (eg. `key=value`, `GIT_AUTH_TOKEN=mytoken`) |
676-
| `ssh` | List | List of SSH agent socket or keys to expose to the build (eg: `default|<id>[=<socket>|<key>[,<key>]]`) |
624+
| `ssh` | List | List of SSH agent socket or keys to expose to the build |
677625

678626
### outputs
679627

UPGRADE.md

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ steps:
9191
push: ${{ github.event_name != 'pull_request' }}
9292
tag_with_ref: true
9393
tag_with_sha: true
94+
add_git_labels: true
9495
```
9596
9697
```yaml
@@ -142,3 +143,6 @@ steps:
142143
org.opencontainers.image.created=${{ steps.prep.outputs.created }}
143144
org.opencontainers.image.revision=${{ github.sha }}
144145
```
146+
147+
> You can also use the [Docker meta action](https://github.com/crazy-max/ghaction-docker-meta) to handle tags and
148+
> labels based on GitHub actions events and Git metadata. A workflow example is available in the [README](README.md#handle-tags-and-labels).

action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ inputs:
6868
default: ${{ github.token }}
6969
required: false
7070
ssh:
71-
description: "List of SSH agent socket or keys to expose to the build (eg: default)"
71+
description: "List of SSH agent socket or keys to expose to the build"
7272
required: false
7373

7474
outputs:

0 commit comments

Comments
 (0)