[JIRA] [pipeline-stage-view-plugin] (JENKINS-33842) Accessing variable from former stage

10 views
Skip to first unread message

michaelhuettermann@java.net (JIRA)

unread,
Mar 27, 2016, 6:13:01 PM3/27/16
to jenkinsc...@googlegroups.com
Michael Hüttermann created an issue
 
Jenkins / Bug JENKINS-33842
Accessing variable from former stage
Issue Type: Bug Bug
Assignee: Antonio Muñiz
Components: pipeline-stage-view-plugin
Created: 2016/Mar/27 10:12 PM
Labels: 2.0
Priority: Minor Minor
Reporter: Michael Hüttermann

In a pipeline stage, it is not possible to reference a variable that is set inside a shell script as part of a former stage, see the simplified example below. In my use case, the first stage reads a file in the workspace and evaluates part of its content to set the variable.

stage 'Fetch version'
   sh '''#!/bin/sh
   version=abc'''

stage 'Second'
    sh "${mvnHome}/bin/mvn -f all/pom.xml -Dversion=$version ... 

Result:

groovy.lang.MissingPropertyException: No such property: version for class: WorkflowScript
	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
	at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:25)
	at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:17)
	at WorkflowScript.run(WorkflowScript:48)

I've tried a lot of different things setting and accessing the variable, including the environment variable env, but nothing worked.

Add Comment Add Comment
 
This message was sent by Atlassian JIRA (v6.4.2#64017-sha1:e244265)
Atlassian logo

michaelhuettermann@java.net (JIRA)

unread,
Mar 27, 2016, 6:13:01 PM3/27/16
to jenkinsc...@googlegroups.com

dbeck@cloudbees.com (JIRA)

unread,
Mar 27, 2016, 9:17:01 PM3/27/16
to jenkinsc...@googlegroups.com

In my use case, the first stage reads a file in the workspace and evaluates part of its content to set the variable.

Use the `readFile` workflow function: https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#code-readfile-code-read-file-from-workspace

I'm pretty sure the behavior necessary to make the script you're demonstrating work (either as pipeline variable, or shell variable) could have unexpected and impossible to debug side effects if implemented.

amuniz@cloudbees.com (JIRA)

unread,
Mar 28, 2016, 5:11:01 AM3/28/16
to jenkinsc...@googlegroups.com

sh requires a workspace to run to begin with, I guess you just omitted it for clarity. Moreover, in your example $version is being resolved by the Groovy engine, that's why you are getting groovy.lang.MissingPropertyException on version. Just replace " (double quotes) with ' (single quote) around the shell line to let shell to look up for that environment variable.

And finally, each sh step has its own non-shared scope, so as a general rule: use plain groovy variables to pass information from one step to another (for example using readFile as Daniel Beck pointed out to load something written to disk by the shell step into a groovy variable which you can pass later to the second shell step).

amuniz@cloudbees.com (JIRA)

unread,
Mar 28, 2016, 5:11:01 AM3/28/16
to jenkinsc...@googlegroups.com
Antonio Muñiz updated an issue
 
Change By: Antonio Muñiz
Component/s: workflow-plugin
Component/s: pipeline-stage-view-plugin

amuniz@cloudbees.com (JIRA)

unread,
Mar 28, 2016, 5:11:02 AM3/28/16
to jenkinsc...@googlegroups.com
Antonio Muñiz closed an issue as Won't Fix
Change By: Antonio Muñiz
Status: Open Closed
Resolution: Won't Fix

michaelhuettermann@java.net (JIRA)

unread,
Mar 30, 2016, 2:38:01 AM3/30/16
to jenkinsc...@googlegroups.com
Michael Hüttermann commented on Bug JENKINS-33842
 
Re: Accessing variable from former stage

Hi Antonio Muñiz,
thanks for the feedback.
Yes, sure, the issue here is how to access a variable from a former stage, not how to define a stage for checking out sources from version control.
For me, as a user, a variable is resolved, if Jenkins replaces the variable with the content during build time. The variable could not be resolved and that's what the error message says? I've applied the different syntax before, and it also did not work. I've also tried this and that, and the ticket description just reflects a current situation, where I did not know the recommended, official way, and I'm very thankful for your hint into this direction.
Yes, the different scopes. I know. This is really a long term pain. Actually, as part of a Pipeline, passing a variable to a later build stage by using a file to read+restore the variable can only be a workaround.
Thank you.

michaelhuettermann@java.net (JIRA)

unread,
Mar 30, 2016, 2:46:01 AM3/30/16
to jenkinsc...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages