0

I have branch with this format: release/1.0.0

When I use ${GIT_BRANCH}, I get the following: origin/release/1.0.0

I have tried using EnvInj to set a variable like this: enter image description here

If I leave ${GIT_BRANCH} the way it is it works fine, but if i use ${GIT_BRANCH, fullName=false}. It is always a empty string.

I have also tried using: How to receive local Git branch name with Jenkins Git plugin?

What is correct format to get just 1.0.0.0-rc134

jmogera
  • 1,689
  • 3
  • 30
  • 65

3 Answers3

1

You can always write a small bash script to get this done.

  1. Use a execute shell build step

    echo "RELEASECANDIDATE=${GIT_BRANCH##*/}-rc${BUILD_NUMBER}" > ${WORKSPACE}/inject.txt
    
  2. Use a Inject shell and in the properties file section give the file name

    ${WORKSPACE}/inject.txt
    
  3. Finally in the update build name , use macro below variable

    ${RELEASECANDIDATE}
    

This should get you correct value as expected

prudviraj
  • 3,634
  • 2
  • 16
  • 22
  • Getting this The given properties file path 'inject.txt' doesn't exist – jmogera Feb 17 '19 at 01:57
  • The reason was the file might be getting created in some other location, i have updated the answer to create the file in jenkins workspace parent location. try the updated answer. – prudviraj Feb 17 '19 at 08:58
  • okay that seems to work.. however for my branch "origin/release/1.0.0" GIT_BRANCH##*/ always return empty string. – jmogera Feb 18 '19 at 10:00
  • If you have followed above steps , you should get the Git branch value as 1.0.0 And add another below command in starting execute shell after the inject file step echo ${GIT_BRANCH##*/}-rc${BUILD_NUMBER}" And tell me the output of console and inject.txt file Please check if u missed any of the above steps.It's working from my end – prudviraj Feb 18 '19 at 16:13
0

Use below command at execute bash to get any part of the branch string you want. I am getting the mid part here. Keep in mind it starts counting from left and from 1.

SUBSTR=$(echo $GIT_BRANCH | cut -d'/' -f 2)

I continued the search and found a better solution for this problem at the link below. Basically rather than using $GIT_BRANCH, you can add one additional behaviour after git pull and "check out to specific local branch" and then use $GIT_LOCAL_BRANCH that will return what you expect and is compatible with all plugins (at least with FTP plugin that I used). Please see it in more details at the link below:

How to receive local Git branch name with Jenkins Git plugin?

0

Try to get ${GIT_LOCAL_BRANCH}. It returns only branch name.

Mustafa Güler
  • 884
  • 6
  • 11