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!

Hi, @sarfata. It turns out I need a recent deploy to download that cache. May a trigger another deploy for this same commit id (the commit in the build here)?

I just kicked a new build: Netlify App

Hi, @sarfata. I need the build to be less than 30 minutes old. I can rerun the existing published deploy so that nothing changes (hopefully, as I don’t know enough about your site build to say for sure).

@Luke, You can re-run this one as much as you need.

Hi, @sarfata. I’m seeing only two difference between your repo and the working example.

  1. The file exit-zero.sh is missing a newline after exit 0.
  2. Your ignore command looks like this:
  ignore='./exit-zero.sh'

The working example looks like this:

  ignore = "./exit-zero.sh"

Would you please try changing both of those and test the deploy preview again in a new deploy?

@Luke I did what you suggested in build 607f016a52e7860007805115.

It still goes ahead and build everything:

9:30:04 AM: Finished fetching cache in 31.629803206s
9:30:04 AM: Starting to prepare the repo for build
9:30:05 AM: Preparing Git Reference pull/241/head
9:30:08 AM: Different build command detected, going to use the one specified in the Netlify configuration file: 'echo "CONTEXT=$CONTEXT HEAD=$HEAD" && yarn bootstrap && yarn build:web:staging' versus 'yarn -v && yarn bootstrap && yarn build:web' in the Netlify UI
9:30:08 AM: Detected ignore command in Netlify configuration file. Proceeding with the specified command: './exit-zero.sh'
9:30:09 AM: Starting build script
9:30:09 AM: Installing dependencies
9:30:09 AM: Python version set to 2.7
9:30:09 AM: Started restoring cached node version
9:30:14 AM: Finished restoring cached node version
9:30:15 AM: v14.16.1 is already installed.
9:30:15 AM: Now using node v14.16.1 (npm v6.14.12)
9:30:15 AM: Started restoring cached build plugins
9:30:15 AM: Finished restoring cached build plugins
9:30:16 AM: Attempting ruby version 2.7.1, read from environment

You can re-run this build too if you need too.

thanks,
thomas

Hi, @sarfata. I’ve now realized there is already a Unix/Linux command that always returns zero. We don’t need this script at all and use the command true. Note, it is a command and not a value. Here is the “top” of the manual page:

TRUE(1)               User Commands               TRUE(1)

NAME
       true - do nothing, successfully

SYNOPSIS
       true [ignored command line arguments]
       true OPTION

DESCRIPTION
       Exit with a status code indicating success.

Please just change the ignore command to this and it will stop the build:

[context.deploy-preview]
  ignore = "true"

I’ve tested this and it does work.

Note, I did test the other script as well and cannot find the difference in the two configurations. My test of exit-zero.sh did work.

However, using true is a much simpler solution and I’m sure it will work.

Tried this and it did not work either. I guess there is something else that is causing the issue on my build:

8:26:20 PM: Detected ignore command in Netlify configuration file. Proceeding with the specified command: 'true'

Hi, @sarfata. I considered that maybe the build.ignore setting is itself ignored if it is a deploy preview. However, I tested this myself for a deploy preview and it did stop the build:

https://app.netlify.com/sites/mu-simple-static-site/deploys/6081111ba0643500087f9c11

Quoting the logs:

11:01:00 PM: Build ready to start
11:01:02 PM: build-image version: be42e453d6c8f171cc2f654acc29c0a8b60e6d93
11:01:02 PM: build-image tag: v3.7.1
11:01:02 PM: buildbot version: b47b671c7e5601877c51968241eb899bf590a815
11:01:02 PM: Fetching cached dependencies
11:01:02 PM: Failed to fetch cache, continuing with build
11:01:02 PM: Starting to prepare the repo for build
11:01:02 PM: Netlify Large Media is enabled, running git commands with GIT_LFS_SKIP_SMUDGE=1
11:01:03 PM: No cached dependencies found. Cloning fresh repo
11:01:03 PM: git clone https://github.com/overlordofmu/simple-static-site
11:01:03 PM: Preparing Git Reference pull/1/head
11:01:05 PM: Detected ignore command in Netlify configuration file. Proceeding with the specified command: 'true'
11:01:05 PM: User-specified ignore command returned exit code 0. Returning early from build.
11:01:05 PM: Creating deploy upload records
11:01:06 PM: Failed during stage 'checking build content for changes': Canceled build due to no content change
11:01:06 PM: Finished processing build request in 3.719904325s

However, I see it not working in your logs:

8:25:41 PM: Build ready to start
8:25:43 PM: build-image version: be42e453d6c8f171cc2f654acc29c0a8b60e6d93
8:25:43 PM: build-image tag: v3.7.1
8:25:43 PM: buildbot version: b47b671c7e5601877c51968241eb899bf590a815
8:25:43 PM: Fetching cached dependencies
8:25:43 PM: Starting to download cache of 717.7MB
8:25:47 PM: Finished downloading cache in 3.458520852s
8:25:47 PM: Starting to extract cache
8:26:16 PM: Finished extracting cache in 28.991993154s
8:26:16 PM: Finished fetching cache in 32.989236312s
8:26:16 PM: Starting to prepare the repo for build
8:26:17 PM: Preparing Git Reference pull/241/head
8:26:20 PM: Different build command detected, going to use the one specified in the Netlify configuration file: 'echo "CONTEXT=$CONTEXT HEAD=$HEAD" && yarn bootstrap && yarn build:web:staging' versus 'yarn -v && yarn bootstrap && yarn build:web' in the Netlify UI
8:26:20 PM: Detected ignore command in Netlify configuration file. Proceeding with the specified command: 'true'
8:26:21 PM: Starting build script

Same versions of build-image and buildbot. I checked the syntax of both of our netlify.toml files and they are the same. I truly cannot find a reason why it isn’t working for you. You are doing everything correctly as far as I can see.

Thanks @luke. Should I open a ticket somewhere for more investigation? What next step do you suggest? I would like to be able to use this feature.