Skip to content

Commit 63fb7e0

Browse files
committed
Add GitHub Actions workflow for deploying Next.js site to GitHub Pages
1 parent a85f351 commit 63fb7e0

1 file changed

Lines changed: 100 additions & 0 deletions

File tree

.github/workflows/nextjs.yml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Sample workflow for building and deploying a Next.js site to GitHub Pages
2+
#
3+
# To get started with Next.js see: https://nextjs.org/docs/getting-started
4+
#
5+
name: Deploy Next.js site to Pages
6+
7+
on:
8+
# Runs on pushes targeting the default branch
9+
push:
10+
branches: ["main"]
11+
12+
# Allows you to run this workflow manually from the Actions tab
13+
workflow_dispatch:
14+
15+
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
16+
permissions:
17+
contents: read
18+
pages: write
19+
id-token: write
20+
21+
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
22+
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
23+
concurrency:
24+
group: "pages"
25+
cancel-in-progress: false
26+
27+
jobs:
28+
# Build job
29+
build:
30+
runs-on: ubuntu-latest
31+
defaults:
32+
run:
33+
working-directory: project_page
34+
steps:
35+
- name: Checkout
36+
uses: actions/checkout@v4
37+
- name: Detect package manager
38+
id: detect-package-manager
39+
run: |
40+
if [ -f "yarn.lock" ]; then
41+
echo "manager=yarn" >> $GITHUB_OUTPUT
42+
echo "command=install" >> $GITHUB_OUTPUT
43+
echo "runner=yarn" >> $GITHUB_OUTPUT
44+
exit 0
45+
elif [ -f "package.json" ]; then
46+
echo "manager=npm" >> $GITHUB_OUTPUT
47+
echo "command=ci" >> $GITHUB_OUTPUT
48+
echo "runner=npx --no-install" >> $GITHUB_OUTPUT
49+
exit 0
50+
else
51+
echo "Unable to determine package manager"
52+
exit 1
53+
fi
54+
- name: Setup Node
55+
uses: actions/setup-node@v4
56+
with:
57+
node-version: "20"
58+
cache: ${{ steps.detect-package-manager.outputs.manager }}
59+
cache-dependency-path: |
60+
project_page/package-lock.json
61+
project_page/yarn.lock
62+
- name: Setup Pages
63+
uses: actions/configure-pages@v5
64+
with:
65+
# Automatically inject basePath in your Next.js configuration file and disable
66+
# server side image optimization (https://nextjs.org/docs/api-reference/next/image#unoptimized).
67+
#
68+
# You may remove this line if you want to manage the configuration yourself.
69+
static_site_generator: next
70+
- name: Restore cache
71+
uses: actions/cache@v4
72+
with:
73+
path: |
74+
project_page/.next/cache
75+
# Generate a new cache whenever packages or source files change.
76+
key: ${{ runner.os }}-nextjs-${{ hashFiles('project_page/**/package-lock.json', 'project_page/**/yarn.lock') }}-${{ hashFiles('project_page/**.[jt]s', 'project_page/**.[jt]sx') }}
77+
# If source files changed but packages didn't, rebuild from a prior cache.
78+
restore-keys: |
79+
${{ runner.os }}-nextjs-${{ hashFiles('project_page/**/package-lock.json', 'project_page/**/yarn.lock') }}-
80+
- name: Install dependencies
81+
run: ${{ steps.detect-package-manager.outputs.manager }} ${{ steps.detect-package-manager.outputs.command }}
82+
- name: Build with Next.js
83+
run: ${{ steps.detect-package-manager.outputs.runner }} next build
84+
85+
- name: Upload artifact
86+
uses: actions/upload-pages-artifact@v3
87+
with:
88+
path: project_page/out
89+
90+
# Deployment job
91+
deploy:
92+
environment:
93+
name: github-pages
94+
url: ${{ steps.deployment.outputs.page_url }}
95+
runs-on: ubuntu-latest
96+
needs: build
97+
steps:
98+
- name: Deploy to GitHub Pages
99+
id: deployment
100+
uses: actions/deploy-pages@v4

0 commit comments

Comments
 (0)