@@ -96,33 +96,37 @@ jobs:
96
96
- name : Deploy to Kubernetes [PRODUCTION ENVIRONMENT]
97
97
run : |
98
98
# Get the commit message of the commit that triggered the workflow
99
- COMMIT_MESSAGE=$(git log --format=%B -n 1 ${{ github.event.after }} )
100
-
99
+ COMMIT_MESSAGE=$(git log --format=%B -n 1 HEAD )
100
+
101
101
# Check if the app deployment exists
102
- if kubectl get deployments app-release; then
102
+ if kubectl get deployments app-release-my-chart-v1 && kubectl get deployments app-release-my-chart-v2; then
103
+ # Get the current v1 and v2 image tags from the app deployment in the cluster
104
+ CURRENT_V1_IMAGE_TAG=$(kubectl get pods -l app=spring-app -o json | jq -r '.items[] | select(.metadata.name | contains("v1")) | .spec.containers[0].image' | cut -d ":" -f 2 | head -n 1)
105
+ CURRENT_V2_IMAGE_TAG=$(kubectl get pods -l app=spring-app -o json | jq -r '.items[] | select(.metadata.name | contains("v2")) | .spec.containers[0].image' | cut -d ":" -f 2 | head -n 1)
106
+
107
+ echo "Current v1 image tag: $CURRENT_V1_IMAGE_TAG"
108
+ echo "Current v2 image tag: $CURRENT_V2_IMAGE_TAG"
109
+
103
110
# If the commit message starts with [v1], it's an update for v1
104
111
if [[ "$COMMIT_MESSAGE" == \[v1\]* ]]; then
105
112
echo "Update is for v1. Not setting v2.image.tag."
106
- # Fetch the current v2 image tag from the running pods - this is to ensure the v2.image.tag is not changed from the values.yaml file
107
- CURRENT_V2_IMAGE_TAG=$(kubectl get pods -l app=spring-app -o jsonpath="{.items[0].spec.containers[0].image}" | cut -d ":" -f 2)
108
113
# Update the app without changing the v2 image but instead change the v1 image to inputs.image_tag ensure the v2.image.tag is not changed from the values.yaml file
109
114
helm upgrade --install app-release ./my-chart --set v1.image.tag=${{ inputs.image_tag }},v2.image.tag=$CURRENT_V2_IMAGE_TAG
110
-
111
115
else
112
116
# If the commit message does not start with [v1], it's an update for v2
113
117
echo "Update is for v2. Setting v2.image.tag to: ${{ inputs.image_tag }}."
114
118
# Update the app and change the v2 image to inputs.image_tag
115
- helm upgrade --install app-release ./my-chart --set v2.image.tag=${{ inputs.image_tag }}
119
+ helm upgrade --install app-release ./my-chart --set v2.image.tag=${{ inputs.image_tag }},v1.image.tag=$CURRENT_V1_IMAGE_TAG
116
120
fi
117
121
else
118
122
# If the app deployment does not exist
119
123
echo "App deployment does not exist. Setting v2.image.tag to: ${{ inputs.image_tag }}."
120
- # Set v2.image.tag to inputs.image_tag regardless of the commit message
124
+ # Set v2.image.tag to inputs.image_tag as a new deployment starting point and leave v1.image.tag as the default value in the values.yaml file
121
125
helm upgrade --install app-release ./my-chart --set v2.image.tag=${{ inputs.image_tag }}
122
126
fi
123
-
127
+
124
128
# Wait for 30 seconds
125
129
sleep 30
126
-
130
+
127
131
# Get the status of the deployments
128
132
kubectl get deployments
0 commit comments