-
Notifications
You must be signed in to change notification settings - Fork 0
ci: optimize PR CI time and skip Netlify previews for non-docs changes #145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2bb3817
ci: optimize PR CI time and Netlify deploy preview filtering
61cbd2d
ci: use bunx for nx commands and prefer optional chaining
f4cce21
ci: call nx and nx-cloud from node_modules/.bin to lock versions
e47781a
ci: avoid paths-ignore blocking required checks and harden netlify ig…
47b94d0
ci: restrict PATH for git spawn in netlify ignore script
cfe93ff
ci: use absolute git path in netlify ignore script
7d6f9d3
ci: fix docs-only path filter regex to match files inside website/ an…
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| #!/usr/bin/env node | ||
| /** | ||
| * Netlify ignore script for the Docusaurus site under website/. | ||
| * | ||
| * Exit 0 -> skip the build (no deploy preview/production build needed). | ||
| * Exit 1 -> run the build (website/ or netlify.toml changed). | ||
| * | ||
| * The script is executed by Netlify from the `website/` base directory. | ||
| */ | ||
|
|
||
| const { spawnSync } = require('node:child_process'); | ||
| const https = require('node:https'); | ||
|
|
||
| const CONTEXT = process.env.CONTEXT || ''; | ||
| const IS_PULL_REQUEST = process.env.PULL_REQUEST === 'true'; | ||
| const REVIEW_ID = process.env.REVIEW_ID || ''; | ||
| const COMMIT_REF = process.env.COMMIT_REF || ''; | ||
| const CACHED_COMMIT_REF = process.env.CACHED_COMMIT_REF || ''; | ||
| const GITHUB_TOKEN = process.env.GITHUB_TOKEN || ''; | ||
|
|
||
| const REPO = 'abapify/adt-cli'; | ||
|
|
||
| function hasWebsiteRelevantPath(filePath) { | ||
| if (!filePath) return false; | ||
| return filePath === 'netlify.toml' || filePath.startsWith('website/'); | ||
| } | ||
|
|
||
| function gitDiffChangedFiles(refA, refB) { | ||
| const result = spawnSync( | ||
| '/usr/bin/git', | ||
| ['diff', '--name-only', refA, refB, '--', '.', '../netlify.toml'], | ||
| { | ||
| cwd: process.cwd(), | ||
| encoding: 'utf8', | ||
| env: { ...process.env, PATH: '/usr/local/bin:/usr/bin:/bin' }, | ||
| shell: false, | ||
| }, | ||
| ); | ||
| if (result.error || result.status !== 0) { | ||
| return null; | ||
| } | ||
| return result.stdout.trim().split('\n').filter(Boolean); | ||
| } | ||
|
|
||
| function fetchJson(url) { | ||
| return new Promise((resolve, reject) => { | ||
| const options = { headers: { 'User-Agent': 'netlify-ignore-script' } }; | ||
| if (GITHUB_TOKEN) { | ||
| options.headers.Authorization = `token ${GITHUB_TOKEN}`; | ||
| } | ||
| https | ||
| .get(url, options, (res) => { | ||
| if (res.statusCode !== 200) { | ||
| res.resume(); | ||
| reject(new Error(`GitHub API returned ${res.statusCode}`)); | ||
| return; | ||
| } | ||
| let data = ''; | ||
| res.on('data', (chunk) => { | ||
| data += chunk; | ||
| }); | ||
| res.on('end', () => { | ||
| try { | ||
| resolve(JSON.parse(data)); | ||
| } catch (err) { | ||
| reject(err); | ||
| } | ||
| }); | ||
| }) | ||
| .on('error', reject); | ||
| }); | ||
| } | ||
|
|
||
| async function prTouchesWebsite() { | ||
| const reviewId = Number(REVIEW_ID); | ||
| if (!Number.isInteger(reviewId) || reviewId <= 0) { | ||
| throw new Error(`Invalid REVIEW_ID: ${REVIEW_ID}`); | ||
| } | ||
| const pageSize = 100; | ||
| let page = 1; | ||
| while (true) { | ||
| const files = await fetchJson( | ||
| `https://api.github.com/repos/${REPO}/pulls/${reviewId}/files?per_page=${pageSize}&page=${page}`, | ||
| ); | ||
| if (!Array.isArray(files) || files.length === 0) return false; | ||
| for (const file of files) { | ||
| if (hasWebsiteRelevantPath(file.filename)) return true; | ||
| } | ||
| if (files.length < pageSize) return false; | ||
| page += 1; | ||
| } | ||
| } | ||
|
|
||
| async function main() { | ||
| // Fast path: we have two distinct cached refs. If nothing in website/ or | ||
| // netlify.toml changed, skip the build. | ||
| if (COMMIT_REF && CACHED_COMMIT_REF && CACHED_COMMIT_REF !== COMMIT_REF) { | ||
| const files = gitDiffChangedFiles(CACHED_COMMIT_REF, COMMIT_REF); | ||
| if (files?.length > 0) { | ||
| console.log( | ||
| `Building: ${files.length} change(s) in website/ or netlify.toml`, | ||
| ); | ||
| process.exit(1); | ||
| } | ||
| if (files?.length === 0) { | ||
| console.log( | ||
| 'Skipping: no website/ or netlify.toml changes since the last build', | ||
| ); | ||
| process.exit(0); | ||
| } | ||
| // git diff failed; fall through to PR API or default build. | ||
| } | ||
|
ThePlenkov marked this conversation as resolved.
|
||
|
|
||
| // For pull/merge request previews, ask GitHub which files changed. | ||
| if ((IS_PULL_REQUEST || CONTEXT === 'deploy-preview') && REVIEW_ID) { | ||
| try { | ||
| const touches = await prTouchesWebsite(); | ||
| if (!touches) { | ||
| console.log( | ||
| 'Skipping deploy preview: PR does not touch website/ or netlify.toml', | ||
| ); | ||
| process.exit(0); | ||
| } | ||
| console.log( | ||
| 'Building deploy preview: PR touches website/ or netlify.toml', | ||
| ); | ||
| process.exit(1); | ||
| } catch (err) { | ||
| // If we cannot determine the changed files, build to be safe. | ||
| console.warn( | ||
| `Could not determine PR changed files (${err.message}); building to be safe`, | ||
| ); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| // Production / branch-deploy without useful cached refs or PR info: build. | ||
| console.log('Building: no cached diff or PR information available'); | ||
| process.exit(1); | ||
| } | ||
|
ThePlenkov marked this conversation as resolved.
|
||
|
|
||
| main(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.