jenkins-pipeline - 在声明性 Jenkinsfile 中首先运行脚本以使用扩展选择参数插件
问题描述
我正在尝试运行一个脚本来实例化扩展选择参数变量以在声明性 jenkinsfile 属性部分中使用它,但是我无法在没有步骤的情况下在 jenkinsfile 中运行脚本。我不想将其作为输入步骤或脚本管道。
所以我运行它首先是一个节点步骤,然后是一个管道步骤,如下所示:
import com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition
node('MyServer') {
try {
def multiSelect = new ExtendedChoiceParameterDefinition(...)
properties([ parameters([ multiSelect ]) ])
}
catch(error){
echo "$error"
}
}
pipeline {
stages {
....
}
}
神奇地它起作用了!需要注意的是,只有当我之前只使用管道块运行过构建时。
那么,有没有更好的方法将以前的脚本运行到管道?能够在嵌入脚本块的步骤之外为属性或其他地方创建对象?
解决方案
I would rather go for parameters block in pipeline.
The parameters directive provides a list of parameters which a user should provide when triggering the Pipeline. The values for these user-specified parameters are made available to Pipeline steps via the params object, see the Example for its specific usage.
pipeline {
agent any
parameters {
string(name: 'PERSON', defaultValue: 'Mr Jenkins', description: 'Who should I say hello to?')
text(name: 'BIOGRAPHY', defaultValue: '', description: 'Enter some information about the person')
booleanParam(name: 'TOGGLE', defaultValue: true, description: 'Toggle this value')
choice(name: 'CHOICE', choices: ['One', 'Two', 'Three'], description: 'Pick something')
password(name: 'PASSWORD', defaultValue: 'SECRET', description: 'Enter a password')
file(name: "FILE", description: "Choose a file to upload")
}
stages {
stage('Example') {
steps {
echo "Hello ${params.PERSON}"
echo "Biography: ${params.BIOGRAPHY}"
echo "Toggle: ${params.TOGGLE}"
echo "Choice: ${params.CHOICE}"
echo "Password: ${params.PASSWORD}"
}
}
}
}
推荐阅读
- r - 如何在igraph R中的图形组件之间添加边
- visual-studio-code - 我想摆脱 vs-code 中的行长指示符
- javascript - JavaScript 将代理 getter/setter 绑定到特定上下文
- user-interface - 有没有办法像在 excel 中使用 VBA 一样“单步执行”Fortran 代码?
- bigdata - Splunk 序列关于跟随评估案例函数
- node.js - 安装 AWS CDK 构造库时出现“ERESOLVE 无法解析依赖关系树”
- python - 使用 Google Sheets API 选择多个列
- java - 使用 Java 16 时 Tomcat 未找到 Spring 初始化程序类
- javascript - 未使用 react-router 匹配的自定义组件
- python - 如何在 python web socket-client 库中使用 async on_message 函数?