Skip to content

Commit 6255b6e

Browse files
committed
Add image tag validation and upgrade release function
1 parent 682d79c commit 6255b6e

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

deploy.sh

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,58 @@
11
#!/bin/bash
22
set -e
33

4+
# Validate the input to ensure that the image tag is in the correct format
45
function validate_input() {
56
if [[ ! "${1}" =~ ^.*:.*$ ]]; then
67
echo "Invalid image tag. Expected format: repo:tag"
78
exit 1
89
fi
910
}
1011

12+
# Get the image tag from the specified image
1113
function get_image_tag() {
12-
echo "${1}" | cut -d ":" -f 2
14+
echo "${1}" | rev | cut -d ":" -f 1 | rev
1315
}
1416

17+
# Get the current image tag for the specified container
1518
function get_current_image_tag() {
1619
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
1720
}
1821

1922
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
2026
local debug_flag=""
27+
# If the debug flag is set, enable verbose output for debugging
2128
if [ "${1}" -eq 1 ]; then
2229
debug_flag="--debug"
2330
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}
2556
kubectl rollout status deployment/app-release-my-chart-v1
2657
kubectl rollout status deployment/app-release-my-chart-v2
2758
}
@@ -31,19 +62,23 @@ IMAGE_TAG=$(get_image_tag "${1}")
3162
DEBUG=${2:-0}
3263
COMMIT_MESSAGE=$(git log --format=%B -n 1 HEAD)
3364

65+
# If the app deployment exists, get the current image tags for v1 and v2 and upgrade the release with the new image tag
3466
if kubectl get deployments app-release-my-chart-v1 2>/dev/null && kubectl get deployments app-release-my-chart-v2 2>/dev/null; then
3567
CURRENT_V1_IMAGE_TAG=$(get_current_image_tag "v1")
3668
CURRENT_V2_IMAGE_TAG=$(get_current_image_tag "v2")
3769
echo "Current v1 image tag: $CURRENT_V1_IMAGE_TAG"
3870
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
3972
if [[ "$COMMIT_MESSAGE" == \[v1\]* ]]; then
4073
echo "Update is for v1. Not setting v2.image.tag."
4174
upgrade_release "${DEBUG}" "${IMAGE_TAG}" "${CURRENT_V2_IMAGE_TAG}"
4275
else
76+
# If the commit message contains [v2], upgrade the release with the new image tag for v2
4377
echo "Update is for v2. Setting v2.image.tag to: $IMAGE_TAG."
4478
upgrade_release "${DEBUG}" "${CURRENT_V1_IMAGE_TAG}" "${IMAGE_TAG}"
4579
fi
4680
else
81+
# If the app deployment does not exist, upgrade the release with the new image tag for v2
4782
echo "App deployment does not exist. Setting v2.image.tag to: $IMAGE_TAG."
4883
upgrade_release "${DEBUG}" "" "${IMAGE_TAG}"
4984
fi

0 commit comments

Comments
 (0)