3
0
Fork 0
mirror of https://gitea.com/docker/build-push-action.git synced 2024-11-25 19:09:40 +01:00

Simplify "Complete workflow" with Docker meta action

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2020-10-28 19:40:11 +01:00
parent 2e36e439bc
commit 953dc85723
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
4 changed files with 34 additions and 114 deletions

View file

@ -1,5 +1,4 @@
# This workflow is provided just as an usage example and not for repo testing/verification # This workflow is provided just as an usage example and not for repo testing/verification
# See https://github.com/docker/build-push-action#complete-workflow
name: example name: example
on: on:
@ -12,6 +11,9 @@ on:
- 'v*.*.*' - 'v*.*.*'
pull_request: pull_request:
env:
DOCKER_IMAGE: localhost:5000/name/app
jobs: jobs:
docker: docker:
runs-on: ubuntu-latest runs-on: ubuntu-latest
@ -25,34 +27,12 @@ jobs:
name: Checkout name: Checkout
uses: actions/checkout@v2.3.3 uses: actions/checkout@v2.3.3
- -
name: Prepare name: Docker meta
id: prep id: docker_meta
run: | uses: crazy-max/ghaction-docker-meta@v1
DOCKER_IMAGE=localhost:5000/name/app with:
VERSION=noop images: ${{ env.DOCKER_IMAGE }} # list of Docker images to use as base name for tags
if [ "${{ github.event_name }}" = "schedule" ]; then tag-sha: true # add git short SHA as Docker tag
VERSION=nightly
elif [[ $GITHUB_REF == refs/tags/* ]]; then
VERSION=${GITHUB_REF#refs/tags/}
elif [[ $GITHUB_REF == refs/heads/* ]]; then
VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g')
if [ "${{ github.event.repository.default_branch }}" = "$VERSION" ]; then
VERSION=edge
fi
elif [[ $GITHUB_REF == refs/pull/* ]]; then
VERSION=pr-${{ github.event.number }}
fi
TAGS="${DOCKER_IMAGE}:${VERSION}"
if [[ $VERSION =~ ^v[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
MINOR=${VERSION%.*}
MAJOR=${MINOR%.*}
TAGS="$TAGS,${DOCKER_IMAGE}:${MINOR},${DOCKER_IMAGE}:${MAJOR},${DOCKER_IMAGE}:latest"
elif [ "${{ github.event_name }}" = "push" ]; then
TAGS="$TAGS,${DOCKER_IMAGE}:sha-${GITHUB_SHA::8}"
fi
echo ::set-output name=version::${VERSION}
echo ::set-output name=tags::${TAGS}
echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ')
- -
name: Set up Docker Buildx name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1 uses: docker/setup-buildx-action@v1
@ -64,15 +44,9 @@ jobs:
with: with:
context: ./test context: ./test
file: ./test/Dockerfile file: ./test/Dockerfile
outputs: type=docker load: true
tags: ${{ steps.prep.outputs.tags }} tags: ${{ steps.docker_meta.outputs.tags }}
labels: | labels: ${{ steps.docker_meta.outputs.labels }}
org.opencontainers.image.created=${{ steps.prep.outputs.created }}
org.opencontainers.image.url=${{ github.event.repository.html_url }}
org.opencontainers.image.source=${{ github.event.repository.clone_url }}
org.opencontainers.image.version=${{ steps.prep.outputs.version }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.licenses=${{ github.event.repository.license.spdx_id }}
- -
name: Build and push to local registry name: Build and push to local registry
uses: ./ uses: ./
@ -80,23 +54,17 @@ jobs:
context: ./test context: ./test
file: ./test/Dockerfile file: ./test/Dockerfile
push: ${{ github.event_name != 'pull_request' }} push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.prep.outputs.tags }} tags: ${{ steps.docker_meta.outputs.tags }}
labels: | labels: ${{ steps.docker_meta.outputs.labels }}
org.opencontainers.image.created=${{ steps.prep.outputs.created }}
org.opencontainers.image.url=${{ github.event.repository.html_url }}
org.opencontainers.image.source=${{ github.event.repository.clone_url }}
org.opencontainers.image.version=${{ steps.prep.outputs.version }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.licenses=${{ github.event.repository.license.spdx_id }}
- -
name: Inspect image name: Inspect image
run: | run: |
docker image inspect localhost:5000/name/app:${{ steps.prep.outputs.version }} docker image inspect ${{ env.DOCKER_IMAGE }}:${{ steps.docker_meta.outputs.version }}
- -
name: Check manifest name: Check manifest
if: github.event_name != 'pull_request' if: github.event_name != 'pull_request'
run: | run: |
docker buildx imagetools inspect localhost:5000/name/app:${{ steps.prep.outputs.version }} docker buildx imagetools inspect ${{ env.DOCKER_IMAGE }}:${{ steps.docker_meta.outputs.version }}
- -
name: Dump context name: Dump context
if: always() if: always()

View file

@ -39,7 +39,7 @@ ___
* [Local registry](#local-registry) * [Local registry](#local-registry)
* [Export image to Docker](#export-image-to-docker) * [Export image to Docker](#export-image-to-docker)
* [Leverage GitHub cache](#leverage-github-cache) * [Leverage GitHub cache](#leverage-github-cache)
* [Complete workflow](#complete-workflow) * [Handle tags and labels](#handle-tags-and-labels)
* [Update DockerHub repo description](#update-dockerhub-repo-description) * [Update DockerHub repo description](#update-dockerhub-repo-description)
* [Customizing](#customizing) * [Customizing](#customizing)
* [inputs](#inputs) * [inputs](#inputs)
@ -472,17 +472,12 @@ using [actions/cache](https://github.com/actions/cache) with this action:
> If you want to [export layers for all stages](https://github.com/docker/buildx#--cache-tonametypetypekeyvalue), > If you want to [export layers for all stages](https://github.com/docker/buildx#--cache-tonametypetypekeyvalue),
> you have to specify `mode=max` attribute in `cache-to`. > you have to specify `mode=max` attribute in `cache-to`.
### Complete workflow ### Handle tags and labels
If you come from [`v1`](https://github.com/docker/build-push-action/tree/releases/v1#readme) and want an If you come from [`v1`](https://github.com/docker/build-push-action/tree/releases/v1#readme) and want an
"automatic" tag management through Git reference and [OCI Image Format Specification](https://github.com/opencontainers/image-spec/blob/master/annotations.md) "automatic" tag management and [OCI Image Format Specification](https://github.com/opencontainers/image-spec/blob/master/annotations.md)
for labels, you will have to do it in a dedicated step. 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)
to handle tags and labels based on GitHub actions events and Git metadata.
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)
to handle tags and labels based on GitHub actions events.
This is just an example to show many cases that you might want to use and that you will have to adapt according
to your needs:
<details> <details>
<summary><b>Show workflow</b></summary> <summary><b>Show workflow</b></summary>
@ -508,42 +503,12 @@ to your needs:
name: Checkout name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- -
name: Repo metadata name: Docker meta
id: repo id: docker_meta
uses: actions/github-script@v3 uses: crazy-max/ghaction-docker-meta@v1
with: with:
script: | images: name/app # list of Docker images to use as base name for tags
const repo = await github.repos.get(context.repo) tag-sha: true # add git short SHA as Docker tag
return repo.data
-
name: Prepare
id: prep
run: |
DOCKER_IMAGE=name/app
VERSION=noop
if [ "${{ github.event_name }}" = "schedule" ]; then
VERSION=nightly
elif [[ $GITHUB_REF == refs/tags/* ]]; then
VERSION=${GITHUB_REF#refs/tags/}
elif [[ $GITHUB_REF == refs/heads/* ]]; then
VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g')
if [ "${{ github.event.repository.default_branch }}" = "$VERSION" ]; then
VERSION=edge
fi
elif [[ $GITHUB_REF == refs/pull/* ]]; then
VERSION=pr-${{ github.event.number }}
fi
TAGS="${DOCKER_IMAGE}:${VERSION}"
if [[ $VERSION =~ ^v[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
MINOR=${VERSION%.*}
MAJOR=${MINOR%.*}
TAGS="$TAGS,${DOCKER_IMAGE}:${MINOR},${DOCKER_IMAGE}:${MAJOR},${DOCKER_IMAGE}:latest"
elif [ "${{ github.event_name }}" = "push" ]; then
TAGS="$TAGS,${DOCKER_IMAGE}:sha-${GITHUB_SHA::8}"
fi
echo ::set-output name=version::${VERSION}
echo ::set-output name=tags::${TAGS}
echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ')
- -
name: Set up QEMU name: Set up QEMU
uses: docker/setup-qemu-action@v1 uses: docker/setup-qemu-action@v1
@ -566,28 +531,11 @@ to your needs:
file: ./Dockerfile file: ./Dockerfile
platforms: linux/amd64,linux/arm64,linux/386 platforms: linux/amd64,linux/arm64,linux/386
push: ${{ github.event_name != 'pull_request' }} push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.prep.outputs.tags }} tags: ${{ steps.docker_meta.outputs.tags }}
labels: | labels: ${{ steps.docker_meta.outputs.labels }}
org.opencontainers.image.title=${{ fromJson(steps.repo.outputs.result).name }}
org.opencontainers.image.description=${{ fromJson(steps.repo.outputs.result).description }}
org.opencontainers.image.url=${{ fromJson(steps.repo.outputs.result).html_url }}
org.opencontainers.image.source=${{ fromJson(steps.repo.outputs.result).clone_url }}
org.opencontainers.image.version=${{ steps.prep.outputs.version }}
org.opencontainers.image.created=${{ steps.prep.outputs.created }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.licenses=${{ fromJson(steps.repo.outputs.result).license.spdx_id }}
``` ```
</details> </details>
| Event | Ref | Commit SHA | Docker Tag | Pushed |
|-----------------|-------------------------------|------------|------------------------------------|--------|
| `schedule` | | | `nightly` | Yes |
| `pull_request` | `refs/pull/2/merge` | `a123b57` | `pr-2` | No |
| `push` | `refs/heads/<default_branch>` | `676cae2` | `sha-676cae2`, `edge` | Yes |
| `push` | `refs/heads/dev` | `cf20257` | `sha-cf20257`, `dev` | Yes |
| `push` | `refs/heads/my/branch` | `a5df687` | `sha-a5df687`, `my-branch` | Yes |
| `push tag` | `refs/tags/v1.2.3` | | `v1.2.3`, `v1.2`, `v1`, `latest` | Yes |
### Update DockerHub repo description ### Update DockerHub repo description
You can update the [DockerHub repository description](https://docs.docker.com/docker-hub/repos/) using 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
| `cache-from` | List | List of [external cache sources](https://github.com/docker/buildx#--cache-fromnametypetypekeyvalue) (eg. `type=local,src=path/to/dir`) | | `cache-from` | List | List of [external cache sources](https://github.com/docker/buildx#--cache-fromnametypetypekeyvalue) (eg. `type=local,src=path/to/dir`) |
| `cache-to` | List | List of [cache export destinations](https://github.com/docker/buildx#--cache-tonametypetypekeyvalue) (eg. `type=local,dest=path/to/dir`) | | `cache-to` | List | List of [cache export destinations](https://github.com/docker/buildx#--cache-tonametypetypekeyvalue) (eg. `type=local,dest=path/to/dir`) |
| `secrets` | List | List of secrets to expose to the build (eg. `key=value`, `GIT_AUTH_TOKEN=mytoken`) | | `secrets` | List | List of secrets to expose to the build (eg. `key=value`, `GIT_AUTH_TOKEN=mytoken`) |
| `ssh` | List | List of SSH agent socket or keys to expose to the build (eg: `default|<id>[=<socket>|<key>[,<key>]]`) | | `ssh` | List | List of SSH agent socket or keys to expose to the build |
### outputs ### outputs

View file

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

View file

@ -68,7 +68,7 @@ inputs:
default: ${{ github.token }} default: ${{ github.token }}
required: false required: false
ssh: ssh:
description: "List of SSH agent socket or keys to expose to the build (eg: default)" description: "List of SSH agent socket or keys to expose to the build"
required: false required: false
outputs: outputs: