From dbdfd86c1d1e402adeb2c6f293275ae35d99e982 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sat, 12 Sep 2020 01:44:53 +0200 Subject: [PATCH] Enhanced with OCI Image Format Specification labels Signed-off-by: CrazyMax --- .github/workflows/ci.yml | 78 +++++++++++++++++++++++++++++++++++++--- README.md | 32 ++++++++++++----- 2 files changed, 97 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3452d5b..2fdc40e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,11 +2,11 @@ name: ci on: push: - branches: - - master + branches: master + tags: + - 'v*.*.*' pull_request: - branches: - - master + branches: master jobs: git-context: @@ -233,3 +233,73 @@ jobs: name: Dump context if: always() 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 diff --git a/README.md b/README.md index 9551623..8ff3e28 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ ___ * [Multi-platform image](#multi-platform-image) * [Local registry](#local-registry) * [Leverage GitHub cache](#leverage-github-cache) - * [Tags handling](#tags-handling) + * [Tags and labels handling](#tags-and-labels-handling) * [Complete workflow](#complete-workflow) * [Update DockerHub repo description](#update-dockerhub-repo-description) * [Customizing](#customizing) @@ -298,17 +298,18 @@ jobs: 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 -"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 [`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: * `user/app:pr-2` -* On `push` event with `refs/heads/master` ref and `676cae2` sha: +* On `push` event with `refs/heads/` ref and `676cae2` sha: * `user/app:sha-676cae2` * `user/app:edge` * On `push` event with `refs/heads/dev` ref and `cf20257` sha: @@ -344,22 +345,29 @@ jobs: id: prep run: | DOCKER_IMAGE=name/app - VERSION=edge + 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 - if [ "${{ github.event_name }}" = "pull_request" ]; then - TAGS="${DOCKER_IMAGE}:pr-${{ github.event.number }}" - else - TAGS="${DOCKER_IMAGE}:${VERSION}" + if [ "${{ github.event_name }}" != "pull_request" ]; then TAGS="${DOCKER_IMAGE}:sha-${GITHUB_SHA::8}" 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" 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 uses: docker/setup-buildx-action@v1 @@ -371,6 +379,12 @@ jobs: context: . file: ./Dockerfile 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