在Powershell脚本中传递Jenkins环境变量 [英] Passing Jenkins environment variable in Powershell script

查看:120
本文介绍了在Powershell脚本中传递Jenkins环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i would like to use a jenkins environment variable inside a power shell script.Here ${destination} is coming as null inside powershell script.Not able to identify what is the mistake i am doing.Please help

管道{

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()         
    }   

}

推荐答案

您可以使用两种方法中的一种来解决此问题,以最适合您的一种:

You can resolve this using either one of two methods, whichever suits you best:

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

  1. Use """ instead of ''' to be able to substitute destination with its value. When using this approach you should escape Powershell's variable identifiers to avoid unwanted substitutions, like so: \$destinationPath = "${destination}"

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

Export your Jenkins variables as environment variables: withEnv(["DESTINATION=$destination"]) { powershell ''' $destinationPath ="$env:DESTINATION" ... ''' }

这篇关于在Powershell脚本中传递Jenkins环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆