【问题标题】:Passing Jenkins environment variable in Powershell script在 Powershell 脚本中传递 Jenkins 环境变量
【发布时间】:2018-05-26 21:43:13
【问题描述】:

我想在 power shell 脚本中使用 jenkins 环境变量。这里 ${destination} 在 powershell 脚本中为 null。无法确定我在做什么。请帮助

# !/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()         
        }   
}

【问题讨论】:

标签: jenkins jenkins-pipeline declarative


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    • 2017-02-03
    • 2020-05-03
    • 1970-01-01
    • 2021-10-25
    相关资源
    最近更新 更多