我的要求很简单,我只想将一些“值”外部化以使我的 Jenkinsfile 更可重用,为此我需要从 Jenkinsfile 旁边的文件中加载属性,并确保这些属性是可在管道中的任何地方使用。我对 groovy 和 Jenkins 代码仍然很陌生,但从未想过这么简单的事情会如此困难。我在脚本安全插件中启用了一些方法,但以下代码(以及我尝试过的几种变体)总是会出现错误或打印 null 或给我 NPE。我尝试了多种组合,下面的代码只是其中之一。
properties = null
@NonCPS
def loadProperties() {
checkout scm
File propertiesFile = new File('${workspace}/pipeline.properties')
propertiesFile.withInputStream {
properties.load(propertiesFile)
}
}
pipeline {
agent none
stages {
stage ('prepare') {
agent any
steps {
script {
loadProperties()
echo "${properties['repo']}"
}
}
}
stage('Build') {
agent any
steps {
sh 'echo ${properties.repo}'
}
}
}
}