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

GString not flattened to String by DSL inside a map

    Details

    • Similar Issues:

      Description

      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

        Attachments

          Issue Links

            Activity

            Hide
            jglick Jesse Glick added a comment -

            Workaround (untested):

            unarchive mapping: ['' + requiredFile: '.']
            
            Show
            jglick Jesse Glick added a comment - Workaround (untested): unarchive mapping: [ '' + requiredFile: ' .']
            Hide
            fchuong 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

            Show
            fchuong 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
            Hide
            jekeller 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)

            Show
            jekeller 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)
            Hide
            rainwaj 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

            Show
            rainwaj 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
            Hide
            abayer 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.

            Show
            abayer 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.
            Hide
            jglick Jesse Glick added a comment -

            Probably same as JENKINS-43934.

            Show
            jglick Jesse Glick added a comment - Probably same as  JENKINS-43934 .
            Hide
            scm_issue_link 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

            Show
            scm_issue_link 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

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved: