Skip to content

Commit a39ebd3

Browse files
committed
Enhance GitHub Pages deployment support in Next.js configuration and add GitHub Actions workflow for deployment.
- Updated `next.config.mjs` to enable static export for GitHub Pages and maintain backward compatibility for production builds. - Introduced `.github/workflows/deploy-github-pages.yml` for automated deployment to GitHub Pages. - Modified existing workflow in `.github/workflows/nextjs-publish.yml` to ignore API changes and support pnpm setup.
1 parent fbf9446 commit a39ebd3

4 files changed

Lines changed: 117 additions & 2 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: Deploy to GitHub Pages
2+
3+
on:
4+
push:
5+
branches: [main]
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
pages: write
11+
id-token: write
12+
13+
concurrency:
14+
group: "pages"
15+
cancel-in-progress: false
16+
17+
jobs:
18+
build:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Checkout 🛎️
22+
uses: actions/checkout@v4
23+
24+
- name: Detect package manager
25+
id: detect-package-manager
26+
run: |
27+
if [ -f "${{ github.workspace }}/yarn.lock" ]; then
28+
echo "manager=yarn" >> $GITHUB_OUTPUT
29+
echo "command=install" >> $GITHUB_OUTPUT
30+
echo "runner=yarn" >> $GITHUB_OUTPUT
31+
exit 0
32+
elif [ -f "${{ github.workspace }}/pnpm-lock.yaml" ]; then
33+
echo "manager=pnpm" >> $GITHUB_OUTPUT
34+
echo "command=install" >> $GITHUB_OUTPUT
35+
echo "runner=pnpm" >> $GITHUB_OUTPUT
36+
exit 0
37+
elif [ -f "${{ github.workspace }}/package.json" ]; then
38+
echo "manager=npm" >> $GITHUB_OUTPUT
39+
echo "command=ci" >> $GITHUB_OUTPUT
40+
echo "runner=npx --no-install" >> $GITHUB_OUTPUT
41+
exit 0
42+
else
43+
echo "Unable to determine package manager"
44+
exit 1
45+
fi
46+
47+
- name: Setup pnpm
48+
if: steps.detect-package-manager.outputs.manager == 'pnpm'
49+
run: npm install -g pnpm
50+
51+
- name: Setup Node.js ⚙️
52+
uses: actions/setup-node@v4
53+
with:
54+
node-version: "20"
55+
cache: ${{ steps.detect-package-manager.outputs.manager }}
56+
57+
- name: Install dependencies 🔧
58+
run: ${{ steps.detect-package-manager.outputs.manager }} ${{ steps.detect-package-manager.outputs.command }}
59+
60+
- name: Setup Pages ⚙️
61+
uses: actions/configure-pages@v4
62+
with:
63+
static_site_generator: next
64+
65+
- name: Build with Next.js for GitHub Pages 🏗️
66+
run: DEPLOY_TARGET=github-pages ${{ steps.detect-package-manager.outputs.runner }} run build
67+
env:
68+
NODE_ENV: production
69+
70+
- name: Upload artifact 📡
71+
uses: actions/upload-pages-artifact@v3
72+
with:
73+
path: ./out
74+
75+
deploy:
76+
environment:
77+
name: github-pages
78+
url: ${{ steps.deployment.outputs.page_url }}
79+
runs-on: ubuntu-latest
80+
needs: build
81+
steps:
82+
- name: Deploy to GitHub Pages 🚀
83+
id: deployment
84+
uses: actions/deploy-pages@v4

.github/workflows/nextjs-publish.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88
# Runs on pushes targeting the default branch
99
push:
1010
branches: ["main"]
11+
paths-ignore: ['api/**'] # Don't trigger on API changes
1112

1213
# Allows you to run this workflow manually from the Actions tab
1314
workflow_dispatch:
@@ -40,6 +41,11 @@ jobs:
4041
echo "command=install" >> $GITHUB_OUTPUT
4142
echo "runner=yarn" >> $GITHUB_OUTPUT
4243
exit 0
44+
elif [ -f "${{ github.workspace }}/pnpm-lock.yaml" ]; then
45+
echo "manager=pnpm" >> $GITHUB_OUTPUT
46+
echo "command=install" >> $GITHUB_OUTPUT
47+
echo "runner=pnpm" >> $GITHUB_OUTPUT
48+
exit 0
4349
elif [ -f "${{ github.workspace }}/package.json" ]; then
4450
echo "manager=npm" >> $GITHUB_OUTPUT
4551
echo "command=ci" >> $GITHUB_OUTPUT
@@ -50,6 +56,10 @@ jobs:
5056
exit 1
5157
fi
5258
59+
- name: Setup pnpm
60+
if: steps.detect-package-manager.outputs.manager == 'pnpm'
61+
run: npm install -g pnpm
62+
5363
- name: Setup Node
5464
uses: actions/setup-node@v4
5565
with:

next.config.mjs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
/** @type {import('next').NextConfig} */
22
const nextConfig = {
3-
// Only use export for production builds
4-
...(process.env.NODE_ENV === "production" && { output: "export" }),
3+
// Enable static export for GitHub Pages deployment
4+
...(process.env.DEPLOY_TARGET === "github-pages" && {
5+
output: "export",
6+
trailingSlash: true,
7+
// Add basePath if deploying to a subdirectory (uncomment and modify if needed)
8+
// basePath: "/your-repo-name",
9+
}),
10+
11+
// Keep existing production export for backward compatibility
12+
...(process.env.NODE_ENV === "production" &&
13+
process.env.DEPLOY_TARGET !== "github-pages" && {
14+
output: "export",
15+
}),
16+
517
images: { unoptimized: true },
18+
19+
// Disable server-side features when building for GitHub Pages
20+
...(process.env.DEPLOY_TARGET === "github-pages" && {
21+
// Ensure no server-side features are used in static export
22+
experimental: {
23+
missingSuspenseWithCSRBailout: false,
24+
},
25+
}),
626
};
727

828
export default nextConfig;

public/.nojekyll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)