Sourcing environment variables on node

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Sourcing environment variables on node

Christoph Ruepprich
Hi all,
on a remote node I have a file that exports some variables:
$ cat myenv
export FOO=Hello
export BAR=World

My pipeline script tries to source that file, but the variables don't seem to stick:
node ('remote-server') {
    sh ('. /home/cmr/myenv')
    echo "My Variables: ${env.FOO} ${env.BAR}"
}

Output:
[set-remote-env] Running shell script
+ . /home/cmr/myenv
++ export FOO=Hello
++ FOO=Hello
++ export BAR=World
++ BAR=World
[Pipeline] echo
My Variables: null null


When I source directly from the server's command prompt, the variables stick:
$ . myenv
$ echo "My Variables: $FOO $BAR"
My Variables: Hello World
$




How can I source environment variables in my pipeline script?

Thanks!







--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/2bfd3d27-2fe7-4e49-9e9c-a7edaf416e60%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Sourcing environment variables on node

itchymuzzle
Maybe something like this?

node ('remote-server') {
    sh '''
       . /home/cmr/myenv'
       echo "My Variables: ${env.FOO} ${env.BAR}"
    '''
}

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/86f0cdd9-5705-4d12-8229-210ce6614aee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Sourcing environment variables on node

Christoph Ruepprich
Thanks ichymuzzle. It worked with a minor change.
So the problem is that each sh looses it's environment. Is there a way to set it globally?


node ('remote-server') {
    sh ('''
       . /home/cmr/myenv
       echo "My Variables: $FOO $BAR"
    ''')
}



On Thursday, 2 November 2017 14:01:04 UTC-5, itchymuzzle wrote:
Maybe something like this?

node ('remote-server') {
    sh '''
       . /home/cmr/myenv'
       echo "My Variables: ${env.FOO} ${env.BAR}"
    '''
}

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/892167f5-00f0-43be-ac79-20eb6dec8bfd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Sourcing environment variables on node

itchymuzzle
Don't know how to set it globally, most likely someone will know how to do that and respond.

> each sh looses it's environment.

Each sh is it's own environment.  If you have two different terminals up, what you do in one isn't known in the other. For example.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/a7590e90-e66a-446f-9692-7dfcc6b92d30%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Sourcing environment variables on node

Robert Hales
Yeah, as you found, the environment is the sh is only present during that run. It is just like dropping to a shell (or running another shell within a shell), running some things, and then exiting. 

If you want to set them globally, you will need to set them in an environment block in declarative pipeline, or in groovy code. env.FOO = "BAR". 

If you want to try to set something in the shell, and then get those values back into the pipeline, it gets a little harder. You can return just a value from a shell script and assign that to a variable. Or you can look at the pipeline utility steps plugin to suck things in from a file. 

On Thursday, November 2, 2017 at 2:07:38 PM UTC-6, itchymuzzle wrote:
Don't know how to set it globally, most likely someone will know how to do that and respond.

> each sh looses it's environment.

Each sh is it's own environment.  If you have two different terminals up, what you do in one isn't known in the other. For example.

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/74cf5bb2-00d5-4d4f-8254-787ea6fb7a78%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.