Skip to content

Commit 893ecdf

Browse files
committed
ci: update Next.js deployment workflow for GitHub Pages
- Replace old deploy.yml with new nextjs.yml workflow - Configure Next.js for static export with basePath support - Add proper caching and package manager detection - Update deployment steps to match Next.js requirements
1 parent ae14f49 commit 893ecdf

File tree

3 files changed

+102
-54
lines changed

3 files changed

+102
-54
lines changed

.github/workflows/deploy.yml

Lines changed: 0 additions & 53 deletions
This file was deleted.

.github/workflows/nextjs.yml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
name: Deploy Next.js site to 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 }}/package.json" ]; then
33+
echo "manager=npm" >> $GITHUB_OUTPUT
34+
echo "command=ci" >> $GITHUB_OUTPUT
35+
echo "runner=npx --no-install" >> $GITHUB_OUTPUT
36+
exit 0
37+
else
38+
echo "Unable to determine package manager"
39+
exit 1
40+
fi
41+
42+
- name: Setup Node
43+
uses: actions/setup-node@v4
44+
with:
45+
node-version: "20"
46+
cache: ${{ steps.detect-package-manager.outputs.manager }}
47+
48+
- name: Setup Pages
49+
uses: actions/configure-pages@v5
50+
with:
51+
static_site_generator: next
52+
53+
- name: Restore cache
54+
uses: actions/cache@v4
55+
with:
56+
path: |
57+
.next/cache
58+
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
59+
restore-keys: |
60+
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}-
61+
62+
- name: Install dependencies
63+
run: ${{ steps.detect-package-manager.outputs.manager }} ${{ steps.detect-package-manager.outputs.command }}
64+
65+
- name: Build with Next.js
66+
run: ${{ steps.detect-package-manager.outputs.runner }} next build
67+
68+
- name: Static export (ensure out/ exists)
69+
run: ${{ steps.detect-package-manager.outputs.runner }} next export
70+
71+
- name: Upload artifact
72+
uses: actions/upload-pages-artifact@v3
73+
with:
74+
path: ./out
75+
76+
deploy:
77+
environment:
78+
name: github-pages
79+
url: ${{ steps.deployment.outputs.page_url }}
80+
runs-on: ubuntu-latest
81+
needs: build
82+
steps:
83+
- name: Deploy to GitHub Pages
84+
id: deployment
85+
uses: actions/deploy-pages@v4

next.config.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
import type { NextConfig } from "next";
22

3+
// Optional base path for Project Pages (https://<user>.github.io/<repo>)
4+
// Set via env: NEXT_PUBLIC_BASE_PATH="/<repo>"
5+
const basePath = process.env.NEXT_PUBLIC_BASE_PATH || "";
6+
37
const nextConfig: NextConfig = {
4-
/* config options here */
8+
// Generate a static export for GitHub Pages
9+
output: "export",
10+
11+
// Apply basePath only when provided (for Project Pages). For User/Org Pages, leave empty.
12+
...(basePath
13+
? {
14+
basePath,
15+
assetPrefix: `${basePath}/`,
16+
}
17+
: {}),
18+
19+
// If you use next/image, set unoptimized for static export.
20+
images: { unoptimized: true },
521
};
622

723
export default nextConfig;

0 commit comments

Comments
 (0)