|
| 1 | +name: Deploy Preview Site |
| 2 | +on: |
| 3 | + workflow_run: |
| 4 | + workflows: ["Build Preview Site"] |
| 5 | + types: |
| 6 | + - completed |
| 7 | + branches: |
| 8 | + - master |
| 9 | + |
| 10 | +# This workflow runs with write permissions from the base repository |
| 11 | +permissions: |
| 12 | + contents: write |
| 13 | + pull-requests: write |
| 14 | + actions: read |
| 15 | + |
| 16 | +jobs: |
| 17 | + deploy: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + # Only run if the build was successful and originated from a PR |
| 20 | + if: | |
| 21 | + github.event.workflow_run.conclusion == 'success' && |
| 22 | + github.event.workflow_run.event == 'pull_request' |
| 23 | + steps: |
| 24 | + - name: Checkout 🛎️ |
| 25 | + uses: actions/checkout@v6 |
| 26 | + |
| 27 | + - name: Download artifacts |
| 28 | + uses: actions/github-script@v7 |
| 29 | + with: |
| 30 | + script: | |
| 31 | + let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 32 | + owner: context.repo.owner, |
| 33 | + repo: context.repo.repo, |
| 34 | + run_id: context.payload.workflow_run.id, |
| 35 | + }); |
| 36 | + |
| 37 | + let matchArtifacts = allArtifacts.data.artifacts.filter((artifact) => { |
| 38 | + return artifact.name.match(/^pr-(metadata|public-dir)-/); |
| 39 | + }); |
| 40 | + |
| 41 | + if (matchArtifacts.length === 0) { |
| 42 | + core.setFailed('No artifacts found'); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + let fs = require('fs'); |
| 47 | + for (const artifact of matchArtifacts) { |
| 48 | + let download = await github.rest.actions.downloadArtifact({ |
| 49 | + owner: context.repo.owner, |
| 50 | + repo: context.repo.repo, |
| 51 | + artifact_id: artifact.id, |
| 52 | + archive_format: 'zip', |
| 53 | + }); |
| 54 | + fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/${artifact.name}.zip`, Buffer.from(download.data)); |
| 55 | + } |
| 56 | + |
| 57 | + - name: Extract artifacts |
| 58 | + run: | |
| 59 | + for zip in *.zip; do |
| 60 | + if [ -f "$zip" ]; then |
| 61 | + mkdir -p "${zip%.zip}" |
| 62 | + unzip -q "$zip" -d "${zip%.zip}" |
| 63 | + fi |
| 64 | + done |
| 65 | + |
| 66 | + - name: Read PR metadata |
| 67 | + id: pr-metadata |
| 68 | + run: | |
| 69 | + # Find the metadata directory |
| 70 | + metadata_dir=$(find . -type d -name "pr-metadata-*" | head -n 1) |
| 71 | + |
| 72 | + if [ -z "$metadata_dir" ]; then |
| 73 | + echo "Error: Could not find PR metadata" |
| 74 | + ls -la |
| 75 | + exit 1 |
| 76 | + fi |
| 77 | + |
| 78 | + pr_number=$(cat "$metadata_dir/pr_number") |
| 79 | + pr_action=$(cat "$metadata_dir/pr_action") |
| 80 | + pr_sha=$(cat "$metadata_dir/pr_sha") |
| 81 | + |
| 82 | + echo "pr_number=$pr_number" >> $GITHUB_OUTPUT |
| 83 | + echo "pr_action=$pr_action" >> $GITHUB_OUTPUT |
| 84 | + echo "pr_sha=$pr_sha" >> $GITHUB_OUTPUT |
| 85 | + |
| 86 | + echo "PR Number: $pr_number" |
| 87 | + echo "PR Action: $pr_action" |
| 88 | + echo "PR SHA: $pr_sha" |
| 89 | +
|
| 90 | + - name: Extract site |
| 91 | + run: | |
| 92 | + # Find and extract the public directory zip |
| 93 | + public_artifact=$(find . -type d -name "public-dir-pr-*" | head -n 1) |
| 94 | + |
| 95 | + if [ -z "$public_artifact" ]; then |
| 96 | + echo "Error: Could not find public directory artifact" |
| 97 | + ls -la |
| 98 | + exit 1 |
| 99 | + fi |
| 100 | + |
| 101 | + # Find the zip file in the artifact directory |
| 102 | + public_zip=$(find "$public_artifact" -type f -name "*.zip" | head -n 1) |
| 103 | + |
| 104 | + if [ -z "$public_zip" ]; then |
| 105 | + echo "Error: Could not find public directory zip in artifact" |
| 106 | + ls -la "$public_artifact" |
| 107 | + exit 1 |
| 108 | + fi |
| 109 | + |
| 110 | + unzip -q "$public_zip" |
| 111 | + |
| 112 | + # Ensure the extracted folder has the static files in a 'public' dir for the action |
| 113 | + if [ -d "public-dir" ] && [ ! -d "public" ]; then |
| 114 | + mv public-dir public |
| 115 | + elif [ ! -d "public" ]; then |
| 116 | + echo "Error: Neither public nor public-dir found after extraction" |
| 117 | + ls -la |
| 118 | + exit 1 |
| 119 | + fi |
| 120 | +
|
| 121 | + - name: Deploy Preview |
| 122 | + if: steps.pr-metadata.outputs.pr_action != 'closed' |
| 123 | + |
| 124 | + with: |
| 125 | + source-dir: public |
| 126 | + preview-branch: gh-pages |
| 127 | + umbrella-dir: pr-preview |
| 128 | + custom-url: pr-${{ steps.pr-metadata.outputs.pr_number }} |
| 129 | + token: ${{ secrets.GH_ACCESS_TOKEN }} |
| 130 | + |
| 131 | + - name: Remove Preview on Close |
| 132 | + if: steps.pr-metadata.outputs.pr_action == 'closed' |
| 133 | + |
| 134 | + with: |
| 135 | + source-dir: public |
| 136 | + preview-branch: gh-pages |
| 137 | + umbrella-dir: pr-preview |
| 138 | + custom-url: pr-${{ steps.pr-metadata.outputs.pr_number }} |
| 139 | + action: remove |
| 140 | + token: ${{ secrets.GH_ACCESS_TOKEN }} |
| 141 | + |
| 142 | + - name: Comment on PR |
| 143 | + if: steps.pr-metadata.outputs.pr_action != 'closed' |
| 144 | + uses: actions/github-script@v7 |
| 145 | + with: |
| 146 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 147 | + script: | |
| 148 | + const prNumber = ${{ steps.pr-metadata.outputs.pr_number }}; |
| 149 | + const previewUrl = `https://${context.repo.owner}.github.io/${context.repo.repo}/pr-preview/pr-${prNumber}/`; |
| 150 | + |
| 151 | + // Find existing preview comment |
| 152 | + const comments = await github.rest.issues.listComments({ |
| 153 | + owner: context.repo.owner, |
| 154 | + repo: context.repo.repo, |
| 155 | + issue_number: prNumber, |
| 156 | + }); |
| 157 | + |
| 158 | + const botComment = comments.data.find(comment => |
| 159 | + comment.user.type === 'Bot' && |
| 160 | + comment.body.includes('Preview deployed') |
| 161 | + ); |
| 162 | + |
| 163 | + 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._`; |
| 164 | + |
| 165 | + if (botComment) { |
| 166 | + // Update existing comment |
| 167 | + await github.rest.issues.updateComment({ |
| 168 | + owner: context.repo.owner, |
| 169 | + repo: context.repo.repo, |
| 170 | + comment_id: botComment.id, |
| 171 | + body: commentBody |
| 172 | + }); |
| 173 | + } else { |
| 174 | + // Create new comment |
| 175 | + await github.rest.issues.createComment({ |
| 176 | + owner: context.repo.owner, |
| 177 | + repo: context.repo.repo, |
| 178 | + issue_number: prNumber, |
| 179 | + body: commentBody |
| 180 | + }); |
| 181 | + } |
0 commit comments