Pipeline plugin to access variable set in job in another job

327 views
Skip to first unread message

Johann Muller

unread,
Feb 17, 2016, 11:19:42 AM2/17/16
to Jenkins Users
With the current flow plugin I can do the following:

  api = build("upload-files",VERSION_TO_DEPLOY:params["API_VERSION”]))

  build("deploy-files", ARTIFACT_TO_DEPLOY:api.environment.get("ARTIFACT_TO_DEPLOY") )


This is just a simplified version, as the deploy job has 5 blocks of these. All 5  upload-files job runs concurrently and then runs sequentially through all 5 of them.  The upload-files job generates a file with a certain file name based on the package (not shown in the example for simplicity) and the version supplied. This filename gets set to the "ARTIFACT_TO_DEPLOY" variable with another plugin.


Now my question is can the pipeline plugin also support this?

Johann Muller

unread,
Feb 19, 2016, 8:55:57 AM2/19/16
to Jenkins Users
So I figured out how to do this, I wrote a function that will return the value of the variable that is required. First parameter is the jenkins job, then its the build number of the job that you require and finally the variable name,

def getJobVariable(jobName,buildId,varName){ 
job = Jenkins.instance.getItemByFullName(jobName)
        newJob = job.getBuildByNumber(buildId)
  return newJob.getEnvVars().get(varName,null)
}


api = build job: 'PipelineVarTest'
buildID =  api.getNumber()
echo getJobVariable('PipelineVarTest',15,'ARTIFACT_TO_DEPLOY')

akostadinov

unread,
Nov 24, 2017, 4:58:23 PM11/24/17
to Jenkins Users

On Friday, February 19, 2016 at 3:55:57 PM UTC+2, Johann Muller wrote:
...
  return newJob.getEnvVars().get(varName,null)
... 

Thank you for the useful pointers. FYI `getEnvVars` not very reliable. I wanted to get in this way but what I've got is only the first line of the string for some reason. Also `getEnvVars` is deprecated. I actually wanted to get a variable that was set by a parameter. So I ended up doing:

>    paramValue = build.allActions.
>        find {it in hudson.model.ParametersAction}.
>        getParameter("MY_PARAM_NAME").
>        value

Regards.
Reply all
Reply to author
Forward
0 new messages