【问题标题】:Create new Env Variable using existing Env Variables in Jenkins Pipeline使用 Jenkins Pipeline 中的现有环境变量创建新的环境变量
【发布时间】:2017-08-09 04:41:05
【问题描述】:

我正在尝试生成一些环境。 jenkins 管道项目中的变量并希望将它们注入到我的构建中,但到目前为止我无法这样做,在下面提到的 sn-p 中,我已经创建了一个 env 变量,即 build=20160107 随机内部版本号,现在我执行一个批处理脚本名称 CalculateTempEnvVariablesForBuild.bat 生成 TempEnvVariables.properties 文件。此属性文件包含 year、Week_number、week_day 的更新值 现在我需要根据 ${year}0${Week_number}${week_day}

更新我的构建变量
pipeline {
    agent { label 'Build_Machine_Windows_Server' }
    stages {
       stage('Build') {
          steps {
            dir ('E:\\\\Jenkins'){
                 bat 'call E:\\Jenkins\\CalculateTempEnvVariablesForBuild.bat'   

            }  
             load "E:\\Jenkins\\TempEnvVariables.properties" 

             withEnv(['build=${year}0${Week_number}${week_day}']) {
                      echo "${build}"    
                    }

             echo "${year}"
             echo "${week_day}"
             echo "${Week_number}"
          }
       }
    }
}

当我回显 ${build} 时,它没有给我更新值,而是给我纯字符串 -> ${year}0${Week_number}${week_day} 我需要的是20173203 当我分别回显 year、week_day 和 Week_number 时,我得到正确的值,例如 17,32,03

我猜我没有正确设置/注入变量(可能是语法错误)

【问题讨论】:

    标签: jenkins groovy build environment-variables jenkins-pipeline


    【解决方案1】:

    管道代码使用单引号。根据the docs,这意味着插值不是由 groovy 完成的,而是由底层 shell 完成的:

    (注意这里我们在 Groovy 中使用了单引号,所以变量 扩展是由 Bourne shell 完成的,而不是 Jenkins。)

    尝试使用双引号:

    withEnv(["build=${BUILD_NUMBER}"]) {
        echo env.build
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 1970-01-01
      • 2012-05-11
      • 2017-01-03
      • 1970-01-01
      • 2018-09-21
      • 2021-03-12
      相关资源
      最近更新 更多