繁体   English   中英

在Powershell脚本中传递Jenkins环境变量

[英]Passing Jenkins environment variable in Powershell script

我想在Power Shell脚本中使用jenkins环境变量。在Powershell脚本中$ {destination}为null。无法识别我在做什么错误。请帮助

!/ bin / groovy

管道{

agent {
    label {
        label ""
        customWorkspace "C:\\Jenkins\\workspace"
    }
}
environment {       

    def destination=''
}
options {
    timestamps()
    timeout(time: 60, unit: 'MINUTES')      
    skipDefaultCheckout(true)
    disableConcurrentBuilds()
 }

stages {

    stage('TEST') 
    {     
        steps {
                script{
                    destination="\\\\SERVERNAME\\d\$"
                }
                echo "${destination}"

                powershell '''

                    $destinationPath ="${destination}"

                    write-host $destinationPath

                    write-host "test3" '''

            }  
    }
}
post {
    always {
        deleteDir()         
    }   

}

您可以使用以下两种方法中的一种来解决此问题:

  1. 使用“”而不是“”可以用其值替换destination 。使用这种方法时,应转义Powershell的变量标识符以避免不必要的替换,例如: \\$destinationPath = "${destination}"

  2. 将您的Jenkins变量导出为环境变量: withEnv(["DESTINATION=$destination"]) { powershell ''' $destinationPath ="$env:DESTINATION" ... ''' }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM