How to load variables from file into windows environment variables in Jenkins pipeline?

2,364 views
Skip to first unread message

Mark Allison

unread,
Jan 26, 2017, 6:57:45 AM1/26/17
to Jenkins Users
I have the following Jenkinsfile

    node  {
    stage
'checkout'
    deleteDir
()
    checkout scm
   
    load
'LoadTheseVariables.txt'  
   
    echo
"MYKEY: ${MYKEY}"
    echo
"REPO: ${REPO}"
   
    bat
'echo MYKEY is %MYKEY%'
    bat
'echo REPO is %REPO%'        
   
}


The file LoadTheseVariables.txt contains:

    MYKEY="ThisIsTheKey"
    REPO
="ThisIsTheRepo"


The output of the Jenkins build is:

    <..snip..>
    [Pipeline] load
    [Pipeline] { (LoadTheseVariables.txt)
    [Pipeline] }
    [Pipeline] // load
    [Pipeline] echo
    MYKEY: ThisIsTheKey
    [Pipeline] echo
    REPO: ThisIsTheRepo
    [Pipeline] bat
    [test] Running batch script
   
    D:\Jenkins\workspace\test>echo MYKEY is  
    MYKEY is
    [Pipeline] bat
    [test] Running batch script
   
    D:\Jenkins\workspace\test>echo REPO is  
    REPO is
    [Pipeline] }
    [Pipeline] // node
    [Pipeline] End of Pipeline
    Finished: SUCCESS


The Jenkins pipeline can see the variables but why can't my windows cmd environment see the variables? I even tried this step before the `bat` commands at the end: `bat 'refreshenv'` but that didn't reload the environment with those variables.

How can I load variables from a file in a Windows job and have them loaded into the environment at run time?

Daniel Beck

unread,
Jan 26, 2017, 9:25:37 AM1/26/17
to jenkins...@googlegroups.com

> On 26.01.2017, at 12:57, Mark Allison <marka...@gmail.com> wrote:
>
> How can I load variables from a file in a Windows job and have them loaded into the environment at run time?

These are just Groovy variables. You didn't specify for them to be passed to launched processes' environment (as e.g. build parameters automatically are).

withEnv(…) should work.

env.VAR = 'value' probably too, but am not sure whether that's a recommended approach.

Mark Allison

unread,
Jan 26, 2017, 9:49:21 AM1/26/17
to jenkins...@googlegroups.com
Thanks, simply using double quotes instead of single works fine.

bat "echo MYKEY is %MYKEY%" 


--
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/32EBB24D-996C-4CE4-8C1B-B1E8ECD6560F%40beckweb.net.
For more options, visit https://groups.google.com/d/optout.
--

Sent from my phone

Daniel Beck

unread,
Jan 26, 2017, 10:08:49 AM1/26/17
to jenkins...@googlegroups.com

> On 26.01.2017, at 15:49, Mark Allison <marka...@gmail.com> wrote:
>
> Thanks, simply using double quotes instead of single works fine.

Unexpected. Looks like I learned something new today. Thanks!
Reply all
Reply to author
Forward
0 new messages