Refactor preview deployment workflow for improved clarity and functio… #4
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
| name: Build Preview for Testing | |
| on: | |
| pull_request: | |
| branches: | |
| - main | |
| types: [opened, synchronize, reopened, closed] | |
| push: | |
| branches: | |
| - preview/* | |
| - staging | |
| permissions: | |
| contents: read | |
| pull-requests: 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: | | |
| bundle exec jekyll build --destination ./_site | |
| env: | |
| JEKYLL_ENV: production | |
| - name: Upload preview artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: preview-site-${{ steps.branch-info.outputs.branch-name }} | |
| path: ./_site | |
| retention-days: 7 | |
| - name: Comment on PR with preview info | |
| if: github.event_name == 'pull_request' | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const { owner, repo } = context.repo; | |
| const prNumber = context.issue.number; | |
| const runId = context.runId; | |
| const branchName = "${{ steps.branch-info.outputs.branch-name }}"; | |
| github.rest.issues.createComment({ | |
| owner, | |
| repo, | |
| issue_number: prNumber, | |
| body: `🚀 **Preview Build Complete!** | |
| Your preview has been built successfully and is ready for testing. | |
| **Download & Test Locally:** | |
| 1. Go to [Actions Run #${runId}](https://github.com/${owner}/${repo}/actions/runs/${runId}) | |
| 2. Download the \`preview-site-${branchName}\` artifact | |
| 3. Extract and serve: \`cd extracted-folder && python3 -m http.server 8000\` | |
| 4. Open http://localhost:8000 in your browser | |
| 💡 **Quick Test Command:** | |
| \`\`\`bash | |
| # After downloading and extracting: | |
| python3 -m http.server 8000 | |
| # Then open: http://localhost:8000 | |
| \`\`\` | |
| � This preview updates automatically with each commit to this PR.` | |
| }); | |
| cleanup-preview: | |
| runs-on: ubuntu-latest | |
| if: github.event_name == 'pull_request' && github.event.action == 'closed' | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: gh-pages | |
| - name: Remove preview directory | |
| run: | | |
| if [ -d "preview/pr-${{ github.event.number }}" ]; then | |
| rm -rf preview/pr-${{ github.event.number }} | |
| echo "Removed preview directory for PR #${{ github.event.number }}" | |
| else | |
| echo "Preview directory for PR #${{ github.event.number }} not found" | |
| fi | |
| - name: Deploy cleanup | |
| uses: peaceiris/actions-gh-pages@v3 | |
| with: | |
| github_token: ${{ secrets.GITHUB_TOKEN }} | |
| publish_dir: . | |
| keep_files: true |