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

GString not flattened to String by DSL inside a map

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: Minor Minor
    • workflow-cps-plugin
    • 1.580.13.1 / Workflow 1.4

      I want to resolve the artifact to be unarchived at execution time.

      Therefore I want to be able to perform the following:

      requiredFile = 'target/<some archived file name>'

      unarchive mapping: [requiredFile: .]

      Unfortunately this does not replace `requiredFile` with its value.

      trying to use the Groovy style like this:

      unarchive mapping: ["$requiredFile": .]

      results in

      java.lang.ClassCastException: org.codehaus.groovy.runtime.GStringImpl cannot be cast to java.lang.String

          [JENKINS-27916] GString not flattened to String by DSL inside a map

          Nigel Harniman created issue -

          Jesse Glick added a comment -

          Workaround (untested):

          unarchive mapping: ['' + requiredFile: '.']
          

          Jesse Glick added a comment - Workaround (untested): unarchive mapping: [ '' + requiredFile: ' .']
          Jesse Glick made changes -
          Labels New: groovy
          Summary Original: Unable to pass variable to file path element of unarchive map New: GString not flattened to String by DSL inside a map
          Jesse Glick made changes -
          Epic Link New: JENKINS-35390 [ 171183 ]

          In Groovy, you have to escape symbols for map literals keys:

          [(requiredFile): 'xxx']
          

          http://groovy-lang.org/groovy-dev-kit.html#_map_literals :

          Map keys are strings by default: [a:1] is equivalent to ['a':1]. This can be confusing if you define a variable named a and that you want the value of a to be the key in your map. If this is the case, then you must escape the key by adding parenthesis

          That means the workaround of using a GString as a key should not be needed

          Frédéric Chuong added a comment - In Groovy, you have to escape symbols for map literals keys: [(requiredFile): 'xxx' ] http://groovy-lang.org/groovy-dev-kit.html#_map_literals : Map keys are strings by default: [a:1] is equivalent to ['a':1]. This can be confusing if you define a variable named a and that you want the value of a to be the key in your map. If this is the case, then you must escape the key by adding parenthesis That means the workaround of using a GString as a key should not be needed

          Jacob Keller added a comment -

          The above does not work and still generates the exception:

          def file = "name-${env.BUILD_NUMBER}"
          [(file) : "."] still results in

          java.lang.ClassCastException: org.codehaus.groovy.runtime.GStringImpl cannot be cast to java.lang.String
          at org.jenkinsci.plugins.workflow.steps.ArtifactUnarchiverStepExecution.run(ArtifactUnarchiverStepExecution.java:44)
          at org.jenkinsci.plugins.workflow.steps.ArtifactUnarchiverStepExecution.run(ArtifactUnarchiverStepExecution.java:20)
          at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1$1.call(AbstractSynchronousNonBlockingStepExecution.java:49)
          at hudson.security.ACL.impersonate(ACL.java:213)
          at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run(AbstractSynchronousNonBlockingStepExecution.java:47)
          at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
          at java.util.concurrent.FutureTask.run(FutureTask.java:266)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
          at java.lang.Thread.run(Thread.java:745)

          Jacob Keller added a comment - The above does not work and still generates the exception: def file = "name-${env.BUILD_NUMBER}" [(file) : "."] still results in java.lang.ClassCastException: org.codehaus.groovy.runtime.GStringImpl cannot be cast to java.lang.String at org.jenkinsci.plugins.workflow.steps.ArtifactUnarchiverStepExecution.run(ArtifactUnarchiverStepExecution.java:44) at org.jenkinsci.plugins.workflow.steps.ArtifactUnarchiverStepExecution.run(ArtifactUnarchiverStepExecution.java:20) at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1$1.call(AbstractSynchronousNonBlockingStepExecution.java:49) at hudson.security.ACL.impersonate(ACL.java:213) at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run(AbstractSynchronousNonBlockingStepExecution.java:47) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

          I found this thread because I was running into the exact same error. I was able to get it working by adding .toString() along with the () in the mapping.

          So in this case it would be:
          def file = "name-${env.BUILD_NUMBER}".toString()
          [(file) : "."]

          HTH

          Justin Rainwater added a comment - I found this thread because I was running into the exact same error. I was able to get it working by adding .toString() along with the () in the mapping. So in this case it would be: def file = "name-${env.BUILD_NUMBER}".toString() [(file) : "."] HTH
          R. Tyler Croy made changes -
          Workflow Original: JNJira [ 162512 ] New: JNJira + In-Review [ 180953 ]
          Andrew Bayer made changes -
          Component/s New: pipeline-general [ 21692 ]
          Andrew Bayer made changes -
          Component/s Original: workflow-plugin [ 18820 ]

            abayer Andrew Bayer
            nharniman Nigel Harniman
            Votes:
            1 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved: