【问题标题】:How to refresh the job configuration from pipeline in SCM如何从 SCM 中的管道刷新作业配置
【发布时间】:2018-12-04 14:31:23
【问题描述】:

如果我向 SCM 中保存的管道脚本添加一个新的作业参数,那么如何使用新参数更新 UI? (没有运行并且作业失败,因为管道尚未从 SCM 中拉出)

为了更详细,我在 SCM 中的管道脚本中添加了一个新参数,如下所示:

choice(
    choices: ['github', 'bitbucket'],
    description: 'Which repo to build from',
    name: 'repo')

然后在 UI 中单击作业 -> '使用参数构建'。现有参数选择不包含新参数,因为 Jenkins 尚未从 SCM 检索它。如果我现在运行它会失败(但同时它会更新参数列表,所以下次我“使用参数构建”时它会在那里。

【问题讨论】:

  • 将参数添加到相互触发的许多作业时,这是一种特别的痛苦。

标签: jenkins jenkins-pipeline jenkins-declarative-pipeline


【解决方案1】:

这里的主要问题是您希望基于环境变量或构建参数的管道中的可选/条件stage。 Jenkins 声明性管道有 conditional stages,但它们不适用于脚本化管道(Jenkinsfile 中的 groovy)。

我对 groovy 闭包进行了即兴创作,以跳过基于布尔构建参数的实际构建阶段。

示例代码

import org.jenkinsci.plugins.pipeline.modeldefinition.Utils

/**
 * Stage execution helper for skipping stages based on flag.
 *
 * @argument name of the stage
 * @argument execute boolean flag
 * @argument block to be execute if flag true
 */
def stage(name, execute, block) {
    return stage(name, execute ? block : {
        echo "Skipped stage $name"
        Utils.markStageSkippedForConditional(STAGE_NAME)
    })
}

node('master') {
    try {
        def parameterList = []
        // additional parameters
        parameterList.add(booleanParam(name: 'JUST_UPDATE_JOB_CONFIG', description: 'Just Update the configuration of this job'))
        properties([
            parameters(parameterList)
        ])
        stage('Update-JobConfig', JUST_UPDATE_JOB_CONFIG == 'true') {
            echo 'Updated Job Config'
        }
        stage('Build Stage 1', JUST_UPDATE_JOB_CONFIG != 'true') {
            // some steps
        }
        stage('Build Stage 2', JUST_UPDATE_JOB_CONFIG != 'true') {
            // some steps
        }
    } catch (e) {
        throw e
    }
}

当我在 SCM 中更改我的管道代码时,我只是在检查 JUST_UPDATE_JOB_CONFIG 参数的情况下构建作业。这里的技巧是def stage(...,它实际上会根据标志跳过各个阶段。

不确定这是否是正确的做法,但它对我有用。

【讨论】:

  • 有趣。我想这是一种方法。我正在使用一个声明性管道,它是一个 Jenkinsfile。我理解声明式管道和脚本式管道之间的区别仅仅是语法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多