Jenkins multibranch pipeline: branch name filtering not working?

2.1k Views Asked by At

I've got a Jenkins multibranch pipeline and I'm trying to filter branches and tags, but none of my filters seem to work.

I've added "Discover branches" and "Discover tags" and I can see in the logs that branches and tags are discovered, and builds scheduled.

However, I'd like to filter out certain tags, but the scan logs show every branch and tag satisfies the criteria. This is so even when I add an "Exact Name" branch filter such as "foobar":

branch name filter

Repository type: Git
Looking up **** for branches
Checking branch release/1.2.3 from ****
      ‘Jenkinsfile’ found
    Met criteria
...
Checking branch master from ****
      ‘Jenkinsfile’ found
    Met criteria

This is using Bitbucket source, but similar issue occurs with Git source.

Can anyone tell me what I am doing wrong here? I would think that none of the branches/tags in my repo would match 'foobar', but the scan log shows everything matching.

2

There are 2 best solutions below

1
frontendnils On

Sorry to tell you that it didn't work for me, too. So I decided to use the Jenkinsfile and some RegEx (used regexr.com to figure out) to "Filter" my Branches.

Add the following as the first step in your stage to achieve some kind of filtering:

when { expression {env.BRANCH_NAME =~ /[RegEx Here]/}}
0
John Q Citizen On

Okay ... I figured it out.

The correct method is to add the "Filter By Name" option (under "Behaviours") rather than "Named Branches" option (under "Build Strategies").

They do seem to be kinda similar concepts, at first glance.