从詹金斯导入属性文件

Import properties file from jenkins

我在 RHEL8 上使用 Jenkins 2.289.2。

我试图从管道中读取 属性 文件。

def 属性 = 读取属性文件:'my.properties'

当我构建项目时出现以下错误。我在 Jenkins 中安装了 Plugin Utilities API 插件。我是否遗漏了任何步骤?

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 35: Expected a step @ line 35, column 9.
            def properties = readProperties file :'my.properties''
            ^

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.doParse(CpsGroovyShell.java:142)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:571)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:523)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:337)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE

如果您使用的是声明性管道,则此类语句必须驻留在 script 块中。通常,在声明性管道中使用时,任何脚本化管道语法或纯 groovy DSL 都应封装在 script 块中。
您可以在 Pipeline Syntax 文档中阅读更多相关信息。

要解决它,请尝试使用类似的方法:

pipeline {
   agent any
   stages {
       stage('Hello') {
           steps {
               script {
                   def properties = readProperties file :'my.properties'
                   // Rest of code
               }
           }
       }
   }
}