0

I trying to define powershell variable inside jenkins pipeline with the WORKSPACE variable in value. Is there a way to do this?

stage ('BUILD') {
        steps {
            powershell ("""Write-Output ${WORKSPACE}\\One\\Two\\""")
            }
    }

Output:

    [Pipeline] powershell
    18:18:15  C:\buildenv\Jenkins\workspace\project\One\Two\
    [Pipeline] }
    [Pipeline] // stage

That's nice. Now i need to wrap it into variable:

stage ('BUILD') {
        steps {
            powershell ("""$name02=${WORKSPACE}\\One\\Two\\""")
            }
    }

Output now:

groovy.lang.MissingPropertyException: No such property: name02 for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:264)
at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:288)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:292)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:268)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:268)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at WorkflowScript.run(WorkflowScript:50)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.delegateAndExecute(jar:file:/C:/buildenv/Jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:134)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(jar:file:/C:/buildenv/Jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:679)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(jar:file:/C:/buildenv/Jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:414)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(jar:file:/C:/buildenv/Jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:412)
at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(jar:file:/C:/buildenv/Jenkins/plugins/pipeline-model-definition/WEB-INF/lib/pipeline-model-definition.jar!/org/jenkinsci/plugins/pipeline/modeldefinition/ModelInterpreter.groovy:678)
1

3 Answers 3

1
name02=''
pipeline {
    agent any

    stages{
        stage ('BUILD') {
            steps {
                script{
                    name2 = powershell label: '', returnStdout: true, script: "return \"${Workspace}\\One\\Two\\\""
                }
            }
        }
        stage ('Print') {
            steps {
                echo "name2 = ${name2}"
            }
        }         
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1
pipeline {
    agent any

    stages{
        stage ('BUILD') {
            steps {
                script{
                    powershell """
                        \$5name2 = "${Workspace}\\One\\Two\\"
                        Write-Output "name2 = \$5name2"
                    """
                }
            }
        }
    }
}

Comments

-1
def result = powershell returnStdout: true, script: '''
                        $result = ${WORKSPACE}\\One\\Two\\
                        return $result 
                    '''

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.