Skip to content

Instantly share code, notes, and snippets.

@lukasholzer
Created May 31, 2023 08:06
Show Gist options
  • Save lukasholzer/8bb52fe170ec81588a3e28a1872b219e to your computer and use it in GitHub Desktop.
Save lukasholzer/8bb52fe170ec81588a3e28a1872b219e to your computer and use it in GitHub Desktop.
Netlify affected builds plugin for Nx
// 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`);
}
};
# file located under: plugins/affected-build/manifest.yaml
name: affected-build
[[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