0

When I setup a Jenkins Multibranch Pipeline through the UI the Behaviours section allows me to specify what gets built. We add "Discover pull requests from origin" so PRs get built, and "Discover branches" to build branches. Behaviours

But for one build which takes well over an hour we'd like to disable the branch building so it's only built for PRs (our builds run on AWS and we don't want the additional expense). Unfortunately we've found that when we remove "Discover branches" it also stops building main.

What we'd like to have is for Jenkins to:

  • Build PRs.
  • Build main.
  • Ignore all other branches.

Is this possible?

Update: I tried wildcards as suggested below but it doesn't work. I added "Filter by name (with regular expression)" with the regex set to "^(?:.main).$" (basically match main and nothing else) but no difference. Filter by name (with regular expression)

Then I added back "Discover branches" and it successfully found main and built that but no other branches.

However, when I created a PR called test-build it also ignored this. Seems like the wildcard works on everything. Since we don't have a naming convention this won't work.

parsley72
  • 8,449
  • 8
  • 65
  • 98
  • Ignore as in they are shown, but builds aren't run for them, or ignore as in they aren't shown in jenkins? – markalex Jul 26 '23 at 20:46
  • Builds aren't run. Our builds run in AWS, we don't want the additional expense. – parsley72 Jul 26 '23 at 21:05
  • 1
    `when I created a PR called test-build` — unclear what you mean by this. PRs are usually called e.g. `PR-123`. This may be created over _branch_ named `test-build`. – MaratC Jul 27 '23 at 10:22

3 Answers3

1

If I understand correctly, you only want to build the main branch and the PRs, ignoring all other branches. This can be easily achieved with these configurations:

Discover branches - All branches

Discover pull requests from origin - Merging the pull request with the current target branch revision

Filter by name (with regular expression) - ^(main|PR-\d+)$

Hope this helps!

Dibakar Aditya
  • 3,893
  • 1
  • 14
  • 25
0

Is this possible?

Yes. So filter out all branches except main.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • If I add "Filter by name (with wildcards)" with "Include" = "main" and "Exclude" = "*" it doesn't build main. Is there something else I need? – parsley72 Jul 26 '23 at 20:26
  • Sorry, did you try google? https://stackoverflow.com/questions/30095583/how-do-i-configure-jenkins-to-build-all-branches-except-a-few-which-i-exclude https://stackoverflow.com/questions/21314632/how-to-exclude-git-branch-from-building-in-jenkins https://stackoverflow.com/questions/17155249/jenkins-gerrit-trigger-on-all-branches-except-master . Yes, `exclude ="*"` means excelude all. So exclude all except main. Something along `^(?!main).*$`. You can learn more about lookrounds here https://www.regular-expressions.info/lookaround.html . – KamilCuk Jul 26 '23 at 20:29
  • Tried those, doesn't work. – parsley72 Jul 26 '23 at 20:37
0

There are two things at work here and they are different.

First thing, the "Behaviors" section talks about what branches or PRs are discovered. Those that are discovered will be shown in Jenkins; those that aren't discovered will not be.

In your multi-branch pipeline (or Git Organization — it's not clear enough from your post), there would be several tabs — "branches" with all discovered branches and "Pull requests" with all discovered PRs. (Some configs may add tags; we ignore tags.) Whatever you configure in the "Behaviors" section only influences the contents of these two tabs[0].

The second thing at play here, is the Build strategy — as in, out of what we have discovered, what do we build. This configuration is further down on the page. From the help:

By default (i.e. when empty), everything except tags will be automatically built whenever discovered or changed. If you add at least one strategy then the defaults will be disabled and only the configured strategies will be applied.

So we don't like the default[1], and so we've configured a "Named Branches" strategy. In our case, we used "Change Requests" and "Regular Expression" with PR-\d+ as expression. In your case, you may want to use that, and add a "Exact Match" strategy with "main" as the name. (If that doesn't work, you may want to modify the regex from above to also include main.)

Additionally, please consider that any merge into main branch will be a change of that branch; a build will be triggered automatically. (Depending on your situation, this may or may not be what you want.)

[0]: At my place, it's All for Branches and Current revision for PRs.

[1]: We want to only build PRs automatically but not branches — whoever wants to build their branch can do so manually.

MaratC
  • 6,418
  • 2
  • 20
  • 27