Skip to content

Commit b414ae4

Browse files
committed
chore(docs): Enable ignore_build.mjs to be smarter about when building docs
1 parent bd80e08 commit b414ae4

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

docs/ignore_build.mjs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,46 @@
11
// We only want Netlify to build the site if a PR changes files in this
22
// directory (./docs).
33
// See https://docs.netlify.com/configure-builds/ignore-builds.
4-
// Netlify runs this via Node.js v18.
4+
// Netlify runs this via Node.js v22 (even though their docs say Node 18).
5+
// If the exit-code is 0, the build will be ignored.
56

67
import { execSync } from 'node:child_process'
78

89
async function main() {
9-
const branch = process.env.BRANCH
10-
11-
// Reproduce the default behavior for main.
12-
// See https://docs.netlify.com/configure-builds/ignore-builds/#mimic-default-behavior.
13-
// `execSync` throws if the process times out or has a non-zero exit code.
14-
if (branch === 'main') {
10+
if (process.env.BRANCH === 'main') {
1511
try {
12+
// Reproduce the default behavior for main.
13+
// See https://docs.netlify.com/configure-builds/ignore-builds/#mimic-default-behavior.
14+
// `execSync` throws if the process times out or has a non-zero exit code.
1615
execSync('git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF')
17-
} catch (error) {
18-
process.exitCode = 1
16+
// If we come here, git diff exited with code 0, which means that there
17+
// were no changes since the last cached build. So no need to build -> We
18+
// can exit with 0 to ignore this build.
19+
process.exitCode = 0
1920
return
21+
} catch {
22+
// Continue to check what files changed
2023
}
2124
}
2225

2326
// Query the GithHub API to get the changed files in the PR
2427
// See below for REVIEW_ID
2528
// https://docs.netlify.com/configure-builds/environment-variables/#git-metadata
26-
const url = `https://api.github.com/repos/cedarjs/cedar/pulls/${process.env.REVIEW_ID}/files?per_page=100`
29+
// If we don't have a review ID the best we can do is check the commit (this
30+
// happens when we're committing straight to main)
31+
const url = process.env.REVIEW_ID
32+
? `https://api.github.com/repos/cedarjs/cedar/pulls/${process.env.REVIEW_ID}/files?per_page=100`
33+
: `https://api.github.com/repos/cedarjs/cedar/commits/${process.env.COMMIT_REF}`
2734
const resp = await fetch(url, {
2835
headers: {
29-
Authorization: `Bearer ${process.env.RW_GITHUB_TOKEN}`,
36+
Authorization: `Bearer ${process.env.CEDAR_GITHUB_TOKEN}`,
3037
['X-GitHub-Api-Version']: '2022-11-28',
3138
Accept: 'application/vnd.github+json',
3239
},
3340
})
3441
const json = await resp.json()
35-
const changedFiles = json.map((file) => file.filename)
42+
// Account for both PRs and commits in the returned json object
43+
const changedFiles = (json.files || json).map((file) => file.filename)
3644

3745
console.log({
3846
changedFiles,

docs/netlify.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
base = "docs"
33
publish = "build"
44
command = "yarn build"
5-
# ignore = "node ignore_build.mjs"
5+
ignore = "node ignore_build.mjs"
66

77
[build.environment]
88
NODE_OPTIONS = "--max-old-space-size=4096"

0 commit comments

Comments
 (0)