|
| 1 | +name: Build and Deploy Live Preview |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + types: [opened, synchronize, reopened, closed] |
| 8 | + push: |
| 9 | + branches: |
| 10 | + - preview/* |
| 11 | + - staging |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: read |
| 15 | + pull-requests: write |
| 16 | + pages: write |
| 17 | + id-token: write |
| 18 | + |
| 19 | +jobs: |
| 20 | + build-preview: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + if: github.event.action != 'closed' |
| 23 | + steps: |
| 24 | + - name: Checkout |
| 25 | + uses: actions/checkout@v4 |
| 26 | + |
| 27 | + - name: Setup Ruby |
| 28 | + uses: ruby/setup-ruby@v1 |
| 29 | + with: |
| 30 | + ruby-version: '3.1' |
| 31 | + bundler-cache: true |
| 32 | + |
| 33 | + - name: Get branch/PR info |
| 34 | + id: branch-info |
| 35 | + run: | |
| 36 | + if [ "${{ github.event_name }}" = "pull_request" ]; then |
| 37 | + echo "branch-name=pr-${{ github.event.number }}" >> $GITHUB_OUTPUT |
| 38 | + echo "is-pr=true" >> $GITHUB_OUTPUT |
| 39 | + else |
| 40 | + branch_name="${{ github.ref_name }}" |
| 41 | + safe_branch=$(echo "$branch_name" | sed 's/[^a-zA-Z0-9-]/-/g' | tr '[:upper:]' '[:lower:]') |
| 42 | + echo "branch-name=$safe_branch" >> $GITHUB_OUTPUT |
| 43 | + echo "is-pr=false" >> $GITHUB_OUTPUT |
| 44 | + fi |
| 45 | + |
| 46 | + - name: Build with Jekyll for preview |
| 47 | + run: | |
| 48 | + # Update baseurl for preview deployment |
| 49 | + sed -i 's|baseurl: ""|baseurl: "/preview/${{ steps.branch-info.outputs.branch-name }}"|' _config.yml |
| 50 | + bundle exec jekyll build --destination ./_site |
| 51 | + env: |
| 52 | + JEKYLL_ENV: production |
| 53 | + |
| 54 | + - name: Deploy to preview branch |
| 55 | + run: | |
| 56 | + # Configure git |
| 57 | + git config --global user.name "github-actions[bot]" |
| 58 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 59 | + |
| 60 | + # Clone the repository to a temporary directory |
| 61 | + git clone https://github.com/${{ github.repository }}.git temp-repo |
| 62 | + cd temp-repo |
| 63 | + |
| 64 | + # Check if gh-pages branch exists, create if not |
| 65 | + if git ls-remote --exit-code --heads origin gh-pages; then |
| 66 | + git checkout gh-pages |
| 67 | + else |
| 68 | + git checkout --orphan gh-pages |
| 69 | + git rm -rf . |
| 70 | + fi |
| 71 | + |
| 72 | + # Create preview directory structure |
| 73 | + mkdir -p preview/${{ steps.branch-info.outputs.branch-name }} |
| 74 | + |
| 75 | + # Copy built site to preview directory |
| 76 | + cp -r ../_site/* preview/${{ steps.branch-info.outputs.branch-name }}/ |
| 77 | + |
| 78 | + # Add and commit |
| 79 | + git add . |
| 80 | + git commit -m "Deploy preview for ${{ steps.branch-info.outputs.branch-name }}" || echo "No changes to commit" |
| 81 | + |
| 82 | + # Push to gh-pages |
| 83 | + git push https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git gh-pages |
| 84 | + |
| 85 | + - name: Comment on PR with preview URL |
| 86 | + if: github.event_name == 'pull_request' |
| 87 | + uses: actions/github-script@v7 |
| 88 | + with: |
| 89 | + script: | |
| 90 | + const { owner, repo } = context.repo; |
| 91 | + const prNumber = context.issue.number; |
| 92 | + const previewUrl = `https://${owner}.github.io/${repo}/preview/pr-${prNumber}/`; |
| 93 | + |
| 94 | + github.rest.issues.createComment({ |
| 95 | + owner, |
| 96 | + repo, |
| 97 | + issue_number: prNumber, |
| 98 | + body: `🚀 **Live Preview Ready!** |
| 99 | + |
| 100 | + Your preview is now live at: **${previewUrl}** |
| 101 | + |
| 102 | + 📝 This preview will be updated automatically when you push new commits to this PR. |
| 103 | + 🗑️ The preview will be cleaned up when the PR is merged or closed.` |
| 104 | +
|
| 105 | + cleanup-preview: |
| 106 | + runs-on: ubuntu-latest |
| 107 | + if: github.event_name == 'pull_request' && github.event.action == 'closed' |
| 108 | + steps: |
| 109 | + - name: Cleanup preview on PR close |
| 110 | + run: | |
| 111 | + # Configure git |
| 112 | + git config --global user.name "github-actions[bot]" |
| 113 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 114 | + |
| 115 | + # Clone the repository |
| 116 | + git clone https://github.com/${{ github.repository }}.git temp-repo |
| 117 | + cd temp-repo |
| 118 | + |
| 119 | + # Switch to gh-pages branch |
| 120 | + if git ls-remote --exit-code --heads origin gh-pages; then |
| 121 | + git checkout gh-pages |
| 122 | + |
| 123 | + # Remove the preview directory |
| 124 | + if [ -d "preview/pr-${{ github.event.number }}" ]; then |
| 125 | + rm -rf preview/pr-${{ github.event.number }} |
| 126 | + git add . |
| 127 | + git commit -m "Cleanup preview for closed PR #${{ github.event.number }}" || echo "No changes to commit" |
| 128 | + git push https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git gh-pages |
| 129 | + fi |
| 130 | + fi |
0 commit comments