【问题标题】:How to add a build selector as a parameter?如何添加构建选择器作为参数?
【发布时间】:2021-07-31 10:25:56
【问题描述】:

我有两份工作:

  1. 构建作业
  2. 部署作业

我将第一个作业中生成的工件(jar)复制到第二个作业中,并将它们部署到环境中。

properties([
  parameters(
    [
      string(
        name: 'buildnumber',
        description: 'Buildnumber to deploy'
      ),
      choice(
        name: 'env',
        choices: ['qa', 'stage', 'prod'],
        description: 'Environment where the app should be deployed'
      )
    ]
  )
])

node{

    stage('Copy artifacts'){
          copyArtifacts(projectName: 'my-demo-build-job/master', selector: specific(params.buildnumber))

    }
    stage('Deploy'){
        sh 'Deploying to the specified environment '
    }
}

有了这个,我必须手动检查最新/成功的构建并将其作为参数。有没有一种方法可以让我们获得一个下拉列表,其中包含所有按内部版本号排序的成功构建作为其他作业的选择器?

【问题讨论】:

    标签: jenkins continuous-integration jenkins-pipeline jenkins-plugins jenkins-groovy


    【解决方案1】:

    您可以使用 Extended Choice Parameter Plugin 借助 groovy 脚本实现您想要的功能。
    您将需要定义 单选 类型的扩展选择参数,作为值的来源选择 Groovy 脚本,并作为 groovy 脚本使用如下内容:

    def job = jenkins.model.Jenkins.instance.getItemByFullName('my-demo-build-job/master')
    return job.builds.findResults{
        it.result == hudson.model.Result.SUCCESS ? it.getNumber().toInteger() : null
    }
    

    此脚本将检查已配置作业的所有构建并仅过滤掉成功的构建 - 这将作为参数的选择列表选项返回。
    管道中的配置如下所示:

    properties([
            parameters([
                    extendedChoice(name: 'buildnumber', type: 'PT_SINGLE_SELECT', description: 'Buildnumber to deploy', visibleItemCount: 10, groovyScript:
                            '''def job = jenkins.model.Jenkins.instance.getItemByFullName('my-demo-build-job/master')
                               return job.builds.findResults { it.result == hudson.model.Result.SUCCESS ? it.getNumber().toInteger() : null }''',
                    choice(name: 'env', choices:  ['qa', 'stage', 'prod'], description: 'Environment where the app should be deployed'),
            ])
    ])
    

    【讨论】:

    • 结果会按照内部版本号降序排列吗?
    • 是的,因为它已经在job.builds 列表中排序。此外,您始终可以使用reverse() 函数反转顺序:job.builds.reverse() ...
    • 有没有办法在另一个布尔参数设置为true时动态显示这个参数,否则显示一组不同的参数?
    • 是的,使用Active Choices Plugin 是可能的,请参阅使用反应参数的示例部分,其中有一个更适合您想要的示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多