From f6a224dc50e67bb12c96b250c40af08b597a272f Mon Sep 17 00:00:00 2001 From: Christian Kadner Date: Fri, 1 Mar 2024 08:27:45 -0800 Subject: [PATCH] ci: Generate tags using docker/metadata-action (#4) Use the `docker/metadata-action` to generate image tags, remove push target from Makefile Signed-off-by: Christian Kadner --- .github/workflows/build.yml | 18 +++++++++++++++--- Makefile | 9 --------- scripts/get_image_tags.sh | 37 ------------------------------------- 3 files changed, 15 insertions(+), 49 deletions(-) delete mode 100755 scripts/get_image_tags.sh diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 96b3c57..54c2f64 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -40,6 +40,7 @@ jobs: - name: "Log in to Github Container registry" uses: docker/login-action@v3 + if: github.event_name != 'pull_request' with: registry: ghcr.io username: ${{ github.actor }} @@ -47,6 +48,7 @@ jobs: - name: "Log in to quay" uses: docker/login-action@v3 + if: github.event_name != 'pull_request' with: registry: quay.io username: wxpe+github_pusher_bot @@ -73,15 +75,25 @@ jobs: fi echo "CACHE_TO=$CACHE_TO" >> $GITHUB_ENV - - name: "push tags" - run: echo "PUSH_TAGS=$(scripts/get_image_tags.sh ${QUAY_REPOSITORY} | tr -s '[:blank:]' ',')" >> $GITHUB_ENV + - name: "Generate tags" + id: meta + uses: docker/metadata-action@v5 + with: + images: | + ${{ env.QUAY_REPOSITORY }} + tags: | + type=ref,event=branch + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=sha,enable=true,priority=100,prefix=,suffix=,format=short + type=sha,enable=true,priority=100,prefix=${{ github.ref_name }}.,suffix=,format=short - name: "Build and push" uses: docker/build-push-action@v5 with: context: . target: router-release - tags: ${{ env.PUSH_TAGS }} + tags: ${{ steps.meta.outputs.tags }} cache-from: type=registry,ref=${{ env.CACHE_IMAGE }} cache-to: ${{ env.CACHE_TO }} push: ${{ github.event_name != 'pull_request' }} diff --git a/Makefile b/Makefile index 2526be9..85a44e5 100644 --- a/Makefile +++ b/Makefile @@ -28,12 +28,3 @@ build-router: ## --build-arg BUILDKIT_INLINE_CACHE=1 \ --tag "$(server_image_name)" . docker images - -.PHONY: push-router-image -push-router-image: ## - @for tag in $$(./scripts/get_image_tags.sh); do \ - image_to_push="$(server_image_repo):$${tag}"; \ - docker tag "$(server_image_name)" "$${image_to_push}"; \ - echo "Pushing image $${image_to_push}"; \ - docker push --quiet "$${image_to_push}"; \ - done diff --git a/scripts/get_image_tags.sh b/scripts/get_image_tags.sh deleted file mode 100755 index 9f7609a..0000000 --- a/scripts/get_image_tags.sh +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env bash - -# Returns a space separated list of container image tags to be pushed to the -# registry -# If a repository is supplied as the first arg, (e.g. quay.io/foo/bar") then the tags will be fully qualified - -tags_to_push="" - -if [[ -n $1 ]]; then - repo_bit="${1}:" -else - repo_bit="" -fi - -# if running locally, i.e. CI is unset -if [[ -z "${CI+x}" ]]; -then - commit="$(git rev-parse --short HEAD)" - branch="$(git rev-parse --abbrev-ref HEAD)" - - tags_to_push+="${repo_bit}${commit}" - tags_to_push+=" ${repo_bit}${branch}" - tags_to_push+=" ${repo_bit}${branch}.${commit}" -else - # In CI, pull info from github env vars - commit="${GITHUB_SHA:0:7}" - build_ref="${GITHUB_REF_NAME}" - - tags_to_push+="${repo_bit}${commit}" - if [[ ! ${build_ref} =~ "merge" ]]; - then - tags_to_push+=" ${repo_bit}${build_ref}" - tags_to_push+=" ${repo_bit}${build_ref}.${commit}" - fi -fi - -echo "${tags_to_push}"