Get feature branch name on merge to main


We are trying to get feature branch name on merge to main and this is a single branch merge, no multi branches involved. BRANCH_NAME shows main, not a feature branch name. We tried a lot of things including this (reading from commit msg):

script: “git branch --contains ${scmVars.GIT_PREVIOUS_SUCCESSFUL_COMMIT}”,
returnStdout: true

and it is all messed up, and not dependable. Any suggestions on how to get branch name / set some env var?


Hello @nhrebinka and welcome to this community :wave:

Are you using the Git Plugin?

Hi @poddingue, thank you! Yes, we are using Git Plugin.
We seem to get it resolved by doing a little brain surgery:

                SOURCE_BRANCH = sh(
                    script: "git log --merges --first-parent origin/main | awk '/Merge pull request/{split(\$NF, arr, \"/\"); print arr[2]}' | head -1",
                    returnStdout: true

Is there are not better ways, we will keep it this way. Thanks!

1 Like

Ouch, that sounds painful! :brain: :scissors:

On a more serious note, it’s possible that there is a more effective approach to the situation. Hopefully, someone with more expertise will join the conversation and offer some helpful advice.