|
23 | 23 | steps: |
24 | 24 | - name: 🔍 Checkout repository |
25 | 25 | uses: actions/checkout@v4 |
| 26 | + with: |
| 27 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 28 | + |
| 29 | + - name: 🧹 Strip stale deployment markers from README |
| 30 | + if: github.event_name == 'push' |
| 31 | + run: | |
| 32 | + if [ -f README.md ] && grep -q '<!-- DEPLOYMENT-URL:START -->' README.md; then |
| 33 | + echo "Stripping stale deployment markers from README..." |
| 34 | + # Remove everything between markers (inclusive), handling duplicates |
| 35 | + sed -i '/<!-- DEPLOYMENT-URL:START -->/,/<!-- DEPLOYMENT-URL:END -->/d' README.md |
| 36 | + # Also clean up any git conflict markers that might be left over |
| 37 | + sed -i '/^<<<<<<</d; /^=======/d; /^>>>>>>>/d' README.md |
| 38 | + # Remove resulting double-blank-lines |
| 39 | + sed -i '/^$/N;/^\n$/d' README.md |
| 40 | + if ! git diff --quiet README.md; then |
| 41 | + git config user.name "github-actions" |
| 42 | + git config user.email "github-actions@github.com" |
| 43 | + git add README.md |
| 44 | + git commit -m "ci: clean stale deployment markers from README [skip ci]" |
| 45 | + git push |
| 46 | + echo "Cleaned up stale deployment markers." |
| 47 | + fi |
| 48 | + fi |
26 | 49 |
|
27 | 50 | - name: 🔧 Set deployment variables |
28 | 51 | id: vars |
@@ -186,3 +209,83 @@ jobs: |
186 | 209 | body, |
187 | 210 | }); |
188 | 211 | } |
| 212 | +
|
| 213 | + - name: 📝 Update README with deployment URL |
| 214 | + if: github.event_name == 'push' |
| 215 | + uses: actions/github-script@v7 |
| 216 | + with: |
| 217 | + script: | |
| 218 | + const owner = context.repo.owner; |
| 219 | + const repo = context.repo.repo; |
| 220 | + const branch = '${{ steps.vars.outputs.branch }}'; |
| 221 | + const previewUrl = '${{ steps.vars.outputs.preview_url }}'; |
| 222 | + const sha = context.sha.substring(0, 7); |
| 223 | + const date = new Date().toISOString().split('T')[0]; |
| 224 | +
|
| 225 | + // Fetch current README from the source branch |
| 226 | + let readmeData; |
| 227 | + try { |
| 228 | + readmeData = await github.rest.repos.getContent({ |
| 229 | + owner, repo, path: 'README.md', ref: branch, |
| 230 | + }); |
| 231 | + } catch (e) { |
| 232 | + console.log('No README.md found, skipping update.'); |
| 233 | + return; |
| 234 | + } |
| 235 | +
|
| 236 | + const content = Buffer.from(readmeData.data.content, 'base64').toString('utf8'); |
| 237 | +
|
| 238 | + const marker = { |
| 239 | + start: '<!-- DEPLOYMENT-URL:START -->', |
| 240 | + end: '<!-- DEPLOYMENT-URL:END -->', |
| 241 | + }; |
| 242 | +
|
| 243 | + const section = [ |
| 244 | + marker.start, |
| 245 | + `> **🌐 Live Preview:** [${previewUrl}](${previewUrl})`, |
| 246 | + `> Deployed from \`${sha}\` on ${date}`, |
| 247 | + marker.end, |
| 248 | + ].join('\n'); |
| 249 | +
|
| 250 | + let updated; |
| 251 | + if (content.includes(marker.start)) { |
| 252 | + // Replace existing section |
| 253 | + const re = new RegExp( |
| 254 | + `${marker.start}[\\s\\S]*?${marker.end}`, |
| 255 | + ); |
| 256 | + updated = content.replace(re, section); |
| 257 | + } else { |
| 258 | + // Insert after the first heading line |
| 259 | + const lines = content.split('\n'); |
| 260 | + const headingIdx = lines.findIndex(l => l.startsWith('# ')); |
| 261 | + if (headingIdx !== -1) { |
| 262 | + lines.splice(headingIdx + 1, 0, '', section, ''); |
| 263 | + updated = lines.join('\n'); |
| 264 | + } else { |
| 265 | + updated = section + '\n\n' + content; |
| 266 | + } |
| 267 | + } |
| 268 | +
|
| 269 | + if (updated !== content) { |
| 270 | + await github.rest.repos.createOrUpdateFileContents({ |
| 271 | + owner, repo, path: 'README.md', |
| 272 | + message: `docs: update deployment URL for ${branch} [skip ci]`, |
| 273 | + content: Buffer.from(updated).toString('base64'), |
| 274 | + sha: readmeData.data.sha, |
| 275 | + branch, |
| 276 | + }); |
| 277 | + console.log(`README updated with preview URL: ${previewUrl}`); |
| 278 | + } |
| 279 | +
|
| 280 | + - name: 🌐 Update GitHub repo homepage URL |
| 281 | + if: github.event_name == 'push' && steps.vars.outputs.is_main == 'true' |
| 282 | + uses: actions/github-script@v7 |
| 283 | + with: |
| 284 | + script: | |
| 285 | + const previewUrl = '${{ steps.vars.outputs.preview_url }}'; |
| 286 | + await github.rest.repos.update({ |
| 287 | + owner: context.repo.owner, |
| 288 | + repo: context.repo.repo, |
| 289 | + homepage: previewUrl, |
| 290 | + }); |
| 291 | + console.log(`Repository homepage updated to: ${previewUrl}`); |
0 commit comments