Skip to content
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

Create preview.yml #79

Closed
wants to merge 8 commits into from
Closed
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Jekyll Site Preview
on:
pull_request:
types: [opened, synchronize]

jobs:
build-and-deploy:
runs-on: ubuntu-20.04
permissions:
contents: write
pull-requests: write
pages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Jekyll
run: |
sudo apt-get update
sudo apt-get install -y ruby-full build-essential zlib1g-dev
sudo gem install bundler:2.1.4

# Install jekyll and dependencies
bundle config set --local path 'vendor/bundle'
bundle install

- name: Build Jekyll site
run: |
bundle exec jekyll build
env:
JEKYLL_ENV: production

- name: Deploy preview
run: |
# 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]"

# 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 previous preview if it exists
if [ -d "$PR_DIR" ]; then
rm -rf "$PR_DIR"
fi

# 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 preview for PR #${{ github.event.pull_request.number }}"
git push origin gh-pages

- name: Comment PR
uses: actions/github-script@v7
with:
script: |
const prNumber = context.issue.number;
const repoName = context.repo.repo;
const ownerName = context.repo.owner;
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}\n\nNote: It may take a few minutes for GitHub Pages to update.`
});
Loading