How do I pass my variable VAR_A to an embedded powershell script in a jenkins pipeline ?
e.g.
def VAR_A = 'test'
def mystatus = powershell(returnStatus: true, script: '''
Write-Host "My result: '$VAR_A'" '''
withEnv(["VAR_A=test"]) {
def mystatus = powershell(returnStatus: true, script: '''
Write-Host "My result: '$VAR_A'" '''
}
both result with following output My result: ''
Note : I prefer to define my powershell script in the jenkinsfile to keep things simple.
Try this:
node {
powershell '''
$VAR_A = 'test'
Write-Host "My result: '$VAR_A'"
'''
withEnv(["VAR_A=envtest"]) {
powershell '''
Write-Host "My result is empty: '$VAR_A'"
Write-Host "My env result: '$env:VAR_A'"
'''
}
}
The output is:
My result: 'test'
My result is empty: ''
My env result: 'envtest'
This was tested on Jenkins 2.73.1.
Note that:
$VAR_A = 'test'
is declared within the first powershell '''...'''
env:
is required to access environment variables (see about_Environment_Variables in the Microsoft Docs)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With