1
1
#! /bin/bash
2
2
set -e
3
3
4
+ # Validate the input to ensure that the image tag is in the correct format
4
5
function validate_input() {
5
6
if [[ ! " ${1} " =~ ^.* :.* $ ]]; then
6
7
echo " Invalid image tag. Expected format: repo:tag"
7
8
exit 1
8
9
fi
9
10
}
10
11
12
+ # Get the image tag from the specified image
11
13
function get_image_tag() {
12
- echo " ${1} " | cut -d " :" -f 2
14
+ echo " ${1} " | rev | cut -d " :" -f 1 | rev
13
15
}
14
16
17
+ # Get the current image tag for the specified container
15
18
function get_current_image_tag() {
16
19
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
20
}
18
21
19
22
function upgrade_release() {
23
+ # The first argument is the debug flag
24
+ # The second argument is the v1 image tag
25
+ # The third argument is the v2 image tag
20
26
local debug_flag=" "
27
+ # If the debug flag is set, enable verbose output for debugging
21
28
if [ " ${1} " -eq 1 ]; then
22
29
debug_flag=" --debug"
23
30
fi
24
- helm upgrade --install --atomic app-release ./my-chart --set v1.springAppContainer.image.tag=${2} ,v2.springAppContainer.image.tag=${3} ${debug_flag}
31
+ # If the v1 or v2 image tag is set, set the flag for the helm upgrade command
32
+ local set_flag=" "
33
+ if [ -n " ${2} " ]; then
34
+ set_flag=" v1.springAppContainer.image.tag=${2} "
35
+ fi
36
+ # If the v2 image tag is set, append it to the set flag for the helm upgrade command with a comma separator
37
+ if [ -n " ${3} " ]; then
38
+ if [ -n " ${set_flag} " ]; then
39
+ set_flag=" ${set_flag} ,"
40
+ fi
41
+ # Append the v2 image tag to the set flag
42
+ set_flag=" ${set_flag} v2.springAppContainer.image.tag=${3} "
43
+ fi
44
+ # If the set flag is set, append the flag to the helm upgrade command with the --set flag
45
+ if [ -n " ${set_flag} " ]; then
46
+ set_flag=" --set ${set_flag} "
47
+ fi
48
+ # Deploy the release with the new image tag and wait for the rollout to finish before exiting the script with a success status
49
+ # If the deployment is not ready within the timeout, the script will exit with an error status and the deployment will be marked as failed
50
+ # A rollback will be triggered by the helm controller if the deployment fails
51
+ # The --atomic flag ensures that the deployment will be rolled back if the upgrade fails
52
+ # The --install flag ensures that the release will be installed if it does not exist
53
+ # The --debug flag enables verbose output for debugging
54
+ # The --set flag sets the specified values on the command line
55
+ helm upgrade --install --atomic app-release ./my-chart ${set_flag} ${debug_flag}
25
56
kubectl rollout status deployment/app-release-my-chart-v1
26
57
kubectl rollout status deployment/app-release-my-chart-v2
27
58
}
@@ -31,19 +62,23 @@ IMAGE_TAG=$(get_image_tag "${1}")
31
62
DEBUG=${2:- 0}
32
63
COMMIT_MESSAGE=$( git log --format=%B -n 1 HEAD)
33
64
65
+ # If the app deployment exists, get the current image tags for v1 and v2 and upgrade the release with the new image tag
34
66
if kubectl get deployments app-release-my-chart-v1 2> /dev/null && kubectl get deployments app-release-my-chart-v2 2> /dev/null; then
35
67
CURRENT_V1_IMAGE_TAG=$( get_current_image_tag " v1" )
36
68
CURRENT_V2_IMAGE_TAG=$( get_current_image_tag " v2" )
37
69
echo " Current v1 image tag: $CURRENT_V1_IMAGE_TAG "
38
70
echo " Current v2 image tag: $CURRENT_V2_IMAGE_TAG "
71
+ # If the commit message contains [v1], upgrade the release with the new image tag for v1
39
72
if [[ " $COMMIT_MESSAGE " == \[ v1\] * ]]; then
40
73
echo " Update is for v1. Not setting v2.image.tag."
41
74
upgrade_release " ${DEBUG} " " ${IMAGE_TAG} " " ${CURRENT_V2_IMAGE_TAG} "
42
75
else
76
+ # If the commit message contains [v2], upgrade the release with the new image tag for v2
43
77
echo " Update is for v2. Setting v2.image.tag to: $IMAGE_TAG ."
44
78
upgrade_release " ${DEBUG} " " ${CURRENT_V1_IMAGE_TAG} " " ${IMAGE_TAG} "
45
79
fi
46
80
else
81
+ # If the app deployment does not exist, upgrade the release with the new image tag for v2
47
82
echo " App deployment does not exist. Setting v2.image.tag to: $IMAGE_TAG ."
48
83
upgrade_release " ${DEBUG} " " " " ${IMAGE_TAG} "
49
84
fi
0 commit comments