我的要求很简单,我只想外部化一些“值”以使我的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}'
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 尝试在Jenkins声明性管道脚本中使用cron触发的版本:
options
{
disableConcurrentBuilds()
timeout(time: 1, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr: '5'))
pipelineTriggers([cron('H 0 * * *')])
}
Run Code Online (Sandbox Code Playgroud)
运行时,抛出此异常:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 30: Invalid option type "pipelineTriggers". Valid option types: [buildDiscarder, catchError, disableConcurrentBuilds, overrideIndexTriggers, retry, script, skipDefaultCheckout, skipStagesAfterUnstable, timeout, timestamps, waitUntil, withContext, withCredentials, withEnv, ws] @ line 30, column 3.
pipelineTriggers([cron('H 0 * * *')])
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:67)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:430)
at …Run Code Online (Sandbox Code Playgroud)