|
| 1 | +name: Deploy Preview Site |
| 2 | +on: |
| 3 | + workflow_run: |
| 4 | + workflows: ["Build Preview Site"] |
| 5 | + types: |
| 6 | + - completed |
| 7 | + |
| 8 | +# This workflow runs with write permissions from the base repository |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + deploy: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + # Only run if the build was successful |
| 17 | + if: ${{ github.event.workflow_run.conclusion == 'success' }} |
| 18 | + steps: |
| 19 | + - name: Checkout 🛎️ |
| 20 | + uses: actions/checkout@v6 |
| 21 | + |
| 22 | + - name: Download artifacts |
| 23 | + uses: actions/download-artifact@v4 |
| 24 | + with: |
| 25 | + run-id: ${{ github.event.workflow_run.id }} |
| 26 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 27 | + pattern: pr-* |
| 28 | + merge-multiple: false |
| 29 | + |
| 30 | + - name: Read PR metadata |
| 31 | + id: pr-metadata |
| 32 | + run: | |
| 33 | + # Find the metadata directory |
| 34 | + metadata_dir=$(find . -type d -name "pr-metadata-*" | head -n 1) |
| 35 | + |
| 36 | + if [ -z "$metadata_dir" ]; then |
| 37 | + echo "Error: Could not find PR metadata" |
| 38 | + exit 1 |
| 39 | + fi |
| 40 | + |
| 41 | + pr_number=$(cat "$metadata_dir/pr_number") |
| 42 | + pr_action=$(cat "$metadata_dir/pr_action") |
| 43 | + pr_sha=$(cat "$metadata_dir/pr_sha") |
| 44 | + |
| 45 | + echo "pr_number=$pr_number" >> $GITHUB_OUTPUT |
| 46 | + echo "pr_action=$pr_action" >> $GITHUB_OUTPUT |
| 47 | + echo "pr_sha=$pr_sha" >> $GITHUB_OUTPUT |
| 48 | + |
| 49 | + echo "PR Number: $pr_number" |
| 50 | + echo "PR Action: $pr_action" |
| 51 | + echo "PR SHA: $pr_sha" |
| 52 | +
|
| 53 | + - name: Extract site |
| 54 | + run: | |
| 55 | + # Find and unzip the public directory |
| 56 | + public_zip=$(find . -type f -name "public-dir-pr-*.zip" | head -n 1) |
| 57 | + |
| 58 | + if [ -z "$public_zip" ]; then |
| 59 | + echo "Error: Could not find public directory zip" |
| 60 | + exit 1 |
| 61 | + fi |
| 62 | + |
| 63 | + unzip -q "$public_zip" |
| 64 | + |
| 65 | + # Ensure the extracted folder has the static files in a 'public' dir for the action |
| 66 | + if [ -d "public-dir" ] && [ ! -d "public" ]; then |
| 67 | + mv public-dir public |
| 68 | + elif [ ! -d "public" ]; then |
| 69 | + echo "Error: Neither public nor public-dir found after extraction" |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | +
|
| 73 | + - name: Deploy Preview |
| 74 | + if: steps.pr-metadata.outputs.pr_action != 'closed' |
| 75 | + |
| 76 | + with: |
| 77 | + source-dir: public |
| 78 | + preview-branch: gh-pages |
| 79 | + umbrella-dir: pr-preview |
| 80 | + custom-url: pr-${{ steps.pr-metadata.outputs.pr_number }} |
| 81 | + token: ${{ secrets.GH_ACCESS_TOKEN }} |
| 82 | + |
| 83 | + - name: Remove Preview on Close |
| 84 | + if: steps.pr-metadata.outputs.pr_action == 'closed' |
| 85 | + |
| 86 | + with: |
| 87 | + source-dir: public |
| 88 | + preview-branch: gh-pages |
| 89 | + umbrella-dir: pr-preview |
| 90 | + custom-url: pr-${{ steps.pr-metadata.outputs.pr_number }} |
| 91 | + action: remove |
| 92 | + token: ${{ secrets.GH_ACCESS_TOKEN }} |
| 93 | + |
| 94 | + - name: Comment on PR |
| 95 | + if: steps.pr-metadata.outputs.pr_action != 'closed' |
| 96 | + uses: actions/github-script@v7 |
| 97 | + with: |
| 98 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 99 | + script: | |
| 100 | + const prNumber = ${{ steps.pr-metadata.outputs.pr_number }}; |
| 101 | + const previewUrl = `https://${context.repo.owner}.github.io/${context.repo.repo}/pr-preview/pr-${prNumber}/`; |
| 102 | + |
| 103 | + // Find existing preview comment |
| 104 | + const comments = await github.rest.issues.listComments({ |
| 105 | + owner: context.repo.owner, |
| 106 | + repo: context.repo.repo, |
| 107 | + issue_number: prNumber, |
| 108 | + }); |
| 109 | + |
| 110 | + const botComment = comments.data.find(comment => |
| 111 | + comment.user.type === 'Bot' && |
| 112 | + comment.body.includes('Preview deployed') |
| 113 | + ); |
| 114 | + |
| 115 | + const commentBody = `🚀 **Preview deployed!**\n\nYou can view the preview at: ${previewUrl}\n\n_This preview will be updated on each push and removed when the PR is closed._`; |
| 116 | + |
| 117 | + if (botComment) { |
| 118 | + // Update existing comment |
| 119 | + await github.rest.issues.updateComment({ |
| 120 | + owner: context.repo.owner, |
| 121 | + repo: context.repo.repo, |
| 122 | + comment_id: botComment.id, |
| 123 | + body: commentBody |
| 124 | + }); |
| 125 | + } else { |
| 126 | + // Create new comment |
| 127 | + await github.rest.issues.createComment({ |
| 128 | + owner: context.repo.owner, |
| 129 | + repo: context.repo.repo, |
| 130 | + issue_number: prNumber, |
| 131 | + body: commentBody |
| 132 | + }); |
| 133 | + } |
0 commit comments