Skip to content

Commit 682d79c

Browse files
committed
Refactor deployment script to improve readability and maintainability
1 parent 4948929 commit 682d79c

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

deploy.sh

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

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+
}
518

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+
}
828

9-
# Set debug flag from second argument, default to 0 if not set
29+
validate_input "${1}"
30+
IMAGE_TAG=$(get_image_tag "${1}")
1031
DEBUG=${2:-0}
32+
COMMIT_MESSAGE=$(git log --format=%B -n 1 HEAD)
1133

12-
# Check if the app deployment exists
1334
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")
1837
echo "Current v1 image tag: $CURRENT_V1_IMAGE_TAG"
1938
echo "Current v2 image tag: $CURRENT_V2_IMAGE_TAG"
20-
21-
# If the commit message starts with [v1], it's an update for v1
2239
if [[ "$COMMIT_MESSAGE" == \[v1\]* ]]; then
2340
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}"
2642
else
27-
# If the commit message does not start with [v1], it's an update for v2
2843
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}"
3145
fi
3246
else
33-
# If the app deployment does not exist
3447
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

Comments
 (0)