Created
May 31, 2023 08:06
-
-
Save lukasholzer/8bb52fe170ec81588a3e28a1872b219e to your computer and use it in GitHub Desktop.
Netlify affected builds plugin for Nx
This file contains 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
// file located under: plugins/affected-build/index.mjs | |
/** @type {import('@netlify/build').OnPreBuild} */ | |
export const onPreBuild = async ({ utils }) => { | |
const prevCommitRef = process.env.CACHED_COMMIT_REF; | |
const currentCommitRef = process.env.COMMIT_REF; | |
const isInitialBuild = currentCommitRef === prevCommitRef; // check if it's the initial build | |
const projectName = process.env.PROJECT_NAME; // can be set through an env variable in the `netlify.toml` or UI. | |
const { stdout } = await utils.run.command(`nx print-affected --base=${prevCommitRef}`, { stdio: 'pipe', preferLocal: true }); | |
const { projects = [] } = JSON.parse(stdout.toString()); | |
const isAffected = projects.find((project) => project === projectName); | |
// TODO: you can fine grain the affected logic here for other things like CMS inputs | |
if (!isAffected && !isInitialBuild) { | |
utils.build.cancelBuild(`Project ${projectName} is not affected`); | |
} | |
}; |
This file contains 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
# file located under: plugins/affected-build/manifest.yaml | |
name: affected-build |
This file contains 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
[[plugins]] | |
package = "/plugins/affected-build/index.mjs" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment