33on :
44 push :
55 branches : [main]
6+ tags : ["v*"]
67
78env :
89 DOCKER_IMAGE : moabdelazem/gitops-app
@@ -12,16 +13,35 @@ jobs:
1213 build-and-push :
1314 runs-on : ubuntu-latest
1415
16+ outputs :
17+ image_tag : ${{ steps.meta.outputs.IMAGE_TAG }}
18+ is_release : ${{ steps.meta.outputs.IS_RELEASE }}
19+
1520 steps :
1621 - name : Checkout
1722 uses : actions/checkout@v4
1823
1924 - name : Set build metadata
2025 id : meta
2126 run : |
22- echo "VERSION=${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
23- echo "GIT_COMMIT=${GITHUB_SHA::7}" >> $GITHUB_OUTPUT
24- echo "BUILD_TIME=$(date -u '+%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
27+ GIT_COMMIT=${GITHUB_SHA::7}
28+ BUILD_TIME=$(date -u '+%Y-%m-%dT%H:%M:%SZ')
29+
30+ if [[ "$GITHUB_REF" == refs/tags/v* ]]; then
31+ VERSION=${GITHUB_REF#refs/tags/}
32+ IMAGE_TAG=$VERSION
33+ IS_RELEASE=true
34+ else
35+ VERSION=$GIT_COMMIT
36+ IMAGE_TAG=$GIT_COMMIT
37+ IS_RELEASE=false
38+ fi
39+
40+ echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
41+ echo "GIT_COMMIT=$GIT_COMMIT" >> $GITHUB_OUTPUT
42+ echo "BUILD_TIME=$BUILD_TIME" >> $GITHUB_OUTPUT
43+ echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_OUTPUT
44+ echo "IS_RELEASE=$IS_RELEASE" >> $GITHUB_OUTPUT
2545
2646 - name : Login to Docker Hub
2747 uses : docker/login-action@v3
3959 GIT_COMMIT=${{ steps.meta.outputs.GIT_COMMIT }}
4060 BUILD_TIME=${{ steps.meta.outputs.BUILD_TIME }}
4161 tags : |
42- ${{ env.DOCKER_IMAGE }}:${{ steps.meta.outputs.VERSION }}
62+ ${{ env.DOCKER_IMAGE }}:${{ steps.meta.outputs.IMAGE_TAG }}
4363 ${{ env.DOCKER_IMAGE }}:latest
4464
4565 update-manifests :
@@ -53,15 +73,22 @@ jobs:
5373 repository : ${{ env.MANIFESTS_REPO }}
5474 token : ${{ secrets.MANIFESTS_PAT }}
5575
56- - name : Update dev overlay image tag
76+ - name : Update dev overlay
77+ if : needs.build-and-push.outputs.is_release == 'false'
5778 run : |
5879 cd overlays/dev
59- sed -i "s|newTag:.*|newTag: ${GITHUB_SHA::7}|" kustomization.yaml
80+ sed -i "s|newTag:.*|newTag: ${{ needs.build-and-push.outputs.image_tag }}|" kustomization.yaml
81+
82+ - name : Update prod overlay
83+ if : needs.build-and-push.outputs.is_release == 'true'
84+ run : |
85+ cd overlays/prod
86+ sed -i "s|newTag:.*|newTag: ${{ needs.build-and-push.outputs.image_tag }}|" kustomization.yaml
6087
6188 - name : Commit and push
6289 run : |
6390 git config user.name "github-actions"
6491 git config user.email "github-actions@github.com"
6592 git add .
66- git commit -m "chore: update dev image to ${GITHUB_SHA::7 }"
93+ git commit -m "chore: update image to ${{ needs.build-and-push.outputs.image_tag } }"
6794 git push
0 commit comments