Enhanced with OCI Image Format Specification labels

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2020-09-12 01:44:53 +02:00
parent 28d7281ebf
commit dbdfd86c1d
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
2 changed files with 97 additions and 13 deletions

View file

@ -2,11 +2,11 @@ name: ci
on: on:
push: push:
branches: branches: master
- master tags:
- 'v*.*.*'
pull_request: pull_request:
branches: branches: master
- master
jobs: jobs:
git-context: git-context:
@ -233,3 +233,73 @@ jobs:
name: Dump context name: Dump context
if: always() if: always()
uses: crazy-max/ghaction-dump-context@v1 uses: crazy-max/ghaction-dump-context@v1
tags-labels:
runs-on: ubuntu-latest
services:
registry:
image: registry:2
ports:
- 5000:5000
steps:
-
name: Checkout
uses: actions/checkout@v2.3.2
-
name: Prepare
id: prep
run: |
DOCKER_IMAGE=localhost:5000/name/app
VERSION=noop
if [[ $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 [ "${{ github.event_name }}" != "pull_request" ]; then
TAGS="${DOCKER_IMAGE}:sha-${GITHUB_SHA::8}"
fi
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"
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
uses: docker/setup-qemu-action@v1
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
with:
driver-opts: network=host
-
name: Build and push
uses: ./
with:
context: ./test
file: ./test/Dockerfile
push: true
tags: ${{ steps.prep.outputs.tags }}
labels: |
org.opencontainers.image.created=${{ steps.prep.outputs.created }}
org.opencontainers.image.source=${{ github.repositoryUrl }}
org.opencontainers.image.version=${{ steps.prep.outputs.version }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.licenses=${{ github.event.repository.license.name }}
-
name: Inspect
run: |
docker buildx imagetools inspect localhost:5000/name/app:${{ steps.prep.outputs.version }}
-
name: Dump context
if: always()
uses: crazy-max/ghaction-dump-context@v1

View file

@ -24,7 +24,7 @@ ___
* [Multi-platform image](#multi-platform-image) * [Multi-platform image](#multi-platform-image)
* [Local registry](#local-registry) * [Local registry](#local-registry)
* [Leverage GitHub cache](#leverage-github-cache) * [Leverage GitHub cache](#leverage-github-cache)
* [Tags handling](#tags-handling) * [Tags and labels handling](#tags-and-labels-handling)
* [Complete workflow](#complete-workflow) * [Complete workflow](#complete-workflow)
* [Update DockerHub repo description](#update-dockerhub-repo-description) * [Update DockerHub repo description](#update-dockerhub-repo-description)
* [Customizing](#customizing) * [Customizing](#customizing)
@ -298,17 +298,18 @@ jobs:
cache-to: type=local,dest=/tmp/.buildx-cache cache-to: type=local,dest=/tmp/.buildx-cache
``` ```
### Tags handling ### Tags and labels handling
If you come from [`v1`](https://github.com/docker/build-push-action/tree/releases/v1#readme) and you want an If you come from [`v1`](https://github.com/docker/build-push-action/tree/releases/v1#readme) and you want an
"automatic" tag management through Git reference, you will have to do it in a dedicated step [for now](https://github.com/docker/build-push-action/issues/116). "automatic" tag management through Git reference 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 now](https://github.com/docker/build-push-action/issues/116).
The workflow below with the `Prepare` step will generate a The workflow below with the `Prepare` step will generate a
[`tags` output](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjobs_idoutputs): [`tags` output](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjobs_idoutputs):
* On `pull_request` event with `refs/pull/2/merge` ref and `a123b57` sha: * On `pull_request` event with `refs/pull/2/merge` ref and `a123b57` sha:
* `user/app:pr-2` * `user/app:pr-2`
* On `push` event with `refs/heads/master` ref and `676cae2` sha: * On `push` event with `refs/heads/<default_branch>` ref and `676cae2` sha:
* `user/app:sha-676cae2` * `user/app:sha-676cae2`
* `user/app:edge` * `user/app:edge`
* On `push` event with `refs/heads/dev` ref and `cf20257` sha: * On `push` event with `refs/heads/dev` ref and `cf20257` sha:
@ -344,22 +345,29 @@ jobs:
id: prep id: prep
run: | run: |
DOCKER_IMAGE=name/app DOCKER_IMAGE=name/app
VERSION=edge VERSION=noop
if [[ $GITHUB_REF == refs/tags/* ]]; then if [[ $GITHUB_REF == refs/tags/* ]]; then
VERSION=${GITHUB_REF#refs/tags/} 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 fi
if [ "${{ github.event_name }}" = "pull_request" ]; then if [ "${{ github.event_name }}" != "pull_request" ]; then
TAGS="${DOCKER_IMAGE}:pr-${{ github.event.number }}"
else
TAGS="${DOCKER_IMAGE}:${VERSION}"
TAGS="${DOCKER_IMAGE}:sha-${GITHUB_SHA::8}" TAGS="${DOCKER_IMAGE}:sha-${GITHUB_SHA::8}"
fi fi
TAGS="${DOCKER_IMAGE}:${VERSION}"
if [[ $VERSION =~ ^v[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then if [[ $VERSION =~ ^v[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
MINOR=${VERSION%.*} MINOR=${VERSION%.*}
MAJOR=${MINOR%.*} MAJOR=${MINOR%.*}
TAGS="$TAGS,${DOCKER_IMAGE}:${MINOR},${DOCKER_IMAGE}:${MAJOR},${DOCKER_IMAGE}:latest" TAGS="$TAGS,${DOCKER_IMAGE}:${MINOR},${DOCKER_IMAGE}:${MAJOR},${DOCKER_IMAGE}:latest"
fi fi
echo ::set-output name=version::${VERSION}
echo ::set-output name=tags::${TAGS} 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
@ -371,6 +379,12 @@ jobs:
context: . context: .
file: ./Dockerfile file: ./Dockerfile
tags: ${{ steps.prep.outputs.tags }} tags: ${{ steps.prep.outputs.tags }}
labels: |
org.opencontainers.image.created=${{ steps.prep.outputs.created }}
org.opencontainers.image.source=${{ github.repositoryUrl }}
org.opencontainers.image.version=${{ steps.prep.outputs.version }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.licenses=${{ github.event.repository.license.name }}
``` ```
### Complete workflow ### Complete workflow