Body: <p>I have a config file that's in YAML format, which includes this relevant portion:</p>
  <pre><code>project:
    protectedBranches:
      - master
      - develop
      - feature/something
  </code></pre>
  <p>In my Jenkinsfile, the YAML file is loaded via: <code>config = readYaml('config.yml')</code>.</p>
  <p>I also have <code>env.GIT_BRANCH = env.gitlabSourceBranch</code> which results in <code>env.GIT_BRANCH</code> containing the value <code>origin/feature/something</code></p>
  <p>The <code>config.project.protectedBranches</code> is of type <code>ArrayList()</code></p>
  <p>I am attempting to do something based on whether <code>env.GIT_BRANCH</code> exists in the <code>config.project.protectedBranches</code>, but none of the following have worked.</p>
  <pre><code>def shortBranch = env.GIT_BRANCH
  shortBranch = shortBranch.minus(&quot;origin/&quot;)
  // shortBranch now has a value of feature/something
  
  if(shortBranch in config.project.protectedBranches) // returns false
  </code></pre>
  <p>Also...</p>
  <pre><code>config.project.protectedBranches.each{ branch -&gt;
      echo &quot;Does branch match shortBranch? ${branch.equals(shortBranch)}&quot; // returns false
      echo &quot;Does branch match shortBranch? ${branch == shortBranch}&quot; // also returns false
  }
  </code></pre>
  <p>Both <code>branch</code> and <code>shortBranch</code> are of type <code>String</code>. Why on Earth can I not compare these strings?</p>
  <p>The issue only occurs when the <code>branch</code> string contains a forward-slash, e.g, <code>feature/something</code>. Additionally, someone asked if I've verified the type of each, and yes I have. Both <code>shortBrancg.getClass()</code> and <code>branch.getClass()</code> return type <code>java.lang.String</code>.</p>
  
Comments: CC BY-SA 4.0
Comments: 2021-04-23T15:49:38.490Z
Comments: 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. https://stackoverflow.com/a/47027926/2196304 Maybe try it with a simple for loop.
Comments: KymikoLoco
Comments: CC BY-SA 4.0
Comments: 2021-04-23T16:06:13.310Z
Comments: 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
Comments: jayhendren
Comments: CC BY-SA 4.0
Comments: 2021-04-25T17:08:44.487Z
Comments: @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.
Comments: Argyle
Comments: CC BY-SA 4.0
Comments: 2021-04-25T17:09:26.117Z
Comments: @KymikoLoco Just to clarify, the `.each` works, it's the comparison within that is not working.
Comments: Argyle
Comments: CC BY-SA 4.0
Comments: 2021-04-26T18:12:32.823Z
Comments: 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 https://stackoverflow.com/help/minimal-reproducible-example
Comments: KymikoLoco
Comments: CC BY-SA 4.0
Comments: 2021-04-27T07:25:28.737Z
Comments: I tried to reproduce your scenario in the script console using the snakeyaml classes also used by the utility step, and couldn't. My guess is (because of some copy&paste?) you have got some non-printable characters in your yaml config file, like for instance a zero width space. To check, use: `println ${branch.getBytes()}`
Comments: ᴳᵁᴵᴰᴼ
Comments: CC BY-SA 4.0
Comments: 2021-05-05T16:34:34.263Z
Comments: @ᴳᵁᴵᴰᴼ Looks like you're on to something. `branch` and `shortBranch`, are NOT the same in bytes. `branch bytes: [102, 101, 97, 116, 117, 114, 101, 47, 106, 101, 110, 107, 105, 110, 115, 45, 98, 117, 105, 108, 100, 45, 100, 111, 99, 107, -30, -128, -117, 101, 114]` and `shortBranch bytes: [102, 101, 97, 116, 117, 114, 101, 47, 106, 101, 110, 107, 105, 110, 115, 45, 98, 117, 105, 108, 100, 45, 100, 111, 99, 107, 101, 114]`. I can't for the life of me understand why these are different. Is is something related to encoding perhaps? The git server is Gitlab if that is matters.
Comments: Argyle
Comments: CC BY-SA 4.0
Comments: 2021-05-06T08:06:51.083Z
Comments: 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).
Comments: ᴳᵁᴵᴰᴼ
Comments: CC BY-SA 4.0
Comments: 2021-05-06T12:44:50.000Z
Comments: @ᴳᵁᴵᴰᴼ 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.
Comments: Argyle
Comments: CC BY-SA 4.0
Comments: 2021-05-10T07:07:25.747Z
Comments: 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
Comments: ᴳᵁᴵᴰᴼ
Tags: <jenkins><yaml><groovy>
Title: Jenkins Unable to Match Two Identical Strings
UserDisplayName: Argyle
ThreadText: # Jenkins Unable to Match Two Identical Strings
  
  (Asked by: username_0 on 2021-04-22)
  
  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`.
  
  **Tags:** jenkins, yaml, groovy
  
  > **Comments:**
  > **username_1**: 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. https://stackoverflow.com/a/47027926/2196304 Maybe try it with a simple for loop.  
  > **username_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  
  > **username_0**: @username_2 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.  
  > **username_0**: @username_1 Just to clarify, the `.each` works, it's the comparison within that is not working.  
  > **username_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 @username_2 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 https://stackoverflow.com/help/minimal-reproducible-example  
  > **username_3**: I tried to reproduce your scenario in the script console using the snakeyaml classes also used by the utility step, and couldn't. My guess is (because of some copy&paste?) you have got some non-printable characters in your yaml config file, like for instance a zero width space. To check, use: `println ${branch.getBytes()}`  
  > **username_0**: @username_3 Looks like you're on to something. `branch` and `shortBranch`, are NOT the same in bytes. `branch bytes: [102, 101, 97, 116, 117, 114, 101, 47, 106, 101, 110, 107, 105, 110, 115, 45, 98, 117, 105, 108, 100, 45, 100, 111, 99, 107, -30, -128, -117, 101, 114]` and `shortBranch bytes: [102, 101, 97, 116, 117, 114, 101, 47, 106, 101, 110, 107, 105, 110, 115, 45, 98, 117, 105, 108, 100, 45, 100, 111, 99, 107, 101, 114]`. I can't for the life of me understand why these are different. Is is something related to encoding perhaps? The git server is Gitlab if that is matters.  
  > **username_3**: 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).  
  > **username_0**: @username_3 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.  
  > **username_3**: 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  
  
  ---
  
  ## 0 answers
  
  