From e2a5ce897630121488722deb99b9125fc59972cd Mon Sep 17 00:00:00 2001 From: Leo Huang Date: Wed, 20 Nov 2024 11:31:57 -0800 Subject: [PATCH 1/7] n deployment limit added to cd-dev.yaml --- .github/workflows/cd-dev.yaml | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/.github/workflows/cd-dev.yaml b/.github/workflows/cd-dev.yaml index 0f5afc70b..3d9c4e693 100644 --- a/.github/workflows/cd-dev.yaml +++ b/.github/workflows/cd-dev.yaml @@ -38,6 +38,41 @@ jobs: chart_ver: 0.1.0-dev.${{ needs.compute-sha.outputs.sha_short }} secrets: inherit + limit-deploy: + name: SSH and Limit Deployments + needs: [build-push] + 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=$(kubectl get deployments \ + --namespace=bt \ + --sort-by='.metadata.creationTimestamp' \ + -o custom-columns=":metadata.name,:metadata.creationTimestamp" | grep '^bt-dev-app') + 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] From 7eaac172fe379393aa6f98c97d82d2dfd8628d90 Mon Sep 17 00:00:00 2001 From: Leo Huang Date: Wed, 20 Nov 2024 11:50:14 -0800 Subject: [PATCH 2/7] remove dependency on build-push --- .github/workflows/cd-dev.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/cd-dev.yaml b/.github/workflows/cd-dev.yaml index 3d9c4e693..f589f1ade 100644 --- a/.github/workflows/cd-dev.yaml +++ b/.github/workflows/cd-dev.yaml @@ -40,7 +40,6 @@ jobs: limit-deploy: name: SSH and Limit Deployments - needs: [build-push] runs-on: ubuntu-latest steps: - name: SSH and Check Deployments From ea9ab376aaa8d012bfe4a48344996b9a13446f81 Mon Sep 17 00:00:00 2001 From: Leo Huang Date: Wed, 20 Nov 2024 12:05:40 -0800 Subject: [PATCH 3/7] attempt no deployment fix --- .github/workflows/cd-dev.yaml | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cd-dev.yaml b/.github/workflows/cd-dev.yaml index f589f1ade..5a67923fc 100644 --- a/.github/workflows/cd-dev.yaml +++ b/.github/workflows/cd-dev.yaml @@ -56,8 +56,15 @@ jobs: --namespace=bt \ --sort-by='.metadata.creationTimestamp' \ -o custom-columns=":metadata.name,:metadata.creationTimestamp" | grep '^bt-dev-app') - deployment_count=$(echo "$deployments" | wc -l) - + + if [ -z "$deployments" ]; then + echo "No bt-dev-app deployments found." + deployment_count=0 + else + deployment_count=$(echo "$deployments" | wc -l) + echo "Deployment count: $deployment_count" + fi + # Check if deployment count > 16 (2 per deployment) if [ "$deployment_count" -gt 16 ]; then echo "Too many deployments. Deleting the oldest deployment." From 6d3ce6439ce0b73d24cb60bc9d7449938d20ca09 Mon Sep 17 00:00:00 2001 From: Leo Huang Date: Wed, 20 Nov 2024 12:12:31 -0800 Subject: [PATCH 4/7] debugging --- .github/workflows/cd-dev.yaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/cd-dev.yaml b/.github/workflows/cd-dev.yaml index 5a67923fc..b833d3541 100644 --- a/.github/workflows/cd-dev.yaml +++ b/.github/workflows/cd-dev.yaml @@ -51,19 +51,19 @@ jobs: script: | set -e # Exit immediately if a command fails + echo "hi1" # 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') - - if [ -z "$deployments" ]; then - echo "No bt-dev-app deployments found." - deployment_count=0 - else + + echo "hi" + deployment_count=0 + if [ -n "$deployments" ]; then deployment_count=$(echo "$deployments" | wc -l) - echo "Deployment count: $deployment_count" fi + echo "Deployment count: $deployment_count" # Check if deployment count > 16 (2 per deployment) if [ "$deployment_count" -gt 16 ]; then From 82ec4e6492153256a3d503bd584d67fb60e8040c Mon Sep 17 00:00:00 2001 From: Leo Huang Date: Wed, 20 Nov 2024 12:19:57 -0800 Subject: [PATCH 5/7] fixed grep returning 1 --- .github/workflows/cd-dev.yaml | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/.github/workflows/cd-dev.yaml b/.github/workflows/cd-dev.yaml index b833d3541..0c6429295 100644 --- a/.github/workflows/cd-dev.yaml +++ b/.github/workflows/cd-dev.yaml @@ -51,19 +51,12 @@ jobs: script: | set -e # Exit immediately if a command fails - echo "hi1" # 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') - - echo "hi" - deployment_count=0 - if [ -n "$deployments" ]; then - deployment_count=$(echo "$deployments" | wc -l) - fi - echo "Deployment count: $deployment_count" + -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 From ee4afe711ad523999f336de1c5c122fe317297e8 Mon Sep 17 00:00:00 2001 From: Leo Huang Date: Wed, 20 Nov 2024 15:41:56 -0800 Subject: [PATCH 6/7] Update to use helm list and helm uninstall --- .github/workflows/cd-dev.yaml | 63 +++++++++++++++++------------------ 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/.github/workflows/cd-dev.yaml b/.github/workflows/cd-dev.yaml index 0c6429295..80a781883 100644 --- a/.github/workflows/cd-dev.yaml +++ b/.github/workflows/cd-dev.yaml @@ -37,9 +37,32 @@ 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] + 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 limit-deploy: name: SSH and Limit Deployments + needs: [deploy] runs-on: ubuntu-latest steps: - name: SSH and Check Deployments @@ -52,44 +75,20 @@ jobs: set -e # Exit immediately if a command fails # Get bt-dev-app- deployments sorted by creation timestamp - deployments=$(kubectl get deployments \ + deployments=$(helm list -d \ --namespace=bt \ - --sort-by='.metadata.creationTimestamp' \ - -o custom-columns=":metadata.name,:metadata.creationTimestamp" | grep '^bt-dev-app') || true + -o json | jq -r '.[] | [.name, .updated] | @tsv' | 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 + + # 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 | 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 + # Uninstall deployment + helm uninstall "${oldest_deployment}" else - echo "Deployment count is <= 16." + echo "Deployment count is <= 8." 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 From 2b230fd12452238f457dc29774dac8f98fb00ccf Mon Sep 17 00:00:00 2001 From: Leo Huang Date: Wed, 20 Nov 2024 15:55:23 -0800 Subject: [PATCH 7/7] simplify --- .github/workflows/cd-dev.yaml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/cd-dev.yaml b/.github/workflows/cd-dev.yaml index 80a781883..6c33f5c8e 100644 --- a/.github/workflows/cd-dev.yaml +++ b/.github/workflows/cd-dev.yaml @@ -75,9 +75,10 @@ jobs: set -e # Exit immediately if a command fails # Get bt-dev-app- deployments sorted by creation timestamp - deployments=$(helm list -d \ + deployments=$(helm list \ --namespace=bt \ - -o json | jq -r '.[] | [.name, .updated] | @tsv' | grep '^bt-dev-app') || true + --date \ + --short | grep '^bt-dev-app') || true deployment_count=$(echo "$deployments" | wc -l) # Check if deployment count > 8 @@ -85,7 +86,7 @@ jobs: 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\}') + oldest_deployment=$(echo "$deployments" | head -n 1) # Uninstall deployment helm uninstall "${oldest_deployment}"