Skip to content

Commit 44934d4

Browse files
committed
Update deployment scripts and workflows
1 parent 23c846b commit 44934d4

File tree

3 files changed

+58
-46
lines changed

3 files changed

+58
-46
lines changed

.github/workflows/cd.yml

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ on:
1616
image_tag:
1717
required: true
1818
type: string
19+
debug:
20+
required: false
21+
type: string
22+
default: '0'
1923
env:
2024
# AZURE_CONTAINER_REGISTRY: "your-azure-container-registry"
2125
# CONTAINER_NAME: "your-container-name"
@@ -88,48 +92,7 @@ jobs:
8892
use-kubelogin: 'true'
8993

9094
- name: Install Helm
91-
run: |
92-
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
93-
chmod 700 get_helm.sh
94-
./get_helm.sh
95+
uses: azure/setup-helm@v1
9596

9697
- name: Deploy to Kubernetes [PRODUCTION ENVIRONMENT]
97-
run: |
98-
# Get the commit message of the commit that triggered the workflow
99-
COMMIT_MESSAGE=$(git log --format=%B -n 1 HEAD)
100-
101-
# Extract just the tagname from inputs.image_tag
102-
IMAGE_TAG=$(echo "${{ inputs.image_tag }}" | cut -d ":" -f 2)
103-
104-
# Check if the app deployment exists
105-
if kubectl get deployments app-release-my-chart-v1 && kubectl get deployments app-release-my-chart-v2; then
106-
# Get the current v1 and v2 image tags from the app deployment in the cluster
107-
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)
108-
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)
109-
110-
echo "Current v1 image tag: $CURRENT_V1_IMAGE_TAG"
111-
echo "Current v2 image tag: $CURRENT_V2_IMAGE_TAG"
112-
113-
# If the commit message starts with [v1], it's an update for v1
114-
if [[ "$COMMIT_MESSAGE" == \[v1\]* ]]; then
115-
echo "Update is for v1. Not setting v2.image.tag."
116-
# 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
117-
helm upgrade --install app-release ./my-chart --set v1.springAppContainer.image.tag=$IMAGE_TAG,v2.springAppContainer.image.tag=$CURRENT_V2_IMAGE_TAG
118-
else
119-
# If the commit message does not start with [v1], it's an update for v2
120-
echo "Update is for v2. Setting v2.image.tag to: $IMAGE_TAG."
121-
# Update the app and change the v2 image to IMAGE_TAG
122-
helm upgrade --install app-release ./my-chart --set v2.springAppContainer.image.tag=$IMAGE_TAG,v1.springAppContainer.image.tag=$CURRENT_V1_IMAGE_TAG
123-
fi
124-
else
125-
# If the app deployment does not exist
126-
echo "App deployment does not exist. Setting v2.image.tag to: $IMAGE_TAG."
127-
# 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
128-
helm upgrade --install app-release ./my-chart --set v2.springAppContainer.image.tag=$IMAGE_TAG
129-
fi
130-
131-
# Wait for 30 seconds
132-
sleep 30
133-
134-
# Get the status of the deployments
135-
kubectl get deployments
98+
run: ./deploy.sh "${{ inputs.image_tag }}" "${{ inputs.debug}}"

.github/workflows/ci.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ on:
55
# manual trigger
66
workflow_dispatch:
77
inputs:
8-
debug_enabled:
8+
ssh_debug_enabled:
99
type: boolean
10-
description: 'Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)'
10+
description: 'Run the build/test with ssh debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)'
11+
required: false
12+
default: false
13+
debug_deployment:
14+
type: boolean
15+
description: 'Run the pipeline with debug deployment enabled'
1116
required: false
1217
default: false
1318

@@ -181,7 +186,7 @@ jobs:
181186
182187
- name: Setup tmate session
183188
uses: mxschmitt/action-tmate@v3
184-
if: ${{ github.event_name == 'workflow_dispatch' && inputs.debug_enabled }}
189+
if: ${{ github.event_name == 'workflow_dispatch' && inputs.ssh_debug_enabled }}
185190

186191
# split-tests action - splits the tests into x number of groups
187192
# based on the total number of github-hosted runners and junit previous test results by time and line count.
@@ -300,4 +305,5 @@ jobs:
300305
with:
301306
# with tag from the build-and-publish-docker-image job in the output_tags step
302307
image_tag: "${{ needs.build-and-publish-docker-image.outputs.image_tag }}"
308+
debug: "${{ github.event.inputs.debug_deployment }}"
303309
secrets: inherit

deploy.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
3+
# Get the commit message of the commit that triggered the workflow
4+
COMMIT_MESSAGE=$(git log --format=%B -n 1 HEAD)
5+
6+
# Extract just the tagname from inputs.image_tag
7+
IMAGE_TAG=$(echo "${1}" | cut -d ":" -f 2)
8+
9+
# Set debug flag from second argument, default to 0 if not set
10+
DEBUG=${2:-0}
11+
12+
# Check if the app deployment exists
13+
if kubectl get deployments app-release-my-chart-v1 && kubectl get deployments app-release-my-chart-v2; 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+
18+
echo "Current v1 image tag: $CURRENT_V1_IMAGE_TAG"
19+
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+
if [[ "$COMMIT_MESSAGE" == \[v1\]* ]]; then
23+
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)
26+
else
27+
# If the commit message does not start with [v1], it's an update for v2
28+
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)
31+
fi
32+
else
33+
# If the app deployment does not exist
34+
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

0 commit comments

Comments
 (0)