Skip to content

Commit

Permalink
Update deployment scripts and workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
tsviz committed Feb 27, 2024
1 parent 23c846b commit 44934d4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 46 deletions.
49 changes: 6 additions & 43 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ on:
image_tag:
required: true
type: string
debug:
required: false
type: string
default: '0'
env:
# AZURE_CONTAINER_REGISTRY: "your-azure-container-registry"
# CONTAINER_NAME: "your-container-name"
Expand Down Expand Up @@ -88,48 +92,7 @@ jobs:
use-kubelogin: 'true'

- name: Install Helm
run: |
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.sh
uses: azure/setup-helm@v1

- name: Deploy to Kubernetes [PRODUCTION ENVIRONMENT]
run: |
# Get the commit message of the commit that triggered the workflow
COMMIT_MESSAGE=$(git log --format=%B -n 1 HEAD)
# Extract just the tagname from inputs.image_tag
IMAGE_TAG=$(echo "${{ inputs.image_tag }}" | cut -d ":" -f 2)
# Check if the app deployment exists
if kubectl get deployments app-release-my-chart-v1 && kubectl get deployments app-release-my-chart-v2; then
# Get the current v1 and v2 image tags from the app deployment in the cluster
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)
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)
echo "Current v1 image tag: $CURRENT_V1_IMAGE_TAG"
echo "Current v2 image tag: $CURRENT_V2_IMAGE_TAG"
# If the commit message starts with [v1], it's an update for v1
if [[ "$COMMIT_MESSAGE" == \[v1\]* ]]; then
echo "Update is for v1. Not setting v2.image.tag."
# 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
helm upgrade --install app-release ./my-chart --set v1.springAppContainer.image.tag=$IMAGE_TAG,v2.springAppContainer.image.tag=$CURRENT_V2_IMAGE_TAG
else
# If the commit message does not start with [v1], it's an update for v2
echo "Update is for v2. Setting v2.image.tag to: $IMAGE_TAG."
# Update the app and change the v2 image to IMAGE_TAG
helm upgrade --install app-release ./my-chart --set v2.springAppContainer.image.tag=$IMAGE_TAG,v1.springAppContainer.image.tag=$CURRENT_V1_IMAGE_TAG
fi
else
# If the app deployment does not exist
echo "App deployment does not exist. Setting v2.image.tag to: $IMAGE_TAG."
# 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
helm upgrade --install app-release ./my-chart --set v2.springAppContainer.image.tag=$IMAGE_TAG
fi
# Wait for 30 seconds
sleep 30
# Get the status of the deployments
kubectl get deployments
run: ./deploy.sh "${{ inputs.image_tag }}" "${{ inputs.debug}}"
12 changes: 9 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ on:
# manual trigger
workflow_dispatch:
inputs:
debug_enabled:
ssh_debug_enabled:
type: boolean
description: 'Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)'
description: 'Run the build/test with ssh debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)'
required: false
default: false
debug_deployment:
type: boolean
description: 'Run the pipeline with debug deployment enabled'
required: false
default: false

Expand Down Expand Up @@ -181,7 +186,7 @@ jobs:
- name: Setup tmate session
uses: mxschmitt/action-tmate@v3
if: ${{ github.event_name == 'workflow_dispatch' && inputs.debug_enabled }}
if: ${{ github.event_name == 'workflow_dispatch' && inputs.ssh_debug_enabled }}

# split-tests action - splits the tests into x number of groups
# based on the total number of github-hosted runners and junit previous test results by time and line count.
Expand Down Expand Up @@ -300,4 +305,5 @@ jobs:
with:
# with tag from the build-and-publish-docker-image job in the output_tags step
image_tag: "${{ needs.build-and-publish-docker-image.outputs.image_tag }}"
debug: "${{ github.event.inputs.debug_deployment }}"
secrets: inherit
43 changes: 43 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

# Get the commit message of the commit that triggered the workflow
COMMIT_MESSAGE=$(git log --format=%B -n 1 HEAD)

# Extract just the tagname from inputs.image_tag
IMAGE_TAG=$(echo "${1}" | cut -d ":" -f 2)

# Set debug flag from second argument, default to 0 if not set
DEBUG=${2:-0}

# Check if the app deployment exists
if kubectl get deployments app-release-my-chart-v1 && kubectl get deployments app-release-my-chart-v2; then
# Get the current v1 and v2 image tags from the app deployment in the cluster
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)
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)

echo "Current v1 image tag: $CURRENT_V1_IMAGE_TAG"
echo "Current v2 image tag: $CURRENT_V2_IMAGE_TAG"

# If the commit message starts with [v1], it's an update for v1
if [[ "$COMMIT_MESSAGE" == \[v1\]* ]]; then
echo "Update is for v1. Not setting v2.image.tag."
# 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
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)
else
# If the commit message does not start with [v1], it's an update for v2
echo "Update is for v2. Setting v2.image.tag to: $IMAGE_TAG."
# Update the app and change the v2 image to IMAGE_TAG
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)
fi
else
# If the app deployment does not exist
echo "App deployment does not exist. Setting v2.image.tag to: $IMAGE_TAG."
# 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
helm upgrade --install app-release ./my-chart --set v2.springAppContainer.image.tag=$IMAGE_TAG $(if [ "$DEBUG" -eq 1 ]; then echo "--debug"; fi)
fi

# Wait for 30 seconds
sleep 30

# Get the status of the deployments
kubectl get deployments

0 comments on commit 44934d4

Please sign in to comment.