Skip to content

Instantly share code, notes, and snippets.

@tknickman
Last active January 2, 2024 04:09
Show Gist options
  • Save tknickman/bc63ec30bec6366e1552ff8a37299db5 to your computer and use it in GitHub Desktop.
Save tknickman/bc63ec30bec6366e1552ff8a37299db5 to your computer and use it in GitHub Desktop.

Vercel Ignore Build Step Example

NOTE: I no longer use this script since the release of turbo-ignore (More details)

Example of a script called by vercel Ignored Build Step option to only build application in monorepo when changes have been detected.


node ../../scripts/ignore-step.js

More details available here

const childProcess = require("child_process");
// https://vercel.com/support/articles/how-do-i-use-the-ignored-build-step-field-on-vercel
const ABORT_BUILD_CODE = 0;
const CONTINUE_BUILD_CODE = 1;
const continueBuild = () => process.exit(CONTINUE_BUILD_CODE);
const abortBuild = () => process.exit(ABORT_BUILD_CODE);
const app = process.argv[2] || path.basename(path.resolve());
const stepCheck = () => {
// no app name (directory) was passed in via args
if (!app) {
return abortBuild();
}
// get all file names changed in last commit
const fileNameList = childProcess
.execSync("git diff --name-only HEAD~1")
.toString()
.trim()
.split("\n");
// check if any files in the app, or in any shared packages have changed
const shouldBuild = fileNameList.some(
(file) => file.startsWith(`apps/${app}`) || file.startsWith("packages/")
);
if (shouldBuild) {
return continueBuild();
}
return abortBuild();
};
stepCheck();
@paul-vd
Copy link

paul-vd commented Mar 12, 2022

Thanks, this is just wat I was looking for, just one thing on line L25 the app is referenced but the variable is never set

  const app = process.argv[2];
  const shouldBuild = fileNameList.some(
    (file) => file.startsWith(`apps/${app}`) || file.startsWith("packages/")
  );

@tknickman
Copy link
Author

Ah yes, thanks @paul-vd ! I removed this by accident when generalizing this for the gist - I’ll get that fixed! But the intention is for the app var to be pulled from the command line as described in the README above.

Here is how I’m using this in my own monorepo:
https://github.com/tknickman/personal/blob/main/scripts/ignore-step.js

@IsraelOrtuno
Copy link

Amazing script, so useful!

Improved it a bit for my use case by just assuming the current folder name as app name if no argument provided:

const path = require('path');

const app = process.argv[2] || path.basename(path.resolve());

@tknickman
Copy link
Author

Great idea @IsraelOrtuno, updated!

@tknickman
Copy link
Author

NOTE: If using Turborepo (like I am in this repo) - you can now just use npx turbo-ignore as your ignored build step and it figures out if your app or any of it's dependencies have changed automatically:

https://vercel.com/changelog/intelligent-ignored-builds-using-turborepo

@cjroebuck
Copy link

cjroebuck commented May 2, 2023

This modification checks against the previous successful deploy commit SHA, which is provided by vercel now. (https://vercel.com/docs/concepts/projects/environment-variables/system-environment-variables)

const fileNameList = childProcess
    .execSync(
      `git diff ${process.env.VERCEL_GIT_PREVIOUS_SHA} HEAD --name-only ./`
    )
    .toString()
    .trim()
    .split("\n");

@Youhan
Copy link

Youhan commented Jan 2, 2024

Just wanted to say thank you!
and if you are using "type": "module", in your package.json file, then rename the file to ignore-step.cjs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment