Skip to content

Commit

Permalink
Update preview.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
bendichter authored Nov 25, 2024
1 parent c33ddbd commit d786e1d
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,35 @@ jobs:

- name: Deploy preview
run: |
BRANCH_NAME="preview-pr-${{ github.event.pull_request.number }}"
# Create a temporary directory for the preview
PR_DIR="pr-${{ github.event.pull_request.number }}"
# Configure git
git config --global user.name "GitHub Actions"
git config --global user.email "[email protected]"
git checkout -b $BRANCH_NAME
# Fetch gh-pages branch if it exists, or create it
if git fetch origin gh-pages; then
git checkout gh-pages
else
git checkout --orphan gh-pages
git rm -rf .
git commit --allow-empty -m "Initial gh-pages branch"
fi
# Remove existing content except _site and .git
find . -mindepth 1 -maxdepth 1 ! -name '_site' ! -name '.git' -exec rm -rf {} +
# Remove previous preview if it exists
if [ -d "$PR_DIR" ]; then
rm -rf "$PR_DIR"
fi
# Move _site contents to root
cp -r _site/* ./
rm -rf _site
# Move built site to PR-specific directory
mkdir -p "$PR_DIR"
cp -r _site/* "$PR_DIR/"
# Commit and push
git add .
git commit -m "Deploy PR preview"
git push origin $BRANCH_NAME --force
git commit -m "Deploy preview for PR #${{ github.event.pull_request.number }}"
git push origin gh-pages
- name: Comment PR
uses: actions/github-script@v7
Expand All @@ -57,11 +69,11 @@ jobs:
const prNumber = context.issue.number;
const repoName = context.repo.repo;
const ownerName = context.repo.owner;
const previewUrl = `https://${ownerName}.github.io/${repoName}/preview-pr-${prNumber}/`;
const previewUrl = `https://${ownerName}.github.io/${repoName}/pr-${prNumber}/`;
github.rest.issues.createComment({
owner: ownerName,
repo: repoName,
issue_number: prNumber,
body: `📝 Preview this PR at: ${previewUrl}`
body: `📝 Preview this PR at: ${previewUrl}\n\nNote: It may take a few minutes for GitHub Pages to update.`
});

0 comments on commit d786e1d

Please sign in to comment.