Skip to content

Commit 58f8170

Browse files
committed
feat(ci): enhance Docker image tagging and release handling
Add support for 'release' events in GitHub Actions for tagging and publishing Docker images. Modify existing logic to generate additional versioned tags when a release is published, and update conditional checks to handle both 'push' and 'release' events across relevant steps, including login, build, and sign operations. Use a unified environment variable for managing Docker tags.
1 parent 797e4fd commit 58f8170

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

.github/workflows/build-and-publish.yaml

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ on:
44
push:
55
branches:
66
- main
7-
7+
release:
8+
types: [published]
89
workflow_dispatch:
910
pull_request:
1011
types: [opened, synchronize, reopened, edited]
@@ -27,16 +28,38 @@ jobs:
2728
- name: Set up Docker Buildx
2829
uses: docker/setup-buildx-action@v3
2930

31+
- name: Determine tags
32+
run: |
33+
TAGS="ghcr.io/obeone/multi-registry-cache:latest,docker.io/obeoneorg/multi-registry-cache:latest"
34+
35+
if [[ "${{ github.event_name }}" == "release" ]]; then
36+
VERSION=${{ github.event.release.tag_name }}
37+
VERSION=${VERSION#v} # Remove 'v' prefix if present
38+
MAJOR=$(echo $VERSION | cut -d. -f1)
39+
MINOR=$(echo $VERSION | cut -d. -f2)
40+
PATCH=$(echo $VERSION | cut -d. -f3)
41+
42+
TAGS="$TAGS,ghcr.io/obeone/multi-registry-cache:v$VERSION,ghcr.io/obeone/multi-registry-cache:$VERSION"
43+
TAGS="$TAGS,ghcr.io/obeone/multi-registry-cache:v$MAJOR.$MINOR,ghcr.io/obeone/multi-registry-cache:$MAJOR.$MINOR"
44+
TAGS="$TAGS,ghcr.io/obeone/multi-registry-cache:v$MAJOR,ghcr.io/obeone/multi-registry-cache:$MAJOR"
45+
46+
TAGS="$TAGS,docker.io/obeoneorg/multi-registry-cache:v$VERSION,docker.io/obeoneorg/multi-registry-cache:$VERSION"
47+
TAGS="$TAGS,docker.io/obeoneorg/multi-registry-cache:v$MAJOR.$MINOR,docker.io/obeoneorg/multi-registry-cache:$MAJOR.$MINOR"
48+
TAGS="$TAGS,docker.io/obeoneorg/multi-registry-cache:v$MAJOR,docker.io/obeoneorg/multi-registry-cache:$MAJOR"
49+
fi
50+
51+
echo "DOCKER_TAGS=$TAGS" >> $GITHUB_ENV
52+
3053
- name: Log in to GitHub Container Registry
31-
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
54+
if: github.event_name == 'push' && github.ref == 'refs/heads/main' || github.event_name == 'release'
3255
uses: docker/login-action@v3
3356
with:
3457
registry: ghcr.io
3558
username: ${{ github.actor }}
3659
password: ${{ secrets.GITHUB_TOKEN }}
3760

3861
- name: Log in to Docker Hub
39-
if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == 'refs/heads/main'
62+
if: github.event_name == 'push' && github.ref == 'refs/heads/main' || github.event_name == 'release'
4063
uses: docker/login-action@v3
4164
with:
4265
registry: docker.io
@@ -49,23 +72,22 @@ jobs:
4972
with:
5073
context: .
5174
file: ./docker/Dockerfile
52-
push: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
75+
push: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' || github.event_name == 'release' }}
5376
cache-from: type=gha
5477
cache-to: type=gha,mode=max
55-
tags: |
56-
ghcr.io/obeone/multi-registry-cache:latest
57-
docker.io/obeoneorg/multi-registry-cache:latest
78+
tags: ${{ env.DOCKER_TAGS }}
5879
platforms: linux/amd64,linux/arm64,linux/arm/v8,linux/arm/v6,linux/arm/v7,linux/i386
5980

6081
- name: Set up cosign
61-
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
82+
if: github.event_name == 'push' && github.ref == 'refs/heads/main' || github.event_name == 'release'
6283
uses: sigstore/cosign-installer@v3
6384

6485
- name: Sign the container image with cosign
65-
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
86+
if: github.event_name == 'push' && github.ref == 'refs/heads/main' || github.event_name == 'release'
6687
run: |
67-
cosign sign --yes ghcr.io/obeone/multi-registry-cache@${DIGEST}
68-
cosign sign --yes docker.io/obeoneorg/multi-registry-cache@${DIGEST}
88+
for tag in $(echo $DOCKER_TAGS | tr ',' '\n'); do
89+
cosign sign --yes $tag@${DIGEST}
90+
done
6991
env:
7092
COSIGN_EXPERIMENTAL: true
7193
DIGEST: ${{ steps.docker_build.outputs.digest }}

0 commit comments

Comments
 (0)