Stop Vercel preview deployments on Dependabot PRs
Asked Answered
G

3

6

I want to stop Vercel from creating preview deployments for the dependabot pull requests.

In Vercel, in the Ignored Build Step I've tried this:

bash vercel.sh

and in my repo, the vercel.sh file looks like this:

#!/bin/bash

echo "VERCEL_ENV: $VERCEL_ENV"

# check branch name
BRANCH=$(git rev-parse --abbrev-ref HEAD)
echo "BRANCH: $BRANCH"

# check if branch name does not contain "Bump" (every dependabot PR starts with this)
if [[ $BRANCH != *"Bump"* ]]; then
  exit 1
fi

exit 0

What am I missing? The deployment still went through.

Also tried writing this right to the Ignored Build Step

if [ "$VERCEL_GIT_COMMIT_AUTHOR_LOGIN" == "dependabot" ]; then exit 0; else exit 1; fi

Still created the deployment.

Tried it this way too

if (process.env.VERCEL_GIT_COMMIT_AUTHOR_LOGIN === "dependabot") {
  process.exit(0);
} else {
  process. Exit(1);
}

and then calling it like node ignore-nuild.js in the Ignored Build Step, but this didn't help either.

Update

My bad, it was "dependabot[bot]", not just "dependabot".

Granddaughter answered 25/1, 2023 at 9:40 Comment(0)
H
1

First, always surround with quotes a variable you want to echo. That way, you can catch the invisible space which might plague its value:

echo "BRANCH: '${BRANCH}'"
             ^^^       ^^^

Second, you can add echo before the exit, to differentiate when you exit 1 from exit 0.

echo "continue"
exit 0

Third, you can try, for testing:

if [[ "${BRANCH#Bump}" != "${BRANCH}" ]]; then ...

If $BRANCH starts with Bump, then the condition will be true.


The OP suggests:

if (process.env.VERCEL_GIT_COMMIT_AUTHOR_LOGIN === "dependabot[bot]") {
  process.exit(0);
} else {
  process. Exit(1);
}
Heterochromosome answered 31/1, 2023 at 20:14 Comment(0)
B
2
  1. Navigate to your project settings: https://vercel.com/user/slug/settings/git

  2. Update the build ignore step to "custom" and enter:

[ "$VERCEL_GIT_COMMIT_AUTHOR_LOGIN" == "dependabot[bot]" ]

image of the above steps

Bunch answered 7/8, 2023 at 18:55 Comment(0)
H
1

First, always surround with quotes a variable you want to echo. That way, you can catch the invisible space which might plague its value:

echo "BRANCH: '${BRANCH}'"
             ^^^       ^^^

Second, you can add echo before the exit, to differentiate when you exit 1 from exit 0.

echo "continue"
exit 0

Third, you can try, for testing:

if [[ "${BRANCH#Bump}" != "${BRANCH}" ]]; then ...

If $BRANCH starts with Bump, then the condition will be true.


The OP suggests:

if (process.env.VERCEL_GIT_COMMIT_AUTHOR_LOGIN === "dependabot[bot]") {
  process.exit(0);
} else {
  process. Exit(1);
}
Heterochromosome answered 31/1, 2023 at 20:14 Comment(0)
U
0

The documentation states

. If the command returns "0", the build will be skipped. If, however, a code "1" or greater is returned, then a new deployment will be built.

Therefore I created a script that returns 1 if the build is on the production branch or if not only returns 1 if the users is not the dependency bot (I am not using dependabot)

if [ "$VERCEL_ENV" == "production" ] || [ "$VERCEL_GIT_COMMIT_AUTHOR_LOGIN" != "my-upgrade-bot[bot]" ]; then exit 1; else exit 0; fi

You can find an overview of environment variables here https://vercel.com/docs/projects/environment-variables/system-environment-variables

The [bot] suffix seems to be added to the regular bot name that you see on github.

Unmentionable answered 21/8 at 9:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.