Not able to access groovy variable in shell after upgrading jenkins to 1.X to 2.X

2,346 views
Skip to first unread message

Mahesh Wabale

unread,
Nov 13, 2017, 9:33:59 AM11/13/17
to Jenkins Users
Hi All , 


Recently i have upgraded jenkins to 2.87 from 1.611 . 

But i am facing issue with groovy . I am not able to access groovy variables in shell . Its giving empty value. 

Here is example : 

In system groovy script : 

  def project = new XmlSlurper().parse(new File("$workspace/pom.xml"))
   
  def ARTIFACT_VERSION=project.version.toString(); 

  println ARTIFACT_VERSION                                                      ==================> It is returning correct value. 



But if i am pring same variable in shell 

  echo $ARTIFACT_VERSION                                                  ===================> it is not printing anything with jenkins 2.X ( but it is working fine with jenkins 1.X) 


Is there any specific changes require to access groovy  variables in shell ? may be syntax or some plugins upgrade ?  


Thanks ,
Mahesh  
  


Robert Hales

unread,
Nov 14, 2017, 12:36:47 AM11/14/17
to Jenkins Users
Not sure exactly how it worked in he 1.x versions, but for pipeline, at least, you usually have to either interpolate the variable into the string of the shell script with double quotes: 

    sh "echo ${ARTIFACT_VERSION}"

Or you have to put the in the env object so they get injected into the shell's environment: 

    env.ARTIFACT_VERSION = ARTIFACT_VERSION
    sh
'echo $ARTIFACT_VERSION'

Mahesh Wabale

unread,
Nov 14, 2017, 6:41:15 AM11/14/17
to Jenkins Users
I have tried above , but it is not working  . 

Actual issue is below assignment is not working with system groovy script , it is returning null value. 

def ARTIFACT_VERSION=currentBuild.getEnvironment()['POM_VERSION'];        ====> POM_VERSIOM has values xxxx
def artifactVersionParam = new hudson.model.StringParameterValue("ARTIFACT_VERSION", ARTIFACT_VERSION)
def artifactVersionParamAction = new hudson.model.ParametersAction(artifactVersionParam);
currentBuild.addAction(artifactVersionParamAction);
 
println currentBuild.getEnvironment()['POM_VERSION'];                                         =====> output : xxxx 
println currentBuild.getEnvironment()['ARTIFACT_VERSION'];                                =====> output : null 


Because of this such variables are not accessible in environment variables in shell. It is working as expected in jenkins 1.X version.   

Victor Martinez

unread,
Nov 14, 2017, 10:09:41 AM11/14/17
to Jenkins Users
Couple of points:
- Did you review what changes have been added between both versions? This is the upgrade guide page: https://jenkins.io/doc/upgrade-guide/
- Maybe, I'm wrong but did you try to set hudson.model.ParametersAction.keepUndefinedParameters as true ? Further details: https://wiki.jenkins.io/display/JENKINS/Features+controlled+by+system+properties

Cheers

Daniel Beck

unread,
Nov 14, 2017, 11:25:42 AM11/14/17
to jenkins...@googlegroups.com

> On 13. Nov 2017, at 15:33, Mahesh Wabale <mahes...@gmail.com> wrote:
>
> Recently i have upgraded jenkins to 2.87 from 1.611 .
>

Something must be missing here, because this never worked as you describe it.

Reply all
Reply to author
Forward
0 new messages