I have a config file that's in YAML format, which includes this relevant portion:

    - master
    - develop
    - feature/something

In my Jenkinsfile, the YAML file is loaded via: config = readYaml('config.yml').

I also have env.GIT_BRANCH = env.gitlabSourceBranch which results in env.GIT_BRANCH containing the value origin/feature/something

The config.project.protectedBranches is of type ArrayList()

I am attempting to do something based on whether env.GIT_BRANCH exists in the config.project.protectedBranches, but none of the following have worked.

def shortBranch = env.GIT_BRANCH
shortBranch = shortBranch.minus("origin/")
// shortBranch now has a value of feature/something

if(shortBranch in config.project.protectedBranches) // returns false


config.project.protectedBranches.each{ branch ->
    echo "Does branch match shortBranch? ${branch.equals(shortBranch)}" // returns false
    echo "Does branch match shortBranch? ${branch == shortBranch}" // also returns false

Both branch and shortBranch are of type String. Why on Earth can I not compare these strings?

The issue only occurs when the branch string contains a forward-slash, e.g, feature/something. Additionally, someone asked if I've verified the type of each, and yes I have. Both shortBrancg.getClass() and branch.getClass() return type java.lang.String.

  • 2
    Have you done some basic debugging? E.g. echo "Comparing branch '${branch}' with shortBranch '${shortBranch}'" to verify that you're comparing the values you expect you're comparing
    – jayhendren
    Apr 23, 2021 at 16:06
  • 1
    Stands to reason that the strings you are comparing are not equal, then? What is the output of the strings you are comparing (not just the class, not just the comparison itself, but the actual strings like @jayhendren suggests)? You've not shown what those values are. Also, depending on what you are running and when, in Pipelines GIT_BRANCH will be set when Jenkins checks out code. That may be overwriting your env.GIT_BRANCH, but it's hard to tell without stackoverflow.com/help/minimal-reproducible-example
    – KymikoLoco
    Apr 26, 2021 at 18:12
  • 2
    yes, as you can see you have not printable characters (if you try to convert the first array back to string you'll see "?" characters in feature/jenkins-build-dock???er at the position of -30, -128, -117 (which are &#226, &#128 and &#139). May 6, 2021 at 8:06
  • 1
    @ᴳᵁᴵᴰᴼ Thanks for your help with this. I assume this occurred when copying/pasting the YAML from my IDE to Gitlab. The fix was simple, just delete the value and re-type it. If you'd like to submit the above as an answer, I'll mark it as answered so you get the proper credit.
    – Argyle
    May 6, 2021 at 12:44
  • 1
    You are welcome, anyway I prefer to vote to close the topic as a typographical error since it won't likely be helpful to others May 10, 2021 at 7:07


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.