|
1 | 1 | // We only want Netlify to build the site if a PR changes files in this |
2 | 2 | // directory (./docs). |
3 | 3 | // 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. |
5 | 6 |
|
6 | 7 | import { execSync } from 'node:child_process' |
7 | 8 |
|
8 | 9 | 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') { |
15 | 11 | 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. |
16 | 15 | 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 |
19 | 20 | return |
| 21 | + } catch { |
| 22 | + // Continue to check what files changed |
20 | 23 | } |
21 | 24 | } |
22 | 25 |
|
23 | 26 | // Query the GithHub API to get the changed files in the PR |
24 | 27 | // See below for REVIEW_ID |
25 | 28 | // 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}` |
27 | 34 | const resp = await fetch(url, { |
28 | 35 | headers: { |
29 | | - Authorization: `Bearer ${process.env.RW_GITHUB_TOKEN}`, |
| 36 | + Authorization: `Bearer ${process.env.CEDAR_GITHUB_TOKEN}`, |
30 | 37 | ['X-GitHub-Api-Version']: '2022-11-28', |
31 | 38 | Accept: 'application/vnd.github+json', |
32 | 39 | }, |
33 | 40 | }) |
34 | 41 | 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) |
36 | 44 |
|
37 | 45 | console.log({ |
38 | 46 | changedFiles, |
|
0 commit comments