【问题标题】:How to make sure list of parameters are updated before running a Jenkins pipeline?如何确保在运行 Jenkins 管道之前更新参数列表?
【发布时间】:2017-10-11 05:35:17
【问题描述】:

Jenkins 管道项目配置为从 Git 存储库获取其 Jenkinsfile

如果我更改参数列表,例如,从:

properties([
        parameters([
                string(name: 'FOO', description: 'Choose foo')
        ])
])

到:

properties([
        parameters([
                string(name: 'FOO', description: 'Choose foo'),
                string(name: 'BAR', description: 'Choose bar')
        ])
])

并运行构建,第一次运行并没有显示新添加的BAR参数:

由于更新后的 Jenkins 文件需要存在 BAR 参数,这会导致更改后的第一次构建失败,因为没有向用户提供输入该值的输入。

有没有办法防止这种情况发生?在显示参数输入页面之前确保Jenkinsfile是最新的?

【问题讨论】:

  • 你好吗(是你,这是 17 年的)打电话给BAR?我发现了类似的问题,我的解决方案是测试params.BARif (params.BAR == null) { def BAR = "baz" } else { echo "yes, BAR is set" }
  • 这里的 Jenkins 错误跟踪器上有一些关于这个问题的讨论:issues.jenkins-ci.org/browse/JENKINS-41929

标签: jenkins jenkins-pipeline


【解决方案1】:

简短回答:不。如果有一些工具可以解析和处理与构建分开的 Jenkinsfile,那就太好了,但没有。

Jenkins 在检索、解析和运行 Jenkinsfile 之前不知道新参数,唯一的方法是......运行构建。

实际上,构建历史将始终“落后于”Jenkinsfile;当您更改 Jenkinsfile 中的某些内容时,下一个构建将使用“旧”Jenkinsfile 运行,但之后会为构建选择并处理新的 Jenkinsfile。

【讨论】:

    【解决方案2】:

    解决这个问题的唯一方法 afaik 是手动添加一个“skip_run”布尔参数,而不是在作业的每个阶段添加一个 when{} 子句。

        properties([
            parameters([
                    BooleanParameter(name: 'skip_run', description: 'Skips all stages. Used to update parameters in case of changes.', default: False)
            ])
        ])
    

    ...

    stage('Doing Stuff') {
            when {
                expression { return params.skip_run ==~ /(?i)(N|NO|F|FALSE|OFF|STOP)/ }
            }
            steps {
                ...
            }
        }
    

    当然,这很容易出错。

    或者,您可以添加一个阶段作为管道的最开始并故意使构建失败。

    stage('Update Build Info only') {
            when {
                expression { return params.skip_run ==~ /(?i)(Y|YES|T|TRUE|ON|RUN)/ }
            }
            steps {
                error("This was done deliberately to update the build info.")
            }
        }
    

    更新: 感谢Abort current build from pipeline in Jenkins,我想出了这个解决方案:

    为了防止构建实际上显示为红色,您可以用 try 包裹它 - 捕获并优雅地退出构建。

    final updateOnly = 'updateOnly'     
    try {
          stage('Update Build Info only') {
                when {
                    expression { return params.skip_run ==~ /(?i)(Y|YES|T|TRUE|ON|RUN)/ }
                }
                steps {
                    error(updateOnly)
                }
            }
    ...
    //other stages here
    ...
        } catch (e) {
          if (e.message == updateOnly) {
            currentBuild.result = 'ABORTED'
            echo('Skipping the Job to update the build info')
            // return here instead of throwing error to keep the build "green"
            return
          }
          // normal error handling
          throw e
        }
    

    【讨论】:

      【解决方案3】:

      我有一个跳过构建的函数,除非作业具有所有必需的参数,例如:

      if (job.hasParameters(['FOO', 'BAR'])) {
          // pipeline code
      }
      

      【讨论】:

        【解决方案4】:

        几年前在 Jenkins 中报告了一个与此问题相关的问题 https://issues.jenkins-ci.org/browse/JENKINS-41929

        仍然开放,所以还没有优雅的解决方案。

        【讨论】:

          【解决方案5】:

          试试..

              parameters {
                  string(name: 'GRADLE_ARGS', defaultValue: '--console=plain', description: 'Gradle arguments')
              }
          
              environment{
                  GRADLE_ARGS = "${params.GRADLE_ARGS}"
              }
          

          【讨论】:

          • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多