11name : Docker Build and Publish
22
33on :
4- release :
5- types : [published]
64 push :
75 branches :
8- - main
96 - develop
7+ tags :
8+ - ' v*'
109 paths-ignore :
1110 - " **.md"
1211 - " docs/**"
1918 - " **.md"
2019 - " docs/**"
2120 - " .github/ISSUE_TEMPLATE/**"
21+ release :
22+ types : [published]
2223 workflow_dispatch :
2324 inputs :
2425 reason :
5051 - name : Set up Docker Buildx
5152 uses : docker/setup-buildx-action@v3
5253
53- - name : Get latest version tag
54- id : version
55- run : |
56- # Fetch all tags
57- git fetch --tags
58-
59- # Get the latest version tag
60- LATEST_TAG=$(git tag -l "v*" | grep -v "-" | sort -V | tail -n 1)
61-
62- # If no tag exists, default to v1.0.0
63- if [ -z "$LATEST_TAG" ]; then
64- LATEST_TAG="v1.0.0"
65- fi
66-
67- echo "latest_tag=$LATEST_TAG" >> $GITHUB_OUTPUT
68- echo "version=${LATEST_TAG#v}" >> $GITHUB_OUTPUT
69-
70- echo "📋 Latest version tag: $LATEST_TAG"
71-
7254 - name : Extract metadata for Docker
7355 id : meta
7456 uses : docker/metadata-action@v5
@@ -77,15 +59,14 @@ jobs:
7759 ${{ env.REGISTRY_IMAGE }}
7860 ${{ env.GITHUB_IMAGE }}
7961 tags : |
80- # Main branch tags - use latest available tag
62+ # Main branch tags (only via workflow_dispatch from auto- tag)
8163 type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }}
8264 type=raw,value=main,enable=${{ github.ref == 'refs/heads/main' }}
83- type=raw,value=${{ steps.version.outputs.latest_tag }},enable=${{ github.ref == 'refs/heads/main' }}
8465 # Develop branch tag
8566 type=raw,value=develop,enable=${{ github.ref == 'refs/heads/develop' }}
8667 # PR tags
8768 type=ref,event=pr,prefix=PR-
88- # Release tags (for releases triggered by auto-tag )
69+ # Release/tag-based builds (semantic versioning )
8970 type=semver,pattern={{version}}
9071 type=semver,pattern={{major}}.{{minor}}
9172
@@ -118,15 +99,15 @@ jobs:
11899 cache-from : type=gha,scope=${{ github.workflow }}-${{ github.ref_name }}
119100 cache-to : type=gha,mode=max,scope=${{ github.workflow }}-${{ github.ref_name }}
120101 build-args : |
121- VERSION=${{ github.ref == 'refs/heads/main' && steps.version.outputs.latest_tag || steps.meta.outputs.version }}
102+ VERSION=${{ steps.meta.outputs.version }}
122103 COMMIT_SHA=${{ github.sha }}
123104 BUILD_DATE=${{ steps.build_date.outputs.BUILD_DATE }}
124105
125106 - name : Scan image for vulnerabilities
126107 if : github.event_name != 'pull_request'
127108 uses : aquasecurity/trivy-action@master
128109 with :
129- image-ref : ${{ env.REGISTRY_IMAGE }}:${{ github.ref == 'refs/heads/main' && steps.version.outputs.latest_tag || steps.meta.outputs.version }}
110+ image-ref : ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}
130111 format : " sarif"
131112 output : " trivy-results.sarif"
132113
@@ -155,41 +136,18 @@ jobs:
155136 username : ${{ secrets.DOCKERHUB_USERNAME }}
156137 password : ${{ secrets.DOCKERHUB_TOKEN }}
157138
158- - name : Get latest version tag
159- id : version
160- run : |
161- # Fetch all tags
162- git fetch --tags
163-
164- # Get the latest version tag
165- LATEST_TAG=$(git tag -l "v*" | grep -v "-" | sort -V | tail -n 1)
166-
167- # If no tag exists, default to v1.0.0
168- if [ -z "$LATEST_TAG" ]; then
169- LATEST_TAG="v1.0.0"
170- fi
171-
172- echo "latest_tag=$LATEST_TAG" >> $GITHUB_OUTPUT
173-
174- - name : Extract Docker metadata
139+ - name : Extract Docker metadata for testing
175140 id : meta
176141 uses : docker/metadata-action@v5
177142 with :
178143 images : ${{ env.REGISTRY_IMAGE }}
179144 tags : |
180- type=sha,format=short
181-
182- - name : Determine test image tag
183- id : test_tag
184- run : |
185- if [ "${{ github.ref }}" == "refs/heads/main" ]; then
186- echo "tag=${{ steps.version.outputs.latest_tag }}" >> $GITHUB_OUTPUT
187- else
188- echo "tag=${{ steps.meta.outputs.version }}" >> $GITHUB_OUTPUT
189- fi
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' }}
190148
191149 - name : Pull image for testing
192- run : docker pull ${{ env.REGISTRY_IMAGE }}:${{ steps.test_tag .outputs.tag }}
150+ run : docker pull ${{ env.REGISTRY_IMAGE }}:${{ steps.meta .outputs.version }}
193151
194152 - name : Test Docker image
195153 run : |
@@ -201,7 +159,7 @@ jobs:
201159 -v $(pwd)/test_config:/app/config \
202160 -v $(pwd)/test_logs:/app/logs \
203161 -v $(pwd)/test_exports:/app/exports \
204- ${{ env.REGISTRY_IMAGE }}:${{ steps.test_tag .outputs.tag }} --help
162+ ${{ env.REGISTRY_IMAGE }}:${{ steps.meta .outputs.version }} --help
205163
206164 echo "Docker image tests passed successfully"
207165
0 commit comments