jenkins - 可锁定资源插件 Jenkinsfile -> 动态
问题描述
这是一个声明性的 Jenkins-Pipeline。
使用可锁定资源插件(https://plugins.jenkins.io/lockable-resources/)我想动态锁定多个阶段,具体取决于用户在参数部分选择的环境。这就是我希望这样做的方式:
pipeline {
parameters {
choice choices: ['---', 'prod', 'test'], description: 'Environment', name: 'environment'
}
stage('MY_APPLICATION') {
options{
lock('resource': "${params.environment}")
}
stages {
stage('TEST') {
when { expression { "${params.environment}" == 'prod' } }
steps { ... }
}
stage('PROD') {
when { expression { "${params.environment}" == 'test' } }
steps { ... }
}
}
}
}
但我无法访问选项块中的参数,它始终使用默认值。有谁知道如何根据环境变量动态锁定资源?
解决方案
我设法像这样解决它:
在选项块中,有可用的 $currentBuild 变量,这样就可以动态锁定资源:
pipeline {
parameters {
choice choices: ['---', 'prod', 'test'], description: 'Environment', name: 'environment'
}
stage('MY_APPLICATION') {
options{
lock('resource': "${currentBuild.getRawBuild().getEnvironment(TaskListener.NULL).environment}")
}
stages {
stage('TEST') {
when { expression { "${params.environment}" == 'prod' } }
steps { ... }
}
stage('PROD') {
when { expression { "${params.environment}" == 'test' } }
steps { ... }
}
}
}
}
推荐阅读
- javascript - mustache Handlebars.js 部分获取列表中的所有对象
- testing - 从 Prometheus 指标中减去值 x 以获得 Grafana“单一统计”并激活“Delta”?
- javascript - 笑话:导入的类“未定义”
- oracle - 在存储过程 (Oracle) 中按视图名称打开视图 SQL
- ios - ios设备上的recaptcha问题
- c# - 从 WPF 中的 ComboBox 获取值
- android - 如何从 AutoCompleteTextView 中的 id 获取字段?
- python - python pandas中if else语句的lambda函数
- android - 为 IntDef 字段分配错误值时,Android Studio 和 Lint 不会生成错误
- kubernetes - Kubernetes autoscaler:在特定日期更改最小副本