From beea0809636e419a30dff2fef105c4194446054d Mon Sep 17 00:00:00 2001 From: Kelvin Steiner Date: Thu, 15 Feb 2024 10:19:30 -0300 Subject: [PATCH] tag docker image with branch name and git tags --- .github/workflows/docker-build-n-push.yml | 16 +++++++++-- .github/workflows/docker-retag.yml | 35 +++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/docker-retag.yml diff --git a/.github/workflows/docker-build-n-push.yml b/.github/workflows/docker-build-n-push.yml index ef1f326bc..a55b1ed24 100644 --- a/.github/workflows/docker-build-n-push.yml +++ b/.github/workflows/docker-build-n-push.yml @@ -2,8 +2,10 @@ name: Build and push Docker image on: push: - branches: [current, feb12] + branches: [current, test] +env: + image_base: ghcr.io/agicommies/subspace jobs: build-n-push: @@ -25,10 +27,18 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} + - name: Compute image names for commit tags + run: | + tags=$(git tag --contains ${{ steps.commit.outputs.short }}) + image_names=$(<<< $tags xargs -I {_} printf "$image_base:{_}\n") + echo tags_image_names=$image_names | tee -a $GITHUB_ENV + - name: Build and push uses: docker/build-push-action@v5 with: context: . push: true - tags: ghcr.io/agicommies/subspace:${{ steps.commit.outputs.short }} - # , ghcr.io/agicommies/subspace:latest + tags: | + ${{ env.image_base }}:${{ steps.commit.outputs.short }} + ${{ env.image_base }}:${{ github.head_ref || github.ref_name }} + ${{ env.tags_image_names }} diff --git a/.github/workflows/docker-retag.yml b/.github/workflows/docker-retag.yml new file mode 100644 index 000000000..681fa6498 --- /dev/null +++ b/.github/workflows/docker-retag.yml @@ -0,0 +1,35 @@ +name: Tag docker image with git tags + +on: + push: + tags: ["*"] + +env: + image_base: ghcr.io/agicommies/subspace + +jobs: + tag-docker-image: + permissions: write-all + runs-on: ubuntu-latest + + steps: + - id: commit + uses: prompt/actions-commit-hash@v3 + + - name: Docker Login + uses: docker/login-action@v3.0.0 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Checks if docker image is on registry + env: + image_of_commit: $image_base:${{ steps.commit.outputs.short }} + run: | + image_exists_check=$(docker manifest inspect "$image_of_commit" > /dev/null 2>&1; echo $?) + + if [ $image_exists_check -eq 0 ]; then + echo "Image exists. Tagging with `${{ github.ref_name }}`" + docker buildx imagetools create "$image_of_commit" --tag "$image_base:${{ github.ref_name }}" + fi