How to pass paramater to process environment variable?

45 views
Skip to first unread message

Idan Adar

unread,
Sep 20, 2017, 3:29:32 AM9/20/17
to Jenkins Users
The following is my current pipeline:

stage ("My Stage") {  
         environment
{
            cloudcerts_iamConfig_preprod
= credentials('${cloudcerts_iamConfig_preprod}')
         
}
             
         steps
{
               
// Setup packages and run tests
               sh
'''
                  npm install
                  npm test
               '''

         
}
}

During runtime of the tests, "cloudcerts_iamConfig_preprod" is available via process.env and everything is great.
My requirement now however is to add another property dynamically.

To do this I ticked the checkbox "This project is parameterized" > String parameter, and provided a name and default value (myStringParameter, "xxx").
I then added to "environments":

myProperty = ${myStringParameter)

However when the tests run process.env.myProperty returns "undefined".
Is this the right way to pass the parameter? Is there a different way?

Steven Foster

unread,
Sep 20, 2017, 5:21:05 AM9/20/17
to Jenkins Users
You should define the parameters in the pipeline also:
https://jenkins.io/doc/book/pipeline/syntax/#parameters

currently there is some disconnect in the project configuration UI for pipeline jobs, many of the options there need to actually be defined in the pipeline.

Idan Adar

unread,
Sep 20, 2017, 5:34:52 AM9/20/17
to Jenkins Users
Okay.
How can I then use this in combination with: https://github.com/jansepar/node-jenkins-api#build_with_params

I used parameters as a way to test getting a value "externally".
With this working, the end goal is to trigger the job remotely with a parameter, and the parameter's value then needs to be used as an environment variable...

Can this be done with the Parameters pipeline option you've mentioned?

Robert Hales

unread,
Sep 21, 2017, 11:42:35 PM9/21/17
to Jenkins Users
The Parameters can be defined in the pipeline, or in the job configuration, but if you define it in the pipeline, it will update the job configuration the first time it runs. So when you add the parameters to the pipeline, and have none configured in your job, your Build Now button will still be Build Now. Run it once, things may fail and die. But now your job will be configured with the parameters, and the Build Now link now says Build with Parameters. I think it is cleaner to add any config possible in the pipeline code. Also, if you change any property from the pipeline code, it will change all properties in the job config, so if it wasn't specified in the pipeline code, it will disable. So, for example, if you configure the build discarder in the project, then configure parameters in the pipeline code, you will lose your build discarder when the job runs. 

To access the parameters in your pipeline, use the params object: 

parameters {
   
string(defaultValue: 'master', description: 'Git Branch', name: 'gitBranch')
}


...


myProperty
= "${params.gitBranch}"

Idan Adar

unread,
Sep 22, 2017, 12:15:54 AM9/22/17
to Jenkins Users
Thanks Robert. However I'm not sure yet how this helps in solving the next step here. Namely:

> How can I then use this in combination with: https://github.com/jansepar/node-jenkins-api#build_with_params
> ... the end goal is to trigger the job remotely with a parameter, and the parameter's value then needs to be used as an environment variable...

Robert Hales

unread,
Sep 22, 2017, 12:49:28 AM9/22/17
to Jenkins Users
You should be able to use the params object in the environment{} block and assign it to an environment variable.

Viacheslav Dubrovskyi

unread,
Oct 3, 2017, 8:41:59 AM10/3/17
to jenkins...@googlegroups.com

Hi Robert,

In case if I use Jenkinsfile in multibranch and simple pipeline jobs, and for simple pipeline jobs I would like to configure job's parameters manually and for multibranch jobs from Jenkinsfile. How can I do this?


22.09.2017 06:42, Robert Hales пишет:
--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/2b08c388-ccf5-43dd-a9a6-980eed34546f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
WBD,
Viacheslav Dubrovskyi
Reply all
Reply to author
Forward
0 new messages