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
.
echo "Comparing branch '${branch}' with shortBranch '${shortBranch}'"
to verify that you're comparing the values you expect you're comparingfeature/jenkins-build-dock???er
at the position of -30, -128, -117 (which are â, € and ‹).