Skip to content

Remove unnecessary comments and trailing newlines from remote develop… #1

Remove unnecessary comments and trailing newlines from remote develop…

Remove unnecessary comments and trailing newlines from remote develop… #1

name: Build and Deploy Live Preview
on:
pull_request:
branches:
- master
types: [opened, synchronize, reopened, closed]
push:
branches:
- preview/*
- staging
permissions:
contents: read
pull-requests: write
pages: write
id-token: write
jobs:
build-preview:
runs-on: ubuntu-latest
if: github.event.action != 'closed'
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.1'
bundler-cache: true
- name: Get branch/PR info
id: branch-info
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
echo "branch-name=pr-${{ github.event.number }}" >> $GITHUB_OUTPUT
echo "is-pr=true" >> $GITHUB_OUTPUT
else
branch_name="${{ github.ref_name }}"
safe_branch=$(echo "$branch_name" | sed 's/[^a-zA-Z0-9-]/-/g' | tr '[:upper:]' '[:lower:]')
echo "branch-name=$safe_branch" >> $GITHUB_OUTPUT
echo "is-pr=false" >> $GITHUB_OUTPUT
fi
- name: Build with Jekyll for preview
run: |
# Update baseurl for preview deployment
sed -i 's|baseurl: ""|baseurl: "/preview/${{ steps.branch-info.outputs.branch-name }}"|' _config.yml
bundle exec jekyll build --destination ./_site
env:
JEKYLL_ENV: production
- name: Deploy to preview branch
run: |
# Configure git
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
# Clone the repository to a temporary directory
git clone https://github.com/${{ github.repository }}.git temp-repo
cd temp-repo
# Check if gh-pages branch exists, create if not
if git ls-remote --exit-code --heads origin gh-pages; then
git checkout gh-pages
else
git checkout --orphan gh-pages
git rm -rf .
fi
# Create preview directory structure
mkdir -p preview/${{ steps.branch-info.outputs.branch-name }}
# Copy built site to preview directory
cp -r ../_site/* preview/${{ steps.branch-info.outputs.branch-name }}/
# Add and commit
git add .
git commit -m "Deploy preview for ${{ steps.branch-info.outputs.branch-name }}" || echo "No changes to commit"
# Push to gh-pages
git push https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git gh-pages
- name: Comment on PR with preview URL
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const { owner, repo } = context.repo;
const prNumber = context.issue.number;
const previewUrl = `https://${owner}.github.io/${repo}/preview/pr-${prNumber}/`;
github.rest.issues.createComment({
owner,
repo,
issue_number: prNumber,
body: `🚀 **Live Preview Ready!**
Your preview is now live at: **${previewUrl}**
📝 This preview will be updated automatically when you push new commits to this PR.
🗑️ The preview will be cleaned up when the PR is merged or closed.`
cleanup-preview:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request' && github.event.action == 'closed'
steps:
- name: Cleanup preview on PR close
run: |
# Configure git
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
# Clone the repository
git clone https://github.com/${{ github.repository }}.git temp-repo
cd temp-repo
# Switch to gh-pages branch
if git ls-remote --exit-code --heads origin gh-pages; then
git checkout gh-pages
# Remove the preview directory
if [ -d "preview/pr-${{ github.event.number }}" ]; then
rm -rf preview/pr-${{ github.event.number }}
git add .
git commit -m "Cleanup preview for closed PR #${{ github.event.number }}" || echo "No changes to commit"
git push https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git gh-pages
fi
fi