---
name: Stop Staging Slots

on:
  workflow_dispatch:
    inputs: {}


jobs:
  stop-slots:
    name: Stop Slots
    runs-on: ubuntu-20.04
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: Api
          - name: Admin
          - name: Billing
          - name: Events
          - name: Sso
          - name: Identity
    steps:
      - name: Setup
        id: setup
        run: |
          NAME_LOWER=$(echo "${{ matrix.name }}" | awk '{print tolower($0)}')
          echo "Matrix name: ${{ matrix.name }}"
          echo "NAME_LOWER: $NAME_LOWER"
          echo "::set-output name=name_lower::$NAME_LOWER"

      - name: Login to Azure
        uses: Azure/login@1f63701bf3e6892515f1b7ce2d2bf1708b46beaf
        with:
          creds: ${{ secrets.AZURE_PROD_KV_CREDENTIALS }}

      - name: Retrieve secrets
        id: retrieve-secrets
        env:
          VAULT_NAME: "bitwarden-prod-kv"
        run: |
          webapp_name=$(
            az keyvault secret show --vault-name $VAULT_NAME \
              --name appservices-${{ steps.setup.outputs.name_lower }}-webapp-name \
              --query value --output tsv
          )
          echo "::add-mask::$webapp_name"
          echo "::set-output name=webapp-name::$webapp_name"

      - name: Stop staging slot
        env:
          SERVICE: ${{ matrix.name }}
          WEBAPP_NAME: ${{ steps.retrieve-secrets.outputs.webapp-name }}
        run: |
          if [[ "$SERVICE" = "Api" ]] || [[ "$SERVICE" = "Identity" ]]; then
            RESOURCE_GROUP=bitwardenappservices
          else
            RESOURCE_GROUP=bitwarden
          fi
          az webapp stop -n $WEBAPP_NAME -g $RESOURCE_GROUP -s staging
