-
Notifications
You must be signed in to change notification settings - Fork 8
Update Previews #392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anarnold97
wants to merge
2
commits into
migtools:main
Choose a base branch
from
anarnold97:New-Preview-PR
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Update Previews #392
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| name: PR Preview Cleanup | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [closed] | ||
|
|
||
| permissions: | ||
| contents: write | ||
| pull-requests: write | ||
|
|
||
| # Use the same concurrency group as the deploy workflow so cleanup and | ||
| # deploy pushes to gh-pages are always serialized. | ||
| concurrency: | ||
| group: gh-pages-deploy | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| cleanup: | ||
| name: Remove Preview | ||
| runs-on: ubuntu-latest | ||
| if: github.event.pull_request.head.repo.full_name == github.repository | ||
|
|
||
| steps: | ||
| - name: Checkout gh-pages branch | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| ref: gh-pages | ||
| token: ${{ secrets.GITHUB_TOKEN }} | ||
| fetch-depth: 1 | ||
| persist-credentials: true | ||
|
|
||
| - name: Delete preview directory | ||
| env: | ||
| PR_NUMBER: ${{ github.event.number }} | ||
| run: | | ||
| PREVIEW_DIR="pr-previews/${PR_NUMBER}" | ||
| if [ -d "${PREVIEW_DIR}" ]; then | ||
| git config user.name "github-actions[bot]" | ||
| git config user.email "github-actions[bot]@users.noreply.github.com" | ||
| git rm -rf "${PREVIEW_DIR}" | ||
| git commit -m "chore: remove preview for PR #${PR_NUMBER}" | ||
|
|
||
| for attempt in 1 2 3; do | ||
| git fetch origin gh-pages | ||
| git rebase origin/gh-pages 2>/dev/null || { git rebase --abort; true; } | ||
| git push origin gh-pages && break | ||
| echo "Push attempt ${attempt} failed, retrying..." | ||
| sleep $((attempt * 5)) | ||
| done | ||
| else | ||
| echo "No preview directory found for PR #${PR_NUMBER}, nothing to do." | ||
| fi | ||
|
|
||
| - name: Update preview comment to reflect closure | ||
| uses: actions/github-script@v7 | ||
| env: | ||
| PR_NUMBER: ${{ github.event.number }} | ||
| with: | ||
| github-token: ${{ secrets.GITHUB_TOKEN }} | ||
| script: | | ||
| const prNumber = process.env.PR_NUMBER; | ||
| const owner = context.repo.owner; | ||
| const repo = context.repo.repo; | ||
|
|
||
| const marker = '<!-- pr-preview-bot -->'; | ||
|
|
||
| // Paginate to handle PRs with more than 30 comments | ||
| const comments = await github.paginate(github.rest.issues.listComments, { | ||
| owner, repo, issue_number: prNumber, | ||
| }); | ||
|
|
||
| const existing = comments.find(c => c.body.includes(marker)); | ||
| if (existing) { | ||
| await github.rest.issues.updateComment({ | ||
| owner, repo, | ||
| comment_id: existing.id, | ||
| body: marker + '\n## 📄 Documentation Preview\n\n> This preview has been removed because the PR was closed.', | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| name: PR Preview | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened, synchronize, reopened] | ||
| paths: | ||
| - 'docs/**' | ||
| - 'assemblies/**' | ||
| - 'topics/**' | ||
| - '_includes/**' | ||
| - '_layouts/**' | ||
| - '_sass/**' | ||
| - 'assets/**' | ||
| - '_config.yml' | ||
| - 'Gemfile' | ||
| - 'index.md' | ||
| - '.github/workflows/pr-preview.yml' | ||
|
|
||
| permissions: | ||
| contents: write | ||
| pull-requests: write | ||
|
|
||
| # Serialize all gh-pages pushes across both workflows to prevent | ||
| # non-fast-forward failures when two PRs deploy at the same time. | ||
| concurrency: | ||
| group: gh-pages-deploy | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| build-and-deploy: | ||
| name: Build & Deploy Preview | ||
| runs-on: ubuntu-latest | ||
| # Skip fork PRs — GITHUB_TOKEN is read-only for forks and the push would fail. | ||
| if: github.event.pull_request.head.repo.full_name == github.repository | ||
|
|
||
| steps: | ||
| - name: Checkout PR branch | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| persist-credentials: true | ||
|
|
||
| - name: Set up Ruby | ||
| uses: ruby/setup-ruby@v1 | ||
| with: | ||
| ruby-version: '3.2' | ||
| bundler-cache: true | ||
|
|
||
| - name: Install system dependencies | ||
| run: sudo apt-get install -y asciidoctor | ||
|
|
||
| - name: Build site with preview base URL | ||
| env: | ||
| JEKYLL_ENV: production | ||
| PR_NUMBER: ${{ github.event.number }} | ||
| run: | | ||
| bundle exec jekyll build \ | ||
| --baseurl "/mta-documentation/pr-previews/${PR_NUMBER}" \ | ||
| --destination _site | ||
|
|
||
| - name: Deploy preview to gh-pages | ||
| env: | ||
| PR_NUMBER: ${{ github.event.number }} | ||
| run: | | ||
| git config user.name "github-actions[bot]" | ||
| git config user.email "github-actions[bot]@users.noreply.github.com" | ||
|
|
||
| mkdir -p /tmp/pr-preview | ||
| cp -r _site/. /tmp/pr-preview/ | ||
|
|
||
| git fetch origin gh-pages 2>/dev/null || true | ||
| if git show-ref --verify --quiet refs/remotes/origin/gh-pages; then | ||
| git checkout -B gh-pages origin/gh-pages | ||
| else | ||
| git checkout --orphan gh-pages | ||
| git rm -rf . --quiet | ||
| echo "GitHub Pages for mta-documentation" > README.md | ||
| git add README.md | ||
| git commit -m "chore: initialise gh-pages branch" | ||
| fi | ||
|
|
||
| PREVIEW_DIR="pr-previews/${PR_NUMBER}" | ||
| rm -rf "${PREVIEW_DIR}" | ||
| mkdir -p "${PREVIEW_DIR}" | ||
| cp -r /tmp/pr-preview/. "${PREVIEW_DIR}/" | ||
|
|
||
| git add "${PREVIEW_DIR}" | ||
| if git diff --staged --quiet; then | ||
| echo "No changes to deploy." | ||
| else | ||
| COMMIT_SHA="${{ github.event.pull_request.head.sha }}" | ||
| git commit -m "preview: PR #${PR_NUMBER} @ ${COMMIT_SHA::7}" | ||
|
|
||
| # Retry push up to 3 times in case of a concurrent non-fast-forward | ||
| for attempt in 1 2 3; do | ||
| git fetch origin gh-pages | ||
| git rebase origin/gh-pages 2>/dev/null || { git rebase --abort; true; } | ||
| git push origin gh-pages && break | ||
| echo "Push attempt ${attempt} failed, retrying..." | ||
| sleep $((attempt * 5)) | ||
| done | ||
| fi | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| - name: Post or update preview comment | ||
| uses: actions/github-script@v7 | ||
| env: | ||
| PR_NUMBER: ${{ github.event.number }} | ||
| with: | ||
| github-token: ${{ secrets.GITHUB_TOKEN }} | ||
| script: | | ||
| const prNumber = process.env.PR_NUMBER; | ||
| const sha = context.payload.pull_request.head.sha.substring(0, 7); | ||
| const owner = context.repo.owner; | ||
| const repo = context.repo.repo; | ||
|
|
||
| const previewUrl = | ||
| `https://${owner}.github.io/${repo}/pr-previews/${prNumber}/`; | ||
|
|
||
| const body = [ | ||
| `## 📄 Documentation Preview`, | ||
| ``, | ||
| `| | |`, | ||
| `|---|---|`, | ||
| `| **Preview URL** | ${previewUrl} |`, | ||
| `| **Commit** | \`${sha}\` |`, | ||
| `| **Updated** | ${new Date().toUTCString()} |`, | ||
| ``, | ||
| `> Preview updates automatically on every push to this PR.`, | ||
| `> It will be removed when the PR is closed.`, | ||
| ].join('\n'); | ||
|
|
||
| const marker = '<!-- pr-preview-bot -->'; | ||
| const markedBody = marker + '\n' + body; | ||
|
|
||
| // Paginate to handle PRs with more than 30 comments | ||
| const comments = await github.paginate(github.rest.issues.listComments, { | ||
| owner, repo, issue_number: prNumber, | ||
| }); | ||
|
|
||
| const existing = comments.find(c => c.body.includes(marker)); | ||
|
|
||
| if (existing) { | ||
| await github.rest.issues.updateComment({ | ||
| owner, repo, | ||
| comment_id: existing.id, | ||
| body: markedBody, | ||
| }); | ||
| } else { | ||
| await github.rest.issues.createComment({ | ||
| owner, repo, | ||
| issue_number: prNumber, | ||
| body: markedBody, | ||
| }); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.