-
Notifications
You must be signed in to change notification settings - Fork 0
152 lines (130 loc) · 5.31 KB
/
Copy pathnextjs-static-gh-pages.yml
File metadata and controls
152 lines (130 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
name: 🚀 Deploy Static Next.js to GitHub Pages
on:
push:
branches: ['**'] # Triggers on push to any branch
pull_request:
types: [opened, synchronize, reopened]
workflow_dispatch: # Allows manual triggering
permissions:
contents: write
pull-requests: write
concurrency:
group: 'pages-${{ github.head_ref || github.ref_name }}'
cancel-in-progress: true
jobs:
build-and-deploy:
name: 🏗 Build & Deploy
runs-on: ubuntu-latest
steps:
- name: 🔍 Checkout repository
uses: actions/checkout@v4
- name: 🔧 Set deployment variables
id: vars
run: |
REPO_NAME="${{ github.event.repository.name }}"
# Custom domain means no repo name prefix in the URL path.
# Set CUSTOM_DOMAIN to your domain, or leave empty to use github.io/<repo> URLs.
CUSTOM_DOMAIN="dev.codebuilder.org"
# Get the branch name (works for both push and PR events)
if [ "${{ github.event_name }}" = "pull_request" ]; then
BRANCH="${{ github.head_ref }}"
else
BRANCH="${{ github.ref_name }}"
fi
# Sanitize branch name for use in URL paths
SAFE_BRANCH=$(echo "$BRANCH" | sed 's/[^a-zA-Z0-9._-]/-/g')
if [ -n "$CUSTOM_DOMAIN" ]; then
BASE_URL="https://${CUSTOM_DOMAIN}"
else
BASE_URL="https://${{ github.repository_owner }}.github.io/${REPO_NAME}"
fi
if [ "$BRANCH" = "main" ]; then
if [ -n "$CUSTOM_DOMAIN" ]; then
echo "base_path=" >> $GITHUB_OUTPUT
else
echo "base_path=/${REPO_NAME}" >> $GITHUB_OUTPUT
fi
echo "dest_dir=" >> $GITHUB_OUTPUT
echo "keep_files=false" >> $GITHUB_OUTPUT
echo "preview_url=${BASE_URL}/" >> $GITHUB_OUTPUT
else
echo "base_path=/preview/${SAFE_BRANCH}" >> $GITHUB_OUTPUT
echo "dest_dir=preview/${SAFE_BRANCH}" >> $GITHUB_OUTPUT
echo "keep_files=true" >> $GITHUB_OUTPUT
echo "preview_url=${BASE_URL}/preview/${SAFE_BRANCH}/" >> $GITHUB_OUTPUT
fi
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
echo "safe_branch=$SAFE_BRANCH" >> $GITHUB_OUTPUT
- name: 🔎 Detect package manager
id: detect-pm
run: |
if [ -f "pnpm-lock.yaml" ]; then
echo "manager=pnpm" >> $GITHUB_OUTPUT
echo "command=install" >> $GITHUB_OUTPUT
echo "runner=pnpm exec" >> $GITHUB_OUTPUT
else
echo "manager=npm" >> $GITHUB_OUTPUT
echo "command=ci" >> $GITHUB_OUTPUT
echo "runner=npx --no-install" >> $GITHUB_OUTPUT
fi
- name: 📦 Install pnpm
if: steps.detect-pm.outputs.manager == 'pnpm'
run: npm install -g pnpm
- name: ⚙️ Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '24'
cache: ${{ steps.detect-pm.outputs.manager }}
- name: 🚫 Ephemerally delete server/api files
run: |
echo "Deleting src/app/api, src/server, src/proxy.ts, and src/app/jobs/[id] for static build..."
rm -rf src/app/api src/server src/proxy.ts src/app/jobs/[id] src/app/[...not-found] prisma.config.ts
- name: 📥 Install dependencies
run: ${{ steps.detect-pm.outputs.manager }} ${{ steps.detect-pm.outputs.command }}
- name: 🏗 Generate Static Build
env:
NEXT_OUTPUT_MODE: export
GITHUB_PAGES: 1
NEXT_BASE_PATH: ${{ steps.vars.outputs.base_path }}
run: |
echo "Building static files for GitHub Pages..."
echo "Base path: $NEXT_BASE_PATH"
echo "Preview URL: ${{ steps.vars.outputs.preview_url }}"
pnpm build
touch out/.nojekyll
- name: 🚀 Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./out
destination_dir: ${{ steps.vars.outputs.dest_dir }}
keep_files: ${{ steps.vars.outputs.keep_files }}
- name: 💬 Comment preview URL on PR
if: github.event_name == 'pull_request'
uses: actions/github-script@v7
with:
script: |
const url = '${{ steps.vars.outputs.preview_url }}';
const sha = context.sha.substring(0, 7);
const body = `🚀 **Preview deployment ready!**\n\n📎 **Preview URL:** ${url}\n\n_Deployed from commit \`${sha}\`_`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(c => c.body.includes('Preview deployment ready!'));
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}