在初始分支創建時,jenkins 服務器中未配置 jenkinsfile 參數屬性 (jenkinsfile parameter properties not configured in jenkins server at initial branch creation)


問題描述

在初始分支創建時,jenkins 服務器中未配置 jenkinsfile 參數屬性 (jenkinsfile parameter properties not configured in jenkins server at initial branch creation)

我已經為jenkinsfile配置了以下屬性,但是從master分支創建新分支時它不適用於jenkins服務器。

#!groovy

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

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

在git中創建分支後,該分支在jenkins中可見帶有 Build Now 選項的服務器。

一旦我第一次從 jenkins 服務器運行分支,它就會更改為 Build with Parameters 選項。

p>

在 jenkinsfile 中有什麼我錯過的配置嗎?為什麼創建分支時jenkins服務器中沒有配置參數?


參考解法

方法 1:

You can get around this by using params.isValid instead of env.isValid.

方法 2:

Something I've used to get around this is to check if env.BUILD_NUMBER == "1" on a branch and then set some default values for my parameters to use on the initial run of that branch. For example:

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:

This seems to still be broken as of 2019. You can work around it by having an initial stage in your pipeline that sets the parameter value back into the environment variable. After that, you can reference it normally in subsequent stages.

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:

If you are using Declarative Pipeline, just add the following stage as your first stage.

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}"""
  }
}

This issue is being tracked on JENKINS‑41929.

(by Yahwe Rajpaul.letangPurrBiscuitWindlefelipecrs)

參考文件

  1. jenkinsfile parameter properties not configured in jenkins server at initial branch creation (CC BY‑SA 2.5/3.0/4.0)

#jenkins-plugins #jenkins-pipeline #jenkins-workflow #jenkins






相關問題

Email-ext Pre 腳本使用 (Email-ext Pre script usage)

分佈式 Jenkins - Linux 上的主控和 Windows 上的從屬 - 如何配置節點特定設置 (Distributed Jenkins- Master on Linux and slave on windows- How to configure node specific setting)

Jenkins插件開發不適用於maven 2 (Jenkins plugin development not working with maven 2)

Jenkins 全天運行構建和測試 (Jenkins running builds and tests all day long)

Jenkins jasmine-node 命令未找到 (Jenkins jasmine-node command not found)

在 jenkins 中更新 SVN 憑據 (Update SVN credentials in jenkins)

Jenkins 構建錯誤 - MSBUILD:錯誤 MSB1009:項目文件不存在 (Jenkins Build Error - MSBUILD : error MSB1009: Project file does not exist)

在初始分支創建時,jenkins 服務器中未配置 jenkinsfile 參數屬性 (jenkinsfile parameter properties not configured in jenkins server at initial branch creation)

如何將 Active Choices 反應參數傳遞給 Scriptler (How to Pass Active Choices Reactive Parameters to Scriptler)

配置通過 Dockerfile 安裝的 Jenkins 插件 (Configuring Jenkins plugin installed via Dockerfile)

使用觸發構建的用戶從 Jenkins 運行批處理作業 (Run a batch job from Jenkins with the user who triggered the build)

電子郵件擴展插件未加載通過配置文件提供程序插件添加的 groovy 模板 (Email Extension Plugin is not loading groovy template added via Config File Provider Plugin)







留言討論