問題描述
在初始分支創建時,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 Raj、paul.letang、PurrBiscuit、Windle、felipecrs)