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

Variable assignment in if condition is not working as expected

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • workflow-cps-plugin
    • None
    • Jenkins 2.89.3
      Pipeline 2.5

      The below code works as expected as a standalone Groovy script (the if condition doesn't enter and so script value remains the same:

      def script = '''mvn -q -Dexec.executable=echo -Dexec.args=\\${project.version} --non-recursive exec:exec'''
      def index
      if ((index = script.indexOf('release:prepare')) != -1) {
          script = script[0..<index-1] + ' -DpushChanges=false ' + script[index..-1]
      }
      

      But the exact same code in a custom vars function misbehaves. The if condition enters anyway with an index value of -1 resulting in a script value of mvn -q -Dexec.executable=echo -Dexec.args=\${project.version} --non-recursive exec:ex -DpushChanges=false c

      If I pull the assignment out of the if condition, it works as expected:

      def index = script.indexOf('release:prepare')
      if (index != -1) {
      

            Unassigned Unassigned
            haridsv Hari Dara
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: