version: v1.0
name: Clean up cloud resources
agent:
  machine:
    type: f1-standard-2
    os_image: ubuntu2204

execution_time_limit:
  minutes: 10

blocks:
  - name: Clean up GCE resources
    dependencies: []
    task:
      prologue:
        commands:
          - checkout
          - export GOOGLE_APPLICATION_CREDENTIALS=$HOME/secrets/secret.google-service-account-key.json
          - export SHORT_WORKFLOW_ID=$(echo ${SEMAPHORE_WORKFLOW_ID} | sha256sum | cut -c -8)
          - export ZONE=europe-west3-c
          - export VM_PREFIX=sem-${SEMAPHORE_PROJECT_NAME}-${SHORT_WORKFLOW_ID}-
          - echo VM_PREFIX=${VM_PREFIX}
      jobs:
        - name: Clean up GCE instances
          commands:
            - ./.semaphore/vms/clean-up-vms ${VM_PREFIX}
      secrets:
        - name: google-service-account-for-gce

  - name: Clean up windows resources
    dependencies: []
    task:
      prologue:
        commands:
          - checkout
          - az login --service-principal -u "${AZ_SP_ID}" -p "${AZ_SP_PASSWORD}" --tenant "${AZ_TENANT_ID}" --output none
          - export AZURE_SUBSCRIPTION_ID=$AZ_SUBSCRIPTION_ID
          - export AZURE_TENANT_ID=$AZ_TENANT_ID
          - export AZURE_CLIENT_ID=$AZ_SP_ID
          - export AZURE_CLIENT_SECRET=$AZ_SP_PASSWORD
      jobs:
        - name: Clean up windows felix resources
          commands:
            - export RG_PREFIX=${USER}-win-felix-${SEMAPHORE_GIT_SHA:0:4}-pr${SEMAPHORE_GIT_PR_NUMBER}
            - export BACKEND=overlay
            - export AZURE_RESOURCE_GROUP=${RG_PREFIX}-${BACKEND}-rg
            - cd ~/calico/process/testing/aso && make dist-clean
        - name: Clean up windows cni resources
          commands:
            - export RG_PREFIX=${USER}-win-cni-${SEMAPHORE_GIT_SHA:0:4}-pr${SEMAPHORE_GIT_PR_NUMBER}
            - export AZURE_RESOURCE_GROUP=${RG_PREFIX}-overlay-rg
            - cd ~/calico/process/testing/aso && make dist-clean
            - export AZURE_RESOURCE_GROUP=${RG_PREFIX}-l2bridge-rg
            - cd ~/calico/process/testing/aso && make dist-clean
      secrets:
        - name: banzai-secrets
