Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
51 changes: 16 additions & 35 deletions .github/workflows/pr-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,44 @@ jobs:
deploy-preview:
runs-on: ubuntu-latest
permissions:
contents: read
contents: write
pull-requests: write
pages: write
id-token: write

steps:
- name: Checkout PR branch
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 1

- name: Set up Node.js
if: env.BUILD_REQUIRED == 'true' # Set this environment variable in the repository settings if a build is needed
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm' # Cache dependencies for faster builds

- name: Install dependencies
if: env.BUILD_REQUIRED == 'true'
run: npm ci # Use npm ci for reproducible builds

- name: Build project
if: env.BUILD_REQUIRED == 'true'
run: npm run build # Replace with your build command (e.g., `hugo`, `vite build`, etc.)
env:
BASE_URL: /${{ github.repository }}/pr-preview/pr-${{ github.event.pull_request.number }}/
fetch-depth: 0

- name: Copy files to preview folder
run: |
mkdir -p preview/pr-${{ github.event.pull_request.number }}
cp -r ./dist/* preview/pr-${{ github.event.pull_request.number }}/ || cp -r ./* preview/pr-${{ github.event.pull_request.number }}/
# Copies from `dist` if a build step exists, otherwise copies all files (excluding .git, .github)
rsync -av --exclude='.git' --exclude='.github' --exclude='preview' ./ preview/pr-${{ github.event.pull_request.number }}/
# Static site - copy all files except git directories and preview folder

- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./preview/pr-${{ github.event.pull_request.number }}
publish_dir: ./preview
publish_branch: gh-pages
destination_dir: pr-preview/pr-${{ github.event.pull_request.number }}
keep_files: false # Clean up old files to avoid bloat
user_name: 'github-actions[bot]'
user_email: 'github-actions[bot]@users.noreply.github.com'

- name: Get GitHub Pages URL
id: pages
uses: actions/github-script@v7
with:
script: |
const response = await github.rest.repos.getPages({ owner: context.repo.owner, repo: context.repo.repo });
return response.data.html_url;
destination_dir: pr-preview
keep_files: true
user_name: "github-actions[bot]"
user_email: "github-actions[bot]@users.noreply.github.com"
force_orphan: true

- name: Comment PR with preview URL
uses: marocchino/sticky-pull-request-comment@v2
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
header: preview-url
message: |
πŸš€ PR Preview: ${{ steps.pages.outputs.result }}/pr-preview/pr-${{ github.event.pull_request.number }}/
πŸš€ **PR Preview Deployed!**

πŸ“± **Preview URL:** https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pr-preview/pr-${{ github.event.pull_request.number }}/

✨ Your changes are now live! Click the link above to test your CodeClip enhancements.
Loading
Loading