name: Cleanup

on:
  pull_request:
    branches: [master]
    types: [closed]

jobs:
  cleanup_staging_deploy:
    name: Cleanup Staging Deploy
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Setup gcloud CLI
      uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
      with:
        version: '285.0.0'
        project_id: ${{ secrets.GCP_PROJECT_ID }}
        service_account_email: ${{ secrets.GCP_SA_EMAIL }}
        service_account_key: ${{ secrets.GCP_SA_KEY }}
        export_default_credentials: true

    - name: Setup deployment parameters
      id: deployment_params
      run: |
        pr_number=$(jq -r .pull_request.number "$GITHUB_EVENT_PATH")
        echo "::set-output name=version::pr${pr_number}"

    - name: Remove deployment
      run: gcloud app versions delete --project="${{ secrets.GCP_PROJECT_ID }}" "${{ steps.deployment_params.outputs.version }}"
