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

6
我想在PowerShell脚本中使用Jenkins环境变量。在脚本中,${destination}的值为空。我无法确定我的错误。请帮我看看。
# !/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()         
        }   
}

不要使用 ${destination} ,请使用 %destination% - Manmohan_singh
可能是 https://dev59.com/caXja4cB1Zd3GeqPS42I 的重复问题。 - zionyx
%destination%将不会被解析。 - Marcel Melzig
1个回答

11

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

  1. 使用"""而不是',以便能够替换destination的值。在使用此方法时,应该转义Powershell的变量标识符以避免不必要的替换,如下所示:\$destinationPath = "${destination}"

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接