Deploy to Development #130
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy to Development | |
concurrency: | |
group: dev-${{ github.ref }} | |
cancel-in-progress: true | |
on: | |
workflow_dispatch: | |
inputs: | |
ttl: | |
description: "Deployment time to live in hours" | |
required: true | |
type: number | |
default: 24 | |
jobs: | |
compute-sha: | |
name: Compute sha_short | |
runs-on: ubuntu-latest | |
outputs: | |
sha_short: ${{ steps.vars.outputs.sha_short }} | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Set vars | |
id: vars | |
run: | | |
echo "sha_short=$(git rev-parse --short ${{ github.sha }})" >> $GITHUB_OUTPUT | |
build-push: | |
name: Build and Push Images and Charts | |
needs: [compute-sha] | |
uses: ./.github/workflows/cd.yaml | |
with: | |
image_tag: ${{ needs.compute-sha.outputs.sha_short }} | |
chart_ver: 0.1.0-dev.${{ needs.compute-sha.outputs.sha_short }} | |
secrets: inherit | |
limit-deploy: | |
name: SSH and Limit Deployments | |
runs-on: ubuntu-latest | |
steps: | |
- name: SSH and Check Deployments | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
key: ${{ secrets.SSH_KEY }} | |
script: | | |
set -e # Exit immediately if a command fails | |
# Get bt-dev-app- deployments sorted by creation timestamp | |
deployments=$(kubectl get deployments \ | |
--namespace=bt \ | |
--sort-by='.metadata.creationTimestamp' \ | |
-o custom-columns=":metadata.name,:metadata.creationTimestamp" | grep '^bt-dev-app') || true | |
deployment_count=$(echo "$deployments" | wc -l) | |
# Check if deployment count > 16 (2 per deployment) | |
if [ "$deployment_count" -gt 16 ]; then | |
echo "Too many deployments. Deleting the oldest deployment." | |
# Get oldest deployment from first line of deployments | |
oldest_deployment=$(echo "$deployments" | head -n 1 | grep -o '^bt-dev-app-[a-f0-9]\{7\}') | |
# Delete deployment | |
kubectl delete deployment "${oldest_deployment}-frontend" --namespace=bt | |
kubectl delete deployment "${oldest_deployment}-backend" --namespace=bt | |
else | |
echo "Deployment count is <= 16." | |
fi | |
deploy: | |
name: SSH and Deploy | |
needs: [compute-sha, build-push] | |
uses: ./.github/workflows/deploy.yaml | |
with: | |
environment: development | |
name: bt-dev-app-${{ needs.compute-sha.outputs.sha_short }} | |
version: 0.1.0-dev.${{ needs.compute-sha.outputs.sha_short }} | |
values: | | |
env: dev | |
ttl: ${{ inputs.ttl }} | |
frontend: | |
image: | |
tag: ${{ needs.compute-sha.outputs.sha_short }} | |
backend: | |
image: | |
tag: ${{ needs.compute-sha.outputs.sha_short }} | |
host: ${{ needs.compute-sha.outputs.sha_short }}.dev.stanfurdtime.com | |
mongoUri: mongodb://bt-dev-mongo-mongodb.bt.svc.cluster.local:27017/bt | |
redisUri: redis://bt-dev-redis-master.bt.svc.cluster.local:6379 | |
secrets: inherit |