|
| 1 | +name: PR Preview Deployment |
| 2 | + |
| 3 | +# Deploy PR branches to GitHub Pages at /pr-{number}/ for testing |
| 4 | +# This allows testing changes without merging to master |
| 5 | + |
| 6 | +on: |
| 7 | + workflow_dispatch: # Allow manual trigger |
| 8 | + pull_request: |
| 9 | + types: [opened, synchronize, reopened] |
| 10 | + branches: |
| 11 | + - master |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: write |
| 15 | + pages: write |
| 16 | + id-token: write |
| 17 | + pull-requests: write |
| 18 | + |
| 19 | +jobs: |
| 20 | + deploy-preview: |
| 21 | + name: Deploy PR Preview |
| 22 | + runs-on: ubuntu-latest |
| 23 | + |
| 24 | + steps: |
| 25 | + - name: Checkout PR branch |
| 26 | + uses: actions/checkout@v4 |
| 27 | + with: |
| 28 | + ref: ${{ github.event.pull_request.head.sha || github.sha }} |
| 29 | + fetch-depth: 1 |
| 30 | + token: ${{ github.token }} |
| 31 | + |
| 32 | + - name: Determine preview path |
| 33 | + id: preview |
| 34 | + run: | |
| 35 | + if [ "${{ github.event_name }}" = "pull_request" ]; then |
| 36 | + PR_NUMBER="${{ github.event.pull_request.number }}" |
| 37 | + PREVIEW_PATH="pr-${PR_NUMBER}" |
| 38 | + else |
| 39 | + # Manual trigger - use branch name |
| 40 | + BRANCH_NAME="${GITHUB_REF#refs/heads/}" |
| 41 | + PREVIEW_PATH="pr-${BRANCH_NAME//\//-}" |
| 42 | + fi |
| 43 | + echo "path=${PREVIEW_PATH}" >> $GITHUB_OUTPUT |
| 44 | + echo "url=https://ap0ught.github.io/matrix/${PREVIEW_PATH}/" >> $GITHUB_OUTPUT |
| 45 | + echo "Preview will be deployed to: ${PREVIEW_PATH}" |
| 46 | + |
| 47 | + - name: Checkout gh-pages branch |
| 48 | + uses: actions/checkout@v4 |
| 49 | + with: |
| 50 | + ref: gh-pages |
| 51 | + path: gh-pages |
| 52 | + fetch-depth: 0 |
| 53 | + token: ${{ github.token }} |
| 54 | + continue-on-error: true |
| 55 | + |
| 56 | + - name: Initialize gh-pages if needed |
| 57 | + run: | |
| 58 | + if [ ! -d "gh-pages/.git" ]; then |
| 59 | + echo "Creating new gh-pages branch" |
| 60 | + rm -rf gh-pages |
| 61 | + mkdir -p gh-pages |
| 62 | + cd gh-pages |
| 63 | + git init |
| 64 | + git config user.name "github-actions[bot]" |
| 65 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 66 | + git checkout -b gh-pages |
| 67 | + |
| 68 | + # Set up remote with authentication |
| 69 | + git remote add origin "https://x-access-token:${{ github.token }}@github.com/${{ github.repository }}.git" |
| 70 | + |
| 71 | + echo "# Matrix PR Previews" > README.md |
| 72 | + echo "" >> README.md |
| 73 | + echo "This branch contains PR preview deployments." >> README.md |
| 74 | + echo "Main site: https://ap0ught.github.io/matrix/" >> README.md |
| 75 | + |
| 76 | + git add . |
| 77 | + git commit -m "Initialize gh-pages branch" |
| 78 | + cd .. |
| 79 | + else |
| 80 | + echo "gh-pages branch exists, configuring..." |
| 81 | + cd gh-pages |
| 82 | + git config user.name "github-actions[bot]" |
| 83 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 84 | + cd .. |
| 85 | + fi |
| 86 | + |
| 87 | + - name: Prepare preview directory |
| 88 | + run: | |
| 89 | + PREVIEW_PATH="${{ steps.preview.outputs.path }}" |
| 90 | + |
| 91 | + # Create preview directory in gh-pages |
| 92 | + mkdir -p "gh-pages/${PREVIEW_PATH}" |
| 93 | + |
| 94 | + # Copy web application files to preview directory |
| 95 | + cp index.html "gh-pages/${PREVIEW_PATH}/" |
| 96 | + cp -r js "gh-pages/${PREVIEW_PATH}/" |
| 97 | + cp -r lib "gh-pages/${PREVIEW_PATH}/" |
| 98 | + cp -r assets "gh-pages/${PREVIEW_PATH}/" |
| 99 | + cp -r shaders "gh-pages/${PREVIEW_PATH}/" |
| 100 | + |
| 101 | + # Optional files (don't fail if missing) |
| 102 | + cp README.md "gh-pages/${PREVIEW_PATH}/" 2>/dev/null || true |
| 103 | + cp screenshot.png "gh-pages/${PREVIEW_PATH}/" 2>/dev/null || true |
| 104 | + |
| 105 | + echo "✅ Preview files copied to gh-pages/${PREVIEW_PATH}" |
| 106 | + ls -la "gh-pages/${PREVIEW_PATH}" |
| 107 | + |
| 108 | + - name: Update gh-pages index |
| 109 | + run: | |
| 110 | + cd gh-pages |
| 111 | + |
| 112 | + # Create or update index.html with list of previews |
| 113 | + cat > index.html <<'HTMLEOF' |
| 114 | + <!DOCTYPE html> |
| 115 | + <html lang="en"> |
| 116 | + <head> |
| 117 | + <meta charset="UTF-8"> |
| 118 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 119 | + <title>Matrix PR Previews</title> |
| 120 | + <style> |
| 121 | + body { |
| 122 | + font-family: 'Courier New', monospace; |
| 123 | + background: #000; |
| 124 | + color: #0f0; |
| 125 | + padding: 20px; |
| 126 | + max-width: 800px; |
| 127 | + margin: 0 auto; |
| 128 | + } |
| 129 | + h1 { color: #0f0; text-shadow: 0 0 10px #0f0; } |
| 130 | + a { color: #0f0; text-decoration: none; } |
| 131 | + a:hover { text-decoration: underline; text-shadow: 0 0 5px #0f0; } |
| 132 | + .preview { |
| 133 | + margin: 10px 0; |
| 134 | + padding: 10px; |
| 135 | + border: 1px solid #0f0; |
| 136 | + border-radius: 4px; |
| 137 | + } |
| 138 | + .main-link { |
| 139 | + font-size: 1.2em; |
| 140 | + margin-bottom: 30px; |
| 141 | + padding: 15px; |
| 142 | + border: 2px solid #0f0; |
| 143 | + border-radius: 4px; |
| 144 | + } |
| 145 | + </style> |
| 146 | + </head> |
| 147 | + <body> |
| 148 | + <h1>Matrix PR Preview Deployments</h1> |
| 149 | + |
| 150 | + <div class="main-link"> |
| 151 | + <strong>Main Site:</strong> <a href="/">https://ap0ught.github.io/matrix/</a> |
| 152 | + </div> |
| 153 | + |
| 154 | + <h2>Available PR Previews:</h2> |
| 155 | + <div id="previews"></div> |
| 156 | + |
| 157 | + <script> |
| 158 | + const previews = []; |
| 159 | + HTMLEOF |
| 160 | + |
| 161 | + # Add preview paths to the list |
| 162 | + for dir in pr-*/ ; do |
| 163 | + if [ -d "$dir" ]; then |
| 164 | + dir_name="${dir%/}" |
| 165 | + echo " previews.push('$dir_name');" >> index.html |
| 166 | + fi |
| 167 | + done |
| 168 | + |
| 169 | + cat >> index.html <<'HTMLEOF' |
| 170 | + |
| 171 | + const container = document.getElementById('previews'); |
| 172 | + if (previews.length === 0) { |
| 173 | + container.innerHTML = '<p>No PR previews available yet.</p>'; |
| 174 | + } else { |
| 175 | + previews.sort().reverse().forEach(preview => { |
| 176 | + const div = document.createElement('div'); |
| 177 | + div.className = 'preview'; |
| 178 | + div.innerHTML = `<strong>${preview}</strong>: <a href="/matrix/${preview}/">View Preview</a>`; |
| 179 | + container.appendChild(div); |
| 180 | + }); |
| 181 | + } |
| 182 | + </script> |
| 183 | + </body> |
| 184 | + </html> |
| 185 | + HTMLEOF |
| 186 | + |
| 187 | + echo "✅ Updated gh-pages index.html" |
| 188 | + |
| 189 | + - name: Deploy to gh-pages |
| 190 | + run: | |
| 191 | + cd gh-pages |
| 192 | + |
| 193 | + # Ensure git config is set |
| 194 | + git config user.name "github-actions[bot]" |
| 195 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 196 | + |
| 197 | + # Set up authentication for push |
| 198 | + git remote set-url origin "https://x-access-token:${{ github.token }}@github.com/${{ github.repository }}.git" || \ |
| 199 | + git remote add origin "https://x-access-token:${{ github.token }}@github.com/${{ github.repository }}.git" |
| 200 | + |
| 201 | + # Stage all changes |
| 202 | + git add . |
| 203 | + |
| 204 | + # Check if there are changes to commit |
| 205 | + if git diff --staged --quiet; then |
| 206 | + echo "No changes to deploy" |
| 207 | + else |
| 208 | + git commit -m "Deploy preview: ${{ steps.preview.outputs.path }}" |
| 209 | + |
| 210 | + # Push to gh-pages branch |
| 211 | + git push origin gh-pages --force |
| 212 | + echo "✅ Successfully deployed to gh-pages" |
| 213 | + fi |
| 214 | + |
| 215 | + - name: Comment on PR with preview URL |
| 216 | + if: github.event_name == 'pull_request' |
| 217 | + uses: actions/github-script@v7 |
| 218 | + with: |
| 219 | + script: | |
| 220 | + const previewUrl = '${{ steps.preview.outputs.url }}'; |
| 221 | + const comment = `## 🎬 PR Preview Deployed! |
| 222 | + |
| 223 | + Your changes are now available for testing: |
| 224 | + |
| 225 | + **Preview URL:** ${previewUrl} |
| 226 | + |
| 227 | + ### Test Links: |
| 228 | + - [Default Matrix](${previewUrl}?suppressWarnings=true) |
| 229 | + - [Mirror Effect](${previewUrl}?effect=mirror&suppressWarnings=true) |
| 230 | + - [3D Mode](${previewUrl}?version=3d&suppressWarnings=true) |
| 231 | + - [Resurrections](${previewUrl}?version=resurrections&suppressWarnings=true) |
| 232 | + |
| 233 | + The preview will be updated automatically when you push new commits. |
| 234 | + |
| 235 | + --- |
| 236 | + *Preview deployed from commit ${{ github.event.pull_request.head.sha }}*`; |
| 237 | + |
| 238 | + // Find existing preview comment |
| 239 | + const { data: comments } = await github.rest.issues.listComments({ |
| 240 | + owner: context.repo.owner, |
| 241 | + repo: context.repo.repo, |
| 242 | + issue_number: context.issue.number, |
| 243 | + }); |
| 244 | + |
| 245 | + const botComment = comments.find(comment => |
| 246 | + comment.user.type === 'Bot' && |
| 247 | + comment.body.includes('PR Preview Deployed') |
| 248 | + ); |
| 249 | + |
| 250 | + if (botComment) { |
| 251 | + // Update existing comment |
| 252 | + await github.rest.issues.updateComment({ |
| 253 | + owner: context.repo.owner, |
| 254 | + repo: context.repo.repo, |
| 255 | + comment_id: botComment.id, |
| 256 | + body: comment |
| 257 | + }); |
| 258 | + } else { |
| 259 | + // Create new comment |
| 260 | + await github.rest.issues.createComment({ |
| 261 | + owner: context.repo.owner, |
| 262 | + repo: context.repo.repo, |
| 263 | + issue_number: context.issue.number, |
| 264 | + body: comment |
| 265 | + }); |
| 266 | + } |
| 267 | + |
| 268 | + - name: Summary |
| 269 | + run: | |
| 270 | + echo "## 🎉 PR Preview Deployed Successfully!" >> $GITHUB_STEP_SUMMARY |
| 271 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 272 | + echo "**Preview URL:** ${{ steps.preview.outputs.url }}" >> $GITHUB_STEP_SUMMARY |
| 273 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 274 | + echo "### Quick Test Links:" >> $GITHUB_STEP_SUMMARY |
| 275 | + echo "- [Default Matrix](${{ steps.preview.outputs.url }}?suppressWarnings=true)" >> $GITHUB_STEP_SUMMARY |
| 276 | + echo "- [Mirror Effect](${{ steps.preview.outputs.url }}?effect=mirror&suppressWarnings=true)" >> $GITHUB_STEP_SUMMARY |
| 277 | + echo "- [3D Mode](${{ steps.preview.outputs.url }}?version=3d&suppressWarnings=true)" >> $GITHUB_STEP_SUMMARY |
0 commit comments