Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-40134

Groovy truth issue in Pipeline script

XMLWordPrintable

      A Jenkins pipeline script combining two String values with a logical AND (&& operator) does not behave as expected:

      '' && '' -> false
      'a' && 'b' -> 'b'
      'a' != '' && 'b != '' -> true
      

      So when combining two non empty values the result is not of type boolean but the second value of type String.

      The following script demonstrates this behavior:

      node {
          def doit = { String s1, String s2 ->
              def p = { v -> "'$v' (${v.class})" }
          
              echo "s1 = ${p s1}, s2 = ${p s2}"
              echo "s1 && s2 = ${p s1 && s2}"
              echo "s1 != '' && s2 != '' = ${p s1 != '' && s2 != ''}"
          }
       
          doit '', ''
          doit 'foo', 'bar'
      }
      

      Console of an example run in my environment:

            Unassigned Unassigned
            m_olk Marcus Olk
            Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: