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

          Jesse Glick added a comment -

          Workaround (untested):

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

          Jesse Glick added a comment - Workaround (untested): unarchive mapping: [ '' + requiredFile: ' .']

          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

          Andrew Bayer added a comment -

          This works in workflow-cps 2.39 (with the caveat that, as elsewhere in Groovy, the key has to either be (var) or "${var}"), and I've got a PR up at https://github.com/jenkinsci/workflow-cps-plugin/pull/161 adding a test for it.

          Andrew Bayer added a comment - This works in workflow-cps 2.39 (with the caveat that, as elsewhere in Groovy, the key has to either be (var) or "${var}" ), and I've got a PR up at https://github.com/jenkinsci/workflow-cps-plugin/pull/161 adding a test for it.

          Jesse Glick added a comment -

          Probably same as JENKINS-43934.

          Jesse Glick added a comment - Probably same as  JENKINS-43934 .

          Code changed in jenkins
          User: Andrew Bayer
          Path:
          src/test/java/org/jenkinsci/plugins/workflow/cps/CpsFlowDefinition2Test.java
          http://jenkins-ci.org/commit/workflow-cps-plugin/329f900d776c96f4f91576bb7bda3bbde31a26e1
          Log:
          JENKINS-27916 Test verifying use of GStrings in map keys

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Andrew Bayer Path: src/test/java/org/jenkinsci/plugins/workflow/cps/CpsFlowDefinition2Test.java http://jenkins-ci.org/commit/workflow-cps-plugin/329f900d776c96f4f91576bb7bda3bbde31a26e1 Log: JENKINS-27916 Test verifying use of GStrings in map keys

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

              Created:
              Updated:
              Resolved: