Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure Jenkins to strip the leading "origin/" in git branch parameter?

Tags:

git

regex

jenkins

I'm using Jenkins with a branch parameter to specify the branch to build from. Other stuff downstream needs the branch name to not have the leading "origin/" -- just "feature/blahblah" or "bugfix/12345" or similar. The advanced settings for the parameter let me specify a branch filter via regex, but I'm a regex newbie and the solutions I've found in searching are language-dependent. The Jenkins documentation for this is sparse.

When a user clicks on "build with parameters", for the branch I want to see branch names that omit the leading "origin/". I'm not sure how to write a regex for Jenkins that will "consume" that part of the branch name before setting the parameter value.

I solved this problem once before, I'm pretty sure using Stack Overflow, but I can't find those hints now.

like image 799
Monica Cellio Avatar asked May 23 '18 22:05

Monica Cellio


2 Answers

For the git branch parameter, set Branch Filter to:

origin/(.*)

I found the parentheses to be counter-intuitive, because if you don't specify a filter you get:

.*

(No parens.) If you are filtering stuff out, you use parens to indicate the part to keep.

like image 193
Monica Cellio Avatar answered Sep 18 '22 13:09

Monica Cellio


I usually use a groovy script evaluated before the job, like:

def map = [:]
map['GIT_BRANCH'] = GIT_BRANCH - 'origin/'
return map

This is using the EnvInject plugin, as described in gitlab-plugin issue 444

like image 22
VonC Avatar answered Sep 20 '22 13:09

VonC