将自定义作业 属性 添加到 Jenkins 作业

Adding custom job property to Jenkins job

我想添加一个新的强制作业 属性 来捕获 jenkins 作业中的一些自定义字段。我在插件列表中进行了搜索,但找不到任何可以解决该问题的相关插件。有什么插件可以解决这个问题吗? (注意:Extra columns 插件没有解决我的用例)

自由式作业可以配置为使用参数构建。参见:https://wiki.jenkins.io/display/JENKINS/Parameterized+Build

您可以配置参数类型(字符串、布尔值、下拉列表等),给出参数说明和默认值。字符串参数可以包括验证规则: https://wiki.jenkins.io/display/JENKINS/Validating+String+Parameter+Plugin

虽然这只会在当前参数值不符合正则表达式验证规则时发出警告,但不会阻止提交构建。但是,如果在此状态下提交,构建将失败。

从快速 google 来看,这似乎不适用于管道作业,请参阅上面插件页面 url 上来自 Miguelángel Fernández[=38 的最后评论=]:

如果您查看 class ValidatingStringParameterValue 的实现,您会发现它覆盖了 public BuildWrapper createBuildWrapper(AbstractBuild build) 的实现,如果该字符串无效。这仅适用于自由式作业和其他扩展 AbstractBuild 的作业类型。恐怕这不适用于管道作业。也许在您之前的项目中您使用了自由式作业。

自由式作业的另一种选择是在使用 'Prepare an environment for the run' 来自:
启动任何构建步骤之前进行作业验证 https://wiki.jenkins.io/display/JENKINS/EnvInject+Plugin

您需要编写 groovy 来检查提交的参数,如果值不合适,则此时中止构建。类似于:

def validateString = binding.variables.get('testParam')

if (!binding.variables.get('testParam').matches('\d+')) {
   println "failure of parameter validation - does not match regex"
   throw new InterruptedException()
} else {
   println "Validation passed carry on with build"
}

这不适用于管道构建 - 因为插件是引用:
'This plugin has some known limitations. For Example, Pipeline Plugin is not fully supported.'.

但是如果您使用的是脚本化管道,您可以实现类似的东西:

stage 'start up'
if(!env.testParam.matches('\d+')) {
    error 'failure of parameter validation - does not match regex'
}