Jenkins Pipeline Access stage steps variable in power shell execution

Viewed 2163

I want to access normal variable defined in my steps into power shell execution. We can access environment variables using $env but, how to access normal step variables?

stages {
        stage ('sometask') {
                steps{
                     script {
                        def someString = 'Hi'
                        withCredentials(...) {
                        def out = powershell(returnStdout: true, script: 
                               '''
                               // Access someString 
                         ''')
                         println out
                       }
                }
         }
}
2 Answers

It isn't probably the most elegant way to do it but I've done it like this: (I will use your same example)

stages {
        stage ('sometask') {
                steps{
                     script {
                        def someString = 'Hi'
                        withCredentials(...) {
                        def out = powershell(returnStdout: true, script: 
                               '''
                        PowerShellCommands.... 
                        Write-Host ''' + someString + '''
                        someMorePowerShellCommands
                         ''')
                         println out
                       }
                }

So basically what I'm doing is concatenating the value of

someString

In the middle of the script, if you would for instance need to pass more variables you need to follow the same logic:

''' some script code ''' + variableName + 
''' some more script code''' + anotherVariableName + 
''' evenMoreScriptCode '''

In my case I am using config parameters and instead of the

variableName

I am using

config.webURL

for instance.

Hope this helps.

EN

Related