Passing Jenkins environment variable in Powershell script

北慕城南 提交于 2019-12-11 05:13:57

问题


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

!/bin/groovy

pipeline {

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:


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

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

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



来源:https://stackoverflow.com/questions/50547232/passing-jenkins-environment-variable-in-powershell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!