|
| 1 | +# .github/workflows/deploy-pr.yml |
| 2 | +name: 2 - Clean Web Page PR |
| 3 | + |
| 4 | +on: |
| 5 | + workflow_run: |
| 6 | + workflows: ["1 - Clean Web Page PR"] |
| 7 | + types: [completed] |
| 8 | + |
| 9 | +# CRITICAL: Grant the workflow permission to write comments on PRs |
| 10 | +permissions: |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + deploy-preview: |
| 15 | + if: github.event.workflow_run.conclusion == 'success' |
| 16 | + runs-on: ubuntu-latest |
| 17 | + environment: Cloudflare Pages |
| 18 | + permissions: |
| 19 | + # actions: read # Only required for private GitHub Repo |
| 20 | + contents: read |
| 21 | + deployments: write |
| 22 | + pull-requests: write |
| 23 | + |
| 24 | + steps: |
| 25 | + # NEW: Download the PR metadata artifact |
| 26 | + - name: Download PR Number Artifact |
| 27 | + uses: actions/download-artifact@v4 |
| 28 | + with: |
| 29 | + name: pr-metadata |
| 30 | + run-id: ${{ github.event.workflow_run.id }} |
| 31 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 32 | + |
| 33 | + - name: Set PR Number Context |
| 34 | + run: | |
| 35 | + PR_NUMBER=$(cat pr-number.txt) |
| 36 | + # This syntax makes it available to subsequent steps as ${{ env.PR_NUMBER }} |
| 37 | + echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV |
| 38 | +
|
| 39 | + - name: Delete Cloudflare Deployments |
| 40 | + env: |
| 41 | + # Pull your secrets securely |
| 42 | + CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 43 | + CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
| 44 | + PROJECT_NAME: "openshift-examples" |
| 45 | + PR_NUMBER: ${{ env.PR_NUMBER }} |
| 46 | + run: | |
| 47 | + echo "Cleaning up deployments for PR #$PR_NUMBER..." |
| 48 | + |
| 49 | + # 1. Install the Cloudflare Wrangler CLI |
| 50 | + npm install -g wrangler |
| 51 | + |
| 52 | + # 2. Fetch all project deployments as a JSON payload |
| 53 | + wrangler pages deployment list --project-name $PROJECT_NAME --json > deployments.json |
| 54 | + |
| 55 | + # 3. Use 'jq' to filter the JSON for deployments matching this PR's branch |
| 56 | + # (In our previous step, we named the branch 'pr-123') |
| 57 | + TARGET_IDS=$(jq -r ".[] | select(.Branch == \"pr-$PR_NUMBER\") | .Id" deployments.json) |
| 58 | + |
| 59 | + # Check if any deployments were actually found |
| 60 | + if [ -z "$TARGET_IDS" ]; then |
| 61 | + echo "No preview deployments found for pr-$PR_NUMBER." |
| 62 | + exit 0 |
| 63 | + fi |
| 64 | + |
| 65 | + # 4. Loop through the matching IDs and force-delete them |
| 66 | + for ID in $TARGET_IDS; do |
| 67 | + echo "Deleting deployment ID: $ID..." |
| 68 | + wrangler pages deployment delete $ID --project-name $PROJECT_NAME --force |
| 69 | + done |
| 70 | + |
| 71 | + echo "🎉 Cleanup complete!" |
0 commit comments