3

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

project:
  protectedBranches:
    - 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

Also...

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.

10
  • The second example may be falling prey to how Groovy works in Jenkinsfiles. the .each may only 'run' on the last element in the list. stackoverflow.com/a/47027926/2196304 Maybe try it with a simple for loop.
    – KymikoLoco
    Apr 23 at 15:49
  • 1
    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 at 16:06
  • @jayhendren Yes I did some debugging. Both shortBranch.getClass() and branch.getClass() return java.lang.String. Furthermore, it turns out it's something to do with the ` in the branch name because it works as intended for develop` and master branches.
    – Argyle
    Apr 25 at 17:08
  • 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 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 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.