Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
76 changes: 76 additions & 0 deletions .github/workflows/pr-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: PR Preview (GitHub Pages)

on:
pull_request:
types: [opened, synchronize, reopened, closed]

permissions:
contents: write
pull-requests: write

jobs:
preview:
if: github.event.action != 'closed'
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: "20"
cache: "npm"

- name: Install dependencies
run: npm ci

- name: Build with dynamic BasePath
run: npm run build
env:
# Informs Next.js that it is hosted at /repo-name/pr-number/
BASE_PATH: /${{ github.event.repository.name }}/pr-${{ github.event.pull_request.number }}

- name: Prepare preview folder
run: |
mkdir -p preview/pr-${{ github.event.pull_request.number }}
cp -r out/* preview/pr-${{ github.event.pull_request.number }}

- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: preview
keep_files: true # Prevents the main site and other PRs from being deleted

- name: Comment preview URL
uses: actions/github-script@v7
with:
script: |
const url = `https://${context.repo.owner}.github.io/${context.repo.repo}/pr-${context.payload.pull_request.number}/`;
github.rest.issues.createComment({
issue_number: context.payload.pull_request.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `**Preview deployed:**\n${url}`
})

cleanup:
if: github.event.action == 'closed'
runs-on: ubuntu-latest
steps:
- name: Checkout gh-pages
uses: actions/checkout@v4
with:
ref: gh-pages

- name: Remove preview folder
run: |
rm -rf pr-${{ github.event.pull_request.number }}

- name: Commit and Push cleanup
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: .
36 changes: 36 additions & 0 deletions .github/workflows/vercel-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Vercel Preview Deployment
env:
VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
on:
pull_request:
branches: ["main"]

Comment thread
aniket866 marked this conversation as resolved.
Outdated
jobs:
deploy-preview:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install Vercel CLI
run: npm install --global vercel@latest

- name: Pull Vercel Environment Information
run: vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }}

- name: Build Project Artifacts
run: vercel build --token=${{ secrets.VERCEL_TOKEN }}

- name: Deploy Project Artifacts to Vercel
id: deploy
run: |
COMMAND_OUTPUT=$(vercel deploy --prebuilt --token=${{ secrets.VERCEL_TOKEN }})
echo "preview_url=$COMMAND_OUTPUT" >> $GITHUB_OUTPUT

- name: Comment on PR
uses: thollander/actions-comment-pull-request@v2
with:
message: |
Vercel Preview Deployment is ready!
**URL**: ${{ steps.deploy.outputs.preview_url }}
3 changes: 3 additions & 0 deletions next.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { NextConfig } from "next";

const basePath = process.env.BASE_PATH || "";

const nextConfig: NextConfig = {
/* config options here */
output: "export", // Required for GitHub Pages
basePath: basePath,
images: {
unoptimized: true, // Required for static export
},
Expand Down
Loading