Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a parameter to a powershell script in a jenkins pipeline

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.

like image 624
NicolasW Avatar asked Oct 06 '17 20:10

NicolasW


1 Answers

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)
like image 78
matt9 Avatar answered Sep 30 '22 11:09

matt9