From dc9c718669c759d828e8850b5e981ccec50883bf Mon Sep 17 00:00:00 2001 From: Leo Huang Date: Tue, 3 Dec 2024 19:20:49 -0800 Subject: [PATCH] CICID: Limit Dev Deployments (#746) * n deployment limit added to cd-dev.yaml * remove dependency on build-push * attempt no deployment fix * debugging * fixed grep returning 1 * Update to use helm list and helm uninstall * simplify --- .github/workflows/cd-dev.yaml | 36 ++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/.github/workflows/cd-dev.yaml b/.github/workflows/cd-dev.yaml index ebdf1d214..ead0c87f9 100644 --- a/.github/workflows/cd-dev.yaml +++ b/.github/workflows/cd-dev.yaml @@ -37,7 +37,7 @@ jobs: image_tag: ${{ needs.compute-sha.outputs.sha_short }} chart_ver: 0.1.0-dev.${{ needs.compute-sha.outputs.sha_short }} secrets: inherit - + deploy: name: SSH and Deploy needs: [compute-sha, build-push] @@ -59,3 +59,37 @@ jobs: 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 + + limit-deploy: + name: SSH and Limit Deployments + needs: [deploy] + runs-on: ubuntu-latest + steps: + - name: SSH and Check Deployments + uses: appleboy/ssh-action@v1.2.0 + 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=$(helm list \ + --namespace=bt \ + --date \ + --short | grep '^bt-dev-app') || true + deployment_count=$(echo "$deployments" | wc -l) + + # Check if deployment count > 8 + if [ "$deployment_count" -gt 8 ]; then + echo "Too many deployments. Deleting the oldest deployment." + + # Get oldest deployment from first line of deployments + oldest_deployment=$(echo "$deployments" | head -n 1) + + # Uninstall deployment + helm uninstall "${oldest_deployment}" + else + echo "Deployment count is <= 8." + fi