1313
1414 env :
1515 IMAGE_NAME : ${{ secrets.DOCKERHUB_USERNAME }}/springboot-postgres
16- IMAGE_TAG : ${{ needs.build-and-push.outputs.image-tag }}
16+
17+ outputs :
18+ image-tag : ${{ steps.set-tag.outputs.tag }}
1719
1820 steps :
1921 - name : Checkout code
@@ -38,57 +40,54 @@ jobs:
3840 username : ${{ secrets.DOCKERHUB_USERNAME }}
3941 password : ${{ secrets.DOCKERHUB_TOKEN }}
4042
41- - name : Build and Push Docker Image
43+ - name : Set Image Tag
44+ id : set-tag
4245 run : |
4346 COMMIT_SHA=$(echo "${{ github.sha }}" | cut -c1-7)
4447 BRANCH=$(echo "${GITHUB_REF#refs/heads/}" | tr '/' '-')
4548 TAG="${BRANCH}-${COMMIT_SHA}"
46-
47- docker build -t $IMAGE_NAME:$TAG ./app
48- docker push $IMAGE_NAME:$TAG
49- - name : Checkout code
50- uses : actions/checkout@v4
49+ echo "tag=$TAG" >> $GITHUB_OUTPUT
5150
52- - name : Update image tag in dev overlay
51+ - name : Build and Push Docker Image
5352 run : |
54- FILE="kustomize/overlays/dev/patch-deployment-image.yaml"
55-
56- echo "Updating $FILE with image: $IMAGE_NAME:$IMAGE_TAG"
57-
58- sed -i.bak -E "s|image:.*|image: ${IMAGE_NAME}:${IMAGE_TAG}|" "$FILE"
59-
53+ docker build -t $IMAGE_NAME:${{ steps.set-tag.outputs.tag }} ./app
54+ docker push $IMAGE_NAME:${{ steps.set-tag.outputs.tag }}
6055
6156 update-kustomize :
62- name : Update Kustomize Overlay
63- needs : build-and-push
64- runs-on : ubuntu-latest
57+ name : Update Kustomize Overlay
58+ needs : build-and-push
59+ runs-on : ubuntu-latest
6560
66- env :
67- IMAGE_NAME : ${{ secrets.DOCKERHUB_USERNAME }}/springboot-postgres
68- IMAGE_TAG : ${{ needs.build-and-push.outputs.image-tag }}
61+ env :
62+ IMAGE_NAME : ${{ secrets.DOCKERHUB_USERNAME }}/springboot-postgres
63+ IMAGE_TAG : ${{ needs.build-and-push.outputs.image-tag }}
6964
70- steps :
71- - name : Checkout code
72- uses : actions/checkout@v4
65+ steps :
66+ - name : Checkout code
67+ uses : actions/checkout@v4
68+
69+ - name : Configure git
70+ run : |
71+ git config --global user.name "${{ secrets.GIT_USER_NAME }}"
72+ git config --global user.email "${{ secrets.GIT_USER_EMAIL }}"
7373
74- - name : Configure git
75- run : |
76- git config --global user.name "${{ secrets.GIT_USER_NAME }}"
77- git config --global user.email "${{ secrets.GIT_USER_EMAIL }}"
74+ - name : Update image tag
75+ run : |
76+ if [ -z "$IMAGE_TAG" ]; then
77+ echo " IMAGE_TAG is empty, exiting."
78+ exit 1
79+ fi
7880
79- - name : Update image tag in dev overlay
80- run : |
81- FILE="kustomize/overlays/dev/patch-deployment-image.yaml"
81+ FILE="kustomize/overlays/dev/patch-deployment-image.yaml"
82+ echo "Updating $FILE with image: $IMAGE_NAME:$IMAGE_TAG"
8283
83- echo "Updating $FILE with image: $ IMAGE_NAME:$ IMAGE_TAG"
84+ sed -i.bak -E "s| image:.*|image: ${ IMAGE_NAME}:${ IMAGE_TAG}|" "$FILE "
8485
85- sed -i.bak -E "s|image:.*|image: ${IMAGE_NAME}:${IMAGE_TAG}|" "$FILE"
86+ git add "$FILE"
87+ git commit -m "Update image tag to $IMAGE_TAG"
88+ git push origin HEAD:auto/kustomize-update --force-with-lease
8689
87- git add "$FILE"
88- git commit -m "Update image tag to $IMAGE_TAG in kustomize overlay"
8990
90- git rebase --abort || true
91- git push origin HEAD:auto/kustomize-update --force-with-lease
9291
9392
9493
0 commit comments