Skip to content

Commit 441b69e

Browse files
JohanDevlclaude
andcommitted
fix: restore semantic version tagging for Docker images
- Add back manual Git tag retrieval logic for branch pushes - Fix version detection for both tag pushes (direct) and branch pushes (latest tag) - Ensure semantic versions are properly applied to Docker images - Fix vulnerability scanning and testing to use correct version tags - Correct YAML syntax for push triggers (paths-ignore positioning) This resolves the issue where branch pushes to main weren't getting semantic version tags, only SHA-based tags. Now both scenarios work: - Tag push (v2.0.14) → uses tag directly - Branch push (main) → uses latest available Git tag 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 585ef56 commit 441b69e

File tree

1 file changed

+50
-19
lines changed

1 file changed

+50
-19
lines changed

.github/workflows/docker-build.yml

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ on:
44
push:
55
branches:
66
- develop
7-
tags:
8-
- 'v*'
97
paths-ignore:
108
- "**.md"
119
- "docs/**"
1210
- ".github/ISSUE_TEMPLATE/**"
11+
tags:
12+
- 'v*'
1313
pull_request:
1414
branches:
1515
- main
@@ -51,6 +51,27 @@ jobs:
5151
- name: Set up Docker Buildx
5252
uses: docker/setup-buildx-action@v3
5353

54+
- name: Get version info
55+
id: version
56+
run: |
57+
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
58+
# For tag pushes, use the tag directly
59+
VERSION="${{ github.ref_name }}"
60+
echo "version=$VERSION" >> $GITHUB_OUTPUT
61+
echo "is_tag=true" >> $GITHUB_OUTPUT
62+
echo "🏷️ Building from tag: $VERSION"
63+
else
64+
# For branch pushes, get the latest tag
65+
git fetch --tags
66+
LATEST_TAG=$(git tag -l "v*" | grep -v "-" | sort -V | tail -n 1)
67+
if [ -z "$LATEST_TAG" ]; then
68+
LATEST_TAG="v1.0.0"
69+
fi
70+
echo "version=$LATEST_TAG" >> $GITHUB_OUTPUT
71+
echo "is_tag=false" >> $GITHUB_OUTPUT
72+
echo "📋 Building from branch, using latest tag: $LATEST_TAG"
73+
fi
74+
5475
- name: Extract metadata for Docker
5576
id: meta
5677
uses: docker/metadata-action@v5
@@ -59,14 +80,15 @@ jobs:
5980
${{ env.REGISTRY_IMAGE }}
6081
${{ env.GITHUB_IMAGE }}
6182
tags: |
62-
# Main branch tags (only via workflow_dispatch from auto-tag)
63-
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}
64-
type=raw,value=main,enable=${{ github.ref == 'refs/heads/main' }}
83+
# Main branch tags (manual or tag-triggered)
84+
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/') }}
85+
type=raw,value=main,enable=${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/') }}
86+
type=raw,value=${{ steps.version.outputs.version }},enable=${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/') }}
6587
# Develop branch tag
6688
type=raw,value=develop,enable=${{ github.ref == 'refs/heads/develop' }}
6789
# PR tags
6890
type=ref,event=pr,prefix=PR-
69-
# Release/tag-based builds (semantic versioning)
91+
# Tag-based builds (semantic versioning from git tags)
7092
type=semver,pattern={{version}}
7193
type=semver,pattern={{major}}.{{minor}}
7294
@@ -99,15 +121,15 @@ jobs:
99121
cache-from: type=gha,scope=${{ github.workflow }}-${{ github.ref_name }}
100122
cache-to: type=gha,mode=max,scope=${{ github.workflow }}-${{ github.ref_name }}
101123
build-args: |
102-
VERSION=${{ steps.meta.outputs.version }}
124+
VERSION=${{ steps.version.outputs.version }}
103125
COMMIT_SHA=${{ github.sha }}
104126
BUILD_DATE=${{ steps.build_date.outputs.BUILD_DATE }}
105127
106128
- name: Scan image for vulnerabilities
107129
if: github.event_name != 'pull_request'
108130
uses: aquasecurity/trivy-action@master
109131
with:
110-
image-ref: ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}
132+
image-ref: ${{ env.REGISTRY_IMAGE }}:${{ steps.version.outputs.version }}
111133
format: "sarif"
112134
output: "trivy-results.sarif"
113135

@@ -136,18 +158,27 @@ jobs:
136158
username: ${{ secrets.DOCKERHUB_USERNAME }}
137159
password: ${{ secrets.DOCKERHUB_TOKEN }}
138160

139-
- name: Extract Docker metadata for testing
140-
id: meta
141-
uses: docker/metadata-action@v5
142-
with:
143-
images: ${{ env.REGISTRY_IMAGE }}
144-
tags: |
145-
type=semver,pattern={{version}}
146-
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}
147-
type=raw,value=develop,enable=${{ github.ref == 'refs/heads/develop' }}
161+
- name: Get version info for testing
162+
id: version
163+
run: |
164+
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
165+
# For tag pushes, use the tag directly
166+
VERSION="${{ github.ref_name }}"
167+
echo "version=$VERSION" >> $GITHUB_OUTPUT
168+
echo "🏷️ Testing tag version: $VERSION"
169+
else
170+
# For branch pushes, get the latest tag
171+
git fetch --tags
172+
LATEST_TAG=$(git tag -l "v*" | grep -v "-" | sort -V | tail -n 1)
173+
if [ -z "$LATEST_TAG" ]; then
174+
LATEST_TAG="v1.0.0"
175+
fi
176+
echo "version=$LATEST_TAG" >> $GITHUB_OUTPUT
177+
echo "📋 Testing branch version: $LATEST_TAG"
178+
fi
148179
149180
- name: Pull image for testing
150-
run: docker pull ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}
181+
run: docker pull ${{ env.REGISTRY_IMAGE }}:${{ steps.version.outputs.version }}
151182

152183
- name: Test Docker image
153184
run: |
@@ -159,7 +190,7 @@ jobs:
159190
-v $(pwd)/test_config:/app/config \
160191
-v $(pwd)/test_logs:/app/logs \
161192
-v $(pwd)/test_exports:/app/exports \
162-
${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }} --help
193+
${{ env.REGISTRY_IMAGE }}:${{ steps.version.outputs.version }} --help
163194
164195
echo "Docker image tests passed successfully"
165196

0 commit comments

Comments
 (0)