|
| 1 | +name: Ephemeral Staging Tear Down |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + schedule: |
| 6 | + - cron: '0 */4 * * *' |
| 7 | + |
| 8 | +permissions: |
| 9 | + id-token: write |
| 10 | + contents: read |
| 11 | + pull-requests: read |
| 12 | + |
| 13 | +jobs: |
| 14 | + destroy-pr: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + environment: staging |
| 17 | + steps: |
| 18 | + - name: "Checkout" |
| 19 | + uses: actions/checkout@v4 |
| 20 | + |
| 21 | + - name: "Authenticate with GCP" |
| 22 | + uses: google-github-actions/auth@v2 |
| 23 | + with: |
| 24 | + token_format: access_token |
| 25 | + workload_identity_provider: projects/687476608778/locations/global/workloadIdentityPools/github-oidc-activitypub/providers/github-provider-activitypub |
| 26 | + service_account: stg-activitypub-cicd-stg-envs@ghost-activitypub.iam.gserviceaccount.com |
| 27 | + |
| 28 | + - name: "Check Closed PRs Deployed" |
| 29 | + id: check-closed-prs |
| 30 | + env: |
| 31 | + GCP_PROJECT: ghost-activitypub |
| 32 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 33 | + run: | |
| 34 | + export destroy_prs="" |
| 35 | + for PR_NUMBER in $(gcloud run services list --project ${GCP_PROJECT} --format=json \ |
| 36 | + | jq -r '.[] | select(.metadata.name | test("stg-pr-\\d+-api")) | .metadata.name | capture("stg-pr-(?<num>\\d+)-api") | .num'); do |
| 37 | + PR_STATE=$(gh pr view $PR_NUMBER --json state | jq -r '.state') |
| 38 | + echo "PR $PR_NUMBER state is $PR_STATE." |
| 39 | + if [ "$PR_STATE" == "MERGED" ]; then |
| 40 | + echo "Deleting PR $PR_NUMBER environment." |
| 41 | + export destroy_prs="$destroy_prs $PR_NUMBER" |
| 42 | + fi |
| 43 | + done |
| 44 | + echo "destroy_prs=$destroy_prs" >> "$GITHUB_OUTPUT" |
| 45 | +
|
| 46 | + - name: "Checkout activitypub-infra repo" |
| 47 | + if: ${{ steps.check-closed-prs.outputs.destroy_prs != '' }} |
| 48 | + uses: actions/checkout@v4 |
| 49 | + with: |
| 50 | + repository: TryGhost/activitypub-infra |
| 51 | + ssh-key: ${{ secrets.ACTIVITYPUB_INFRA_DEPLOY_KEY }} |
| 52 | + path: activitypub-infra |
| 53 | + |
| 54 | + - name: "Checkout terraform repo" |
| 55 | + if: ${{ steps.check-closed-prs.outputs.destroy_prs != '' }} |
| 56 | + uses: actions/checkout@v4 |
| 57 | + with: |
| 58 | + repository: TryGhost/terraform |
| 59 | + ssh-key: ${{ secrets.TERRAFORM_DEPLOY_KEY }} |
| 60 | + path: terraform |
| 61 | + |
| 62 | + - name: "Get terraform version" |
| 63 | + if: ${{ steps.check-closed-prs.outputs.destroy_prs != '' }} |
| 64 | + id: terraform-version |
| 65 | + run: | |
| 66 | + echo "terraform_version=$(cat activitypub-infra/infrastructure/activitypub-staging-environments/.terraform-version)" >> "$GITHUB_OUTPUT" |
| 67 | +
|
| 68 | + - name: "Setup terraform" |
| 69 | + if: ${{ steps.check-closed-prs.outputs.destroy_prs != '' }} |
| 70 | + uses: hashicorp/setup-terraform@v3 |
| 71 | + with: |
| 72 | + terraform_version: ${{ steps.terraform-version.outputs.terraform_version }} |
| 73 | + |
| 74 | + - name: "Change github.com url in modules to local directories and add backend prefix" |
| 75 | + if: ${{ steps.check-closed-prs.outputs.destroy_prs != '' }} |
| 76 | + run: | |
| 77 | + cd activitypub-infra/infrastructure/activitypub-staging-environments |
| 78 | + sed -i 's/github\.com\/TryGhost/\.\.\/\.\.\/\.\./gI' main.tf |
| 79 | + sed -i 's/\?ref=main//g' main.tf |
| 80 | +
|
| 81 | + - name: "Authenticate with GCP" |
| 82 | + if: ${{ steps.check-closed-prs.outputs.destroy_prs != '' }} |
| 83 | + uses: google-github-actions/auth@v2 |
| 84 | + with: |
| 85 | + token_format: access_token |
| 86 | + workload_identity_provider: projects/687476608778/locations/global/workloadIdentityPools/github-oidc-activitypub/providers/github-provider-activitypub |
| 87 | + service_account: stg-activitypub-cicd-stg-envs@ghost-activitypub.iam.gserviceaccount.com |
| 88 | + |
| 89 | + - name: "Remove route from GCP Load Balancer" |
| 90 | + if: ${{ steps.check-closed-prs.outputs.destroy_prs != '' }} |
| 91 | + env: |
| 92 | + DESTROY_PRS: ${{ steps.check-closed-prs.outputs.destroy_prs }} |
| 93 | + GCP_PROJECT: ghost-activitypub |
| 94 | + run: | |
| 95 | + set -euo pipefail |
| 96 | + for PR_NUMBER in ${DESTROY_PRS}; do |
| 97 | + # Get current config |
| 98 | + gcloud compute url-maps export stg-activitypub --global --project ${GCP_PROJECT} > config.yml |
| 99 | + # Delete unnecessary fields |
| 100 | + yq 'del(.fingerprint)' config.yml -i |
| 101 | + yq 'del(.creationTimestamp)' config.yml -i |
| 102 | + export PR_SERVICE="https://www.googleapis.com/compute/v1/projects/ghost-activitypub/global/backendServices/stg-pr-${PR_NUMBER}-api" |
| 103 | + # Remove existing route rules for the PR service |
| 104 | + yq '.pathMatchers[] |= (.routeRules |= map(select((.routeAction.weightedBackendServices // []) | length == 0 or .routeAction.weightedBackendServices[0].backendService != env(PR_SERVICE))))' config.yml > config.yml.tmp |
| 105 | + mv config.yml.tmp config.yml |
| 106 | + echo "Updating url map with:" |
| 107 | + cat config.yml |
| 108 | + gcloud compute url-maps import stg-activitypub --source=config.yml --global --project ${GCP_PROJECT} --quiet |
| 109 | + done |
| 110 | +
|
| 111 | + - name: "Terraform destroy" |
| 112 | + if: ${{ steps.check-closed-prs.outputs.destroy_prs != '' }} |
| 113 | + env: |
| 114 | + DESTROY_PRS: ${{ steps.check-closed-prs.outputs.destroy_prs }} |
| 115 | + run: | |
| 116 | + cd activitypub-infra/infrastructure/activitypub-staging-environments |
| 117 | + for PR_NUMBER in ${DESTROY_PRS}; do |
| 118 | + echo "Destroying PR $PR_NUMBER staging environment." |
| 119 | + sed -i 's/REPLACE_ME/'${PR_NUMBER}'/g' terraform.tf |
| 120 | + terraform init |
| 121 | + export TF_VAR_github_pr_number=$PR_NUMBER |
| 122 | + export TF_VAR_primary_region_name=netherlands |
| 123 | + export TF_VAR_migrations_image=europe-docker.pkg.dev/ghost-activitypub/activitypub/migrations:edge |
| 124 | + export TF_VAR_api_image=europe-docker.pkg.dev/ghost-activitypub/activitypub/activitypub:edge |
| 125 | + export TF_VAR_queue_image=europe-docker.pkg.dev/ghost-activitypub/activitypub/activitypub:edge |
| 126 | + terraform destroy -auto-approve |
| 127 | + done |
0 commit comments