From 83b2aeab4738d1d68986bb3ad54dee57d64358a5 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 10 Sep 2020 22:24:13 +0200 Subject: [PATCH 1/6] Add tags handling example Signed-off-by: CrazyMax --- README.md | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 89db5db..9551623 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ ___ * [Multi-platform image](#multi-platform-image) * [Local registry](#local-registry) * [Leverage GitHub cache](#leverage-github-cache) + * [Tags handling](#tags-handling) * [Complete workflow](#complete-workflow) * [Update DockerHub repo description](#update-dockerhub-repo-description) * [Customizing](#customizing) @@ -297,6 +298,81 @@ jobs: cache-to: type=local,dest=/tmp/.buildx-cache ``` +### Tags 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). + +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: + * `user/app:sha-676cae2` + * `user/app:edge` +* On `push` event with `refs/heads/dev` ref and `cf20257` sha: + * `user/app:sha-cf20257` + * `user/app:dev` +* On `push tags` event with `refs/tags/v1.2.3` ref and `9258794` sha: + * `user/app:sha-9258794` + * `user/app:v1.2.3` + * `user/app:v1.2` + * `user/app:v1` + * `user/app:latest` + +```yaml +name: ci + +on: + push: + branches: master + tags: + - 'v*.*.*' + pull_request: + branches: master + +jobs: + docker: + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@v2 + - + name: Prepare + id: prep + run: | + DOCKER_IMAGE=name/app + VERSION=edge + if [[ $GITHUB_REF == refs/tags/* ]]; then + VERSION=${GITHUB_REF#refs/tags/} + fi + 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}" + 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=tags::${TAGS} + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + - + name: Build and push + id: docker_build + uses: docker/build-push-action@v2 + with: + context: . + file: ./Dockerfile + tags: ${{ steps.prep.outputs.tags }} +``` + ### Complete workflow * On `pull_request` event, Docker image `name/app:edge` is **built**. @@ -331,13 +407,13 @@ jobs: DOCKER_IMAGE=name/app VERSION=edge if [[ $GITHUB_REF == refs/tags/* ]]; then - VERSION=${GITHUB_REF#refs/tags/v} + VERSION=${GITHUB_REF#refs/tags/} fi if [ "${{ github.event_name }}" = "schedule" ]; then VERSION=nightly fi TAGS="${DOCKER_IMAGE}:${VERSION}" - if [[ $VERSION =~ ^[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 TAGS="$TAGS,${DOCKER_IMAGE}:latest" fi echo ::set-output name=tags::${TAGS} @@ -356,7 +432,6 @@ jobs: password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Build and push - id: docker_build uses: docker/build-push-action@v2 with: context: . From 28d7281ebf18a186abe615a561b3a94ff9ce8464 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Fri, 11 Sep 2020 00:53:06 +0200 Subject: [PATCH 2/6] Cleanup Signed-off-by: CrazyMax --- .github/workflows/ci.yml | 2 -- .github/workflows/test.yml | 2 -- 2 files changed, 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cace6b5..3452d5b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,11 +4,9 @@ on: push: branches: - master - - v2-working-branch # remove when merged to master pull_request: branches: - master - - v2-working-branch # remove when merged to master jobs: git-context: diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0aaadcd..2c1fe83 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,11 +4,9 @@ on: push: branches: - master - - v2-working-branch # remove when merged to master pull_request: branches: - master - - v2-working-branch # remove when merged to master jobs: test: From dbdfd86c1d1e402adeb2c6f293275ae35d99e982 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sat, 12 Sep 2020 01:44:53 +0200 Subject: [PATCH 3/6] 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 From 371daebc6fd2e7115bc1c24ccd8e49f48dbfdd93 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sat, 12 Sep 2020 20:40:12 +0200 Subject: [PATCH 4/6] Update example with explanations Signed-off-by: CrazyMax --- .github/workflows/ci.yml | 70 -------------------- .github/workflows/example.yml | 100 ++++++++++++++++++++++++++++ README.md | 120 ++++++++++------------------------ 3 files changed, 134 insertions(+), 156 deletions(-) create mode 100644 .github/workflows/example.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2fdc40e..9a0b353 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -233,73 +233,3 @@ 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/.github/workflows/example.yml b/.github/workflows/example.yml new file mode 100644 index 0000000..947c6a7 --- /dev/null +++ b/.github/workflows/example.yml @@ -0,0 +1,100 @@ +name: example + +on: + schedule: + - cron: '0 10 * * 0' # everyday sunday at 10am + push: + branches: + - '**' + tags: + - 'v*.*.*' + pull_request: + +jobs: + docker: + 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.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 + if [[ $GITHUB_REF != refs/tags/* ]] && [[ "${{ github.event_name }}" = "push" ]]; 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 + with: + driver-opts: network=host + - + name: Build and export to Docker client + uses: ./ + with: + context: ./test + file: ./test/Dockerfile + outputs: type=docker + 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: Build and push to local registry + uses: ./ + with: + context: ./test + file: ./test/Dockerfile + push: ${{ github.event_name != 'pull_request' }} + 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 image + run: | + docker image inspect localhost:5000/name/app:${{ steps.prep.outputs.version }} + - + name: Check manifest + if: github.event_name != 'pull_request' + 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 8ff3e28..437c162 100644 --- a/README.md +++ b/README.md @@ -22,9 +22,9 @@ ___ * [Path context](#path-context) * [Isolated builders](#isolated-builders) * [Multi-platform image](#multi-platform-image) +* [Advanced usage](#advanced-usage) * [Local registry](#local-registry) * [Leverage GitHub cache](#leverage-github-cache) - * [Tags and labels handling](#tags-and-labels-handling) * [Complete workflow](#complete-workflow) * [Update DockerHub repo description](#update-dockerhub-repo-description) * [Customizing](#customizing) @@ -42,7 +42,10 @@ build-secrets, remote cache, etc. and different builder deployment/namespacing o ### Git context -The default behavior of this action is to use the Git context invoked by your workflow (`https://github.com/owner/repo#ref`). +The default behavior of this action is to use the [Git context invoked by your workflow](https://github.com/docker/build-push-action/blob/master/src/context.ts#L35). + +> :warning: Subdir for this context is [not yet supported](https://github.com/docker/build-push-action/issues/120). +> For the moment you can use the [path context](#path-context). ```yaml name: ci @@ -215,6 +218,8 @@ jobs: user/app:1.0.0 ``` +## Advanced usage + ### Local registry For testing purposes you may need to create a [local registry](https://hub.docker.com/_/registry) to push images into. @@ -298,40 +303,37 @@ jobs: cache-to: type=local,dest=/tmp/.buildx-cache ``` -### Tags and labels handling +### Complete workflow 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 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): +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: -* On `pull_request` event with `refs/pull/2/merge` ref and `a123b57` sha: - * `user/app:pr-2` -* 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: - * `user/app:sha-cf20257` - * `user/app:dev` -* On `push tags` event with `refs/tags/v1.2.3` ref and `9258794` sha: - * `user/app:sha-9258794` - * `user/app:v1.2.3` - * `user/app:v1.2` - * `user/app:v1` - * `user/app:latest` +| Event | Ref | Commit SHA | Docker Tag | Pushed | +|-----------------|-------------------------------|------------|------------------------------------|--------| +| `schedule` | | | `nightly` | Yes | +| `pull_request` | `refs/pull/2/merge` | `a123b57` | `pr-2` | No | +| `push` | `refs/heads/` | `676cae2` | `sha-676cae2`, `edge` | Yes | +| `push` | `/refs/heads/dev` | `cf20257` | `sha-676cae2`, `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 | ```yaml name: ci on: + schedule: + - cron: '0 10 * * *' # everyday at 10am push: - branches: master + branches: + - '**' tags: - 'v*.*.*' pull_request: - branches: master jobs: docker: @@ -346,7 +348,9 @@ jobs: run: | DOCKER_IMAGE=name/app VERSION=noop - if [[ $GITHUB_REF == refs/tags/* ]]; then + 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') @@ -356,7 +360,7 @@ jobs: elif [[ $GITHUB_REF == refs/pull/* ]]; then VERSION=pr-${{ github.event.number }} fi - if [ "${{ github.event_name }}" != "pull_request" ]; then + if [[ $GITHUB_REF != refs/tags/* ]] && [[ "${{ github.event_name }}" = "push" ]]; then TAGS="${DOCKER_IMAGE}:sha-${GITHUB_SHA::8}" fi TAGS="${DOCKER_IMAGE}:${VERSION}" @@ -368,69 +372,6 @@ jobs: 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 - - - name: Build and push - id: docker_build - uses: docker/build-push-action@v2 - with: - 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 - -* On `pull_request` event, Docker image `name/app:edge` is **built**. -* On `push` event, Docker image `name/app:edge` is **built** and **pushed** to DockerHub. -* On `schedule` event, Docker image `name/app:nightly` is **built** and **pushed** to DockerHub. -* On `push tags` event, Docker image `name/app:` and `name/app:latest` is **built** and **pushed** to DockerHub. - -```yaml -name: ci - -on: - schedule: - - cron: '0 10 * * *' # everyday at 10am - push: - branches: master - tags: - - 'v*.*.*' - pull_request: - branches: master - -jobs: - docker: - runs-on: ubuntu-latest - steps: - - - name: Checkout - uses: actions/checkout@v2 - - - name: Prepare - id: prep - run: | - DOCKER_IMAGE=name/app - VERSION=edge - if [[ $GITHUB_REF == refs/tags/* ]]; then - VERSION=${GITHUB_REF#refs/tags/} - fi - if [ "${{ github.event_name }}" = "schedule" ]; then - VERSION=nightly - fi - TAGS="${DOCKER_IMAGE}:${VERSION}" - if [[ $VERSION =~ ^v[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then - TAGS="$TAGS,${DOCKER_IMAGE}:latest" - fi - echo ::set-output name=tags::${TAGS} - name: Set up QEMU uses: docker/setup-qemu-action@v1 @@ -446,6 +387,7 @@ jobs: password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Build and push + id: docker_build uses: docker/build-push-action@v2 with: context: . @@ -453,6 +395,12 @@ jobs: platforms: linux/amd64,linux/arm64,linux/386 push: ${{ github.event_name != 'pull_request' }} 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 }} ``` ### Update DockerHub repo description From 9a445780ad16ce17c4965c8d11d1f54af0d186b5 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sat, 12 Sep 2020 20:53:25 +0200 Subject: [PATCH 5/6] Fix events on CI workflow Signed-off-by: CrazyMax --- .github/workflows/ci.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9a0b353..3452d5b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,11 +2,11 @@ name: ci on: push: - branches: master - tags: - - 'v*.*.*' + branches: + - master pull_request: - branches: master + branches: + - master jobs: git-context: From 6fd2a708bd093a7301cc29f4361c0e055fed19d0 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sat, 12 Sep 2020 21:14:33 +0200 Subject: [PATCH 6/6] Update example workflow Signed-off-by: CrazyMax --- .github/workflows/example.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/example.yml b/.github/workflows/example.yml index 947c6a7..337eab7 100644 --- a/.github/workflows/example.yml +++ b/.github/workflows/example.yml @@ -1,3 +1,5 @@ +# 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 on: