【问题标题】:jenkinsfile parameter properties not configured in jenkins server at initial branch creation在初始分支创建时,jenkins 服务器中未配置 jenkinsfile 参数属性
【发布时间】:2016-11-24 09:21:37
【问题描述】:

我已经为 jenkinsfile 配置了以下属性,但是从 master 分支创建新分支时,它不适用于 jenkins 服务器。

#!groovy

properties([[$class: 'ParametersDefinitionProperty',
    parameterDefinitions: [
        [$class: 'StringParameterDefinition', name: 'isValid', defaultValue: 'false']
    ]
]])

node {
    stage 'Checkout'        
        checkout scm
    .....
    .....
}

在 git 中创建分支后,通过 Build Now 选项可以在 jenkins 服务器中看到该分支。

一旦我第一次从 jenkins 服务器运行分支,它就会变成 Build with Parameters 选项。

在 jenkinsfile 中有什么我错过的配置吗?为什么创建分支时jenkins服务器中没有配置参数?

【问题讨论】:

标签: jenkins jenkins-plugins jenkins-pipeline jenkins-workflow


【解决方案1】:

您可以使用params.isValid 而不是env.isValid 来解决此问题。

【讨论】:

    【解决方案2】:

    我用来解决这个问题的方法是检查 env.BUILD_NUMBER == "1" 是否在分支上,然后为我的参数设置一些默认值,以便在该分支的初始运行时使用。例如:

    node {
      properties([
        buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '15')),
        parameters([
          string(defaultValue: '', description: '', name: 'PLATFORM')
        ])
      ])
    
      stage("Testing") {
        // set a default for PLATFORM if it's the first build of a PR
        // as a workaround to parameters not being available on first run of build
        if (env.BUILD_NUMBER == "1") {
          PLATFORM = ''
        }
    
        if (PLATFORM.empty) {
          ....
        }
      }
    }
    

    【讨论】:

      【解决方案3】:

      截至 2019 年,这似乎仍未解决。您可以通过在管道中设置一个初始阶段将参数值设置回环境变量来解决它。之后,您可以在后续阶段正常引用它。

      parameters {
          string(name: 'SOME_VAR', defaultValue: 'foo', description: 'A custom environment variable')
      }
      
      ....
      
      stage('Verifying Environment Variables') {
          // This is too work around a jenkins bug on the first build of a multi-branch job
          // https://issues.jenkins-ci.org/browse/JENKINS-40574 - it is marked resolved but the last comment says it doesn't work for declaritive pipelines
          environment {
              SOME_VAR = "${params.SOME_VAR}"
          }
          steps {
              script {
                  env.SOME_VAR = env.SOME_VAR
              }
          }
      }
      ...
      

      【讨论】:

        【解决方案4】:

        如果您使用的是声明式管道,只需将以下阶段添加为您的第一个阶段。

        stage('Preparations') {
          steps {
            echo 'Initialize parameters as environment variables due to https://issues.jenkins-ci.org/browse/JENKINS-41929'
            evaluate """${def script = ""; params.each { k, v -> script += "env.${k} = \"${v}\"\n" }; return script}"""
          }
        }
        

        此问题正在JENKINS-41929 上进行跟踪。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-31
          • 1970-01-01
          • 1970-01-01
          • 2017-07-12
          相关资源
          最近更新 更多