Automatically deploy branches that match name pattern

Really nice, thanks so much for sharing this for other folks who want to implement something similar

2 Likes

I just realised I hadn’t spotted an issue as I was originally testing this under the branch deploy context and in that case $BRANCH returns the actual name of the branch like cms/blog/2020-my-blog-name but when I run this under the deploy preview context then $BRANCH responds with something like pull/1234/head so the grep didn’t work.

I came across this page about available environment variables and tried out the $HEAD variable which worked for me in deploy preview context. Just thought I’d mention in case someone had the same issue.

2 Likes

@fool We set up a file called netlify-build-ignore.sh with the following contents:

#!/bin/bash

if echo $HEAD | grep "^module";
  then return 1
else if echo $HEAD | grep "^component";
  then return 1
else
  return 0
fi

The goal is to only build previews when the branch name contains “module” or “component”.
In netlify.toml we have:

[context.deploy-preview]
  ignore = './netlify-build-ignore.sh'

When the build runs, there is a permissions error: sh: 1: ./netlify-build-ignore.sh: Permission denied

How can we get an ignore script running from netlify.toml?

1 Like

Could you try bash ./netlify-build-ignore.sh as your command instead? Git doesn’t usefully save permissions so you can’t run an arbitrary text file like that, but bash ... can :slight_smile:

1 Like

Use exit 1 or exit 0 instead of return

I am trying to skip some deploy previews but not matter what I return from the ignore command, the build goes on.

I have first tried:

[context.deploy-preview]
  command = "yarn bootstrap && yarn build:web:staging"
  ignore =  "exit 0"

and then

[context.deploy-preview]
  command = "yarn bootstrap && yarn build:web:staging"
  ignore =  "exit 1"

In both cases, the build continues:

3:23:16 PM: Detected ignore command in Netlify configuration file. Proceeding with the specified command: 'exit 0'
3:23:16 PM: Starting build script
3:23:16 PM: Installing dependencies
3:23:16 PM: Python version set to 2.7
3:23:17 PM: Started restoring cached node version
3:23:20 PM: Finished restoring cached node version

and

3:26:46 PM: Detected ignore command in Netlify configuration file. Proceeding with the specified command: 'exit 1'
3:26:47 PM: Starting build script
3:26:47 PM: Installing dependencies
3:26:47 PM: Python version set to 2.7
3:26:47 PM: Started restoring cached node version

@fool if you want to check the logs, it’s here and here. (They are marked canceled because I manually canceled them later).

What am I doing wrong?

Hi, @sarfata. Using exit directly doesn’t work. If you make a shell script which exits with 0, that will stop the build:

For example, make a file named exit-zero.sh in the base of the repo (and commit it) containing this:

#!/bin/bash

exit 0

You must make sure the file permissions mark that file as executable and then you can stop builds with a build.ignore command of ./exit-zero.sh.

Attention: An exit code of zero (0) stops builds. Error exit codes (non-zero codes) continue the build. This is due to how the git diff --quiet option works (returning 1 on a difference and wanting to continue only when a difference does exist).

​Please let us know if there are other questions about this.

1 Like

Hi @luke,

I tried the exit-zero.sh option and it did not work (the build continued all the way):

1:09:39 PM: Detected ignore command in Netlify configuration file. Proceeding with the specified command: './exit-zero.sh'
1:09:39 PM: Starting build script
1:09:39 PM: Installing dependencies
1:09:39 PM: Python version set to 2.7
1:09:40 PM: Started restoring cached node version

(buildlink)

I then tried return 0 directly in the build.ignore and it did not work either:

11:47:34 AM: Detected ignore command in Netlify configuration file. Proceeding with the specified command: 'return 0'
11:47:35 AM: Starting build script
11:47:35 AM: Installing dependencies
11:47:35 AM: Python version set to 2.7
11:47:36 AM: Started restoring cached node version

(buildlink)

Any suggestion on how to debug this?

thanks a lot,
thomas

Hi, @sarfata. That is a private repo so I cannot see what it contains. May I have your permission to download the build cache so I can examine it?

Yes - I give you permission. Thank you!