1
1
#! /bin/bash
2
+ set -e
2
3
3
- # Get the commit message of the commit that triggered the workflow
4
- COMMIT_MESSAGE=$( git log --format=%B -n 1 HEAD)
4
+ function validate_input() {
5
+ if [[ ! " ${1} " =~ ^.* :.* $ ]]; then
6
+ echo " Invalid image tag. Expected format: repo:tag"
7
+ exit 1
8
+ fi
9
+ }
10
+
11
+ function get_image_tag() {
12
+ echo " ${1} " | cut -d " :" -f 2
13
+ }
14
+
15
+ function get_current_image_tag() {
16
+ kubectl get pods -l app=spring-app -o json | jq -r " .items[] | select(.metadata.name | contains(\" ${1} \" )) | .spec.containers[0].image" | cut -d " :" -f 2 | head -n 1
17
+ }
5
18
6
- # Extract just the tagname from inputs.image_tag
7
- IMAGE_TAG=$( echo " ${1} " | cut -d " :" -f 2)
19
+ function upgrade_release() {
20
+ local debug_flag=" "
21
+ if [ " ${1} " -eq 1 ]; then
22
+ debug_flag=" --debug"
23
+ fi
24
+ helm upgrade --install --atomic app-release ./my-chart --set v1.springAppContainer.image.tag=${2} ,v2.springAppContainer.image.tag=${3} ${debug_flag}
25
+ kubectl rollout status deployment/app-release-my-chart-v1
26
+ kubectl rollout status deployment/app-release-my-chart-v2
27
+ }
8
28
9
- # Set debug flag from second argument, default to 0 if not set
29
+ validate_input " ${1} "
30
+ IMAGE_TAG=$( get_image_tag " ${1} " )
10
31
DEBUG=${2:- 0}
32
+ COMMIT_MESSAGE=$( git log --format=%B -n 1 HEAD)
11
33
12
- # Check if the app deployment exists
13
34
if kubectl get deployments app-release-my-chart-v1 2> /dev/null && kubectl get deployments app-release-my-chart-v2 2> /dev/null; then
14
- # Get the current v1 and v2 image tags from the app deployment in the cluster
15
- 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)
16
- 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)
17
-
35
+ CURRENT_V1_IMAGE_TAG=$( get_current_image_tag " v1" )
36
+ CURRENT_V2_IMAGE_TAG=$( get_current_image_tag " v2" )
18
37
echo " Current v1 image tag: $CURRENT_V1_IMAGE_TAG "
19
38
echo " Current v2 image tag: $CURRENT_V2_IMAGE_TAG "
20
-
21
- # If the commit message starts with [v1], it's an update for v1
22
39
if [[ " $COMMIT_MESSAGE " == \[ v1\] * ]]; then
23
40
echo " Update is for v1. Not setting v2.image.tag."
24
- # Update the app without changing the v2 image but instead change the v1 image to IMAGE_TAG ensure the v2.image.tag is not changed from the values.yaml file
25
- helm upgrade --install app-release ./my-chart --set v1.springAppContainer.image.tag=$IMAGE_TAG ,v2.springAppContainer.image.tag=$CURRENT_V2_IMAGE_TAG $( if [ " $DEBUG " -eq 1 ]; then echo " --debug" ; fi)
41
+ upgrade_release " ${DEBUG} " " ${IMAGE_TAG} " " ${CURRENT_V2_IMAGE_TAG} "
26
42
else
27
- # If the commit message does not start with [v1], it's an update for v2
28
43
echo " Update is for v2. Setting v2.image.tag to: $IMAGE_TAG ."
29
- # Update the app and change the v2 image to IMAGE_TAG
30
- helm upgrade --install app-release ./my-chart --set v2.springAppContainer.image.tag=$IMAGE_TAG ,v1.springAppContainer.image.tag=$CURRENT_V1_IMAGE_TAG $( if [ " $DEBUG " -eq 1 ]; then echo " --debug" ; fi)
44
+ upgrade_release " ${DEBUG} " " ${CURRENT_V1_IMAGE_TAG} " " ${IMAGE_TAG} "
31
45
fi
32
46
else
33
- # If the app deployment does not exist
34
47
echo " App deployment does not exist. Setting v2.image.tag to: $IMAGE_TAG ."
35
- # Set v2.image.tag to IMAGE_TAG as a new deployment starting point and leave v1.image.tag as the default value in the values.yaml file
36
- helm upgrade --install app-release ./my-chart --set v2.springAppContainer.image.tag=$IMAGE_TAG $( if [ " $DEBUG " -eq 1 ]; then echo " --debug" ; fi)
37
- fi
38
-
39
- # Wait for 30 seconds
40
- sleep 30
41
-
42
- # Get the status of the deployments
43
- kubectl get deployments
48
+ upgrade_release " ${DEBUG} " " " " ${IMAGE_TAG} "
49
+ fi
0 commit comments