jenkins管道-powershell命令中的groovy和powershell变量

时间:2019-04-05 15:31:08

标签: powershell jenkins pipeline

我试图用值为WORKSPACE变量在jenkins管道内定义powershell变量。有办法吗?

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

输出:

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

很好。现在我需要将其包装到变量中:

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

现在输出:

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)

3 个答案:

答案 0 :(得分:0)

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}"
            }
        }         
    }
}

答案 1 :(得分:0)

pipeline {
    agent any

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

答案 2 :(得分:-1)

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