diff --git a/.github/workflows/cleanup-review-environment.yml b/.github/workflows/cleanup-review-environment.yml new file mode 100644 index 00000000..f9dc7e87 --- /dev/null +++ b/.github/workflows/cleanup-review-environment.yml @@ -0,0 +1,51 @@ +name: Destroy Review Environment on PR closure + +on: + repository_dispatch: + types: [cleanup-review-environment] + +concurrency: + group: cleanup-${{ github.event.client_payload.github-app }}-${{ github.event.client_payload.github-pr }} + cancel-in-progress: true + +jobs: + destroy-gitlab-environment: + name: Destroy GitLab Review Environment + runs-on: ubuntu-latest + environment: + name: dial-review + steps: + - name: Destroy review environment on PR merge + env: + APP: ${{ github.event.client_payload.github-app }} + PR_ID: ${{ github.event.client_payload.github-pr }} + GL_HOST: https://${{ vars.DEPLOY_HOST }} + DESTROY_TOKEN: ${{ secrets.DESTROY_ACCESS_TOKEN }} + GL_PROJECT_ID: ${{ vars.DEPLOY_PROJECT_ID }} + shell: bash + run: | + # Hack, pipeline to return the exit status of the last command to fail (instead of the last command). Exit immediately if any command returns a non-zero exit status. + set -euo pipefail + + # Get environment name to get gitlab environment ID + ENV_NAME="$APP/pr-$PR_ID" + + GL_API_URL="${GL_HOST}/api/v4/projects/${GL_PROJECT_ID}/environments" + curl_gl_api() { curl -sS --fail-with-body --retry 5 --retry-all-errors --retry-delay 3 \ + -H "PRIVATE-TOKEN: $DESTROY_TOKEN" "$@"; } + + ENV_ID=$(curl_gl_api --get --url-query "name=$ENV_NAME" "$GL_API_URL" | jq -r '.[0].id // empty') + [ -n "$ENV_ID" ] || { echo "::notice::No environment '$ENV_NAME'"; exit 0; } + + # Stop environment state and get current state + STATE=$(curl_gl_api -X POST "$GL_API_URL/$ENV_ID/stop" | jq -r '.state') + + # Check environment state + DEADLINE=$((SECONDS + 600)) + while [ "$STATE" = "stopping" ]; do + [ "$SECONDS" -lt "$DEADLINE" ] || { echo "::error::timed out in 'stopping'"; exit 1; } + echo "The environment is still stopping, checking again..." + sleep 15 + STATE=$(curl_gl_api "$GL_API_URL/$ENV_ID" | jq -r '.state') + done + [ "$STATE" = "stopped" ] && { echo "Environment ${ENV_NAME} is stopped"; exit 0; } || { echo "::error::unexpected state '$STATE'"; exit 1; } \ No newline at end of file