prime
prime

Reputation: 799

Import properties file from jenkins

I'm using Jenkins 2.289.2 on RHEL8.

I tried to read a property file from the pipeline.

def properties = readProperties file :'my.properties'

When I build the project I got the below error. I installed Plugin Utilities API plugin in Jenkins. Am I missing any steps?

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

Upvotes: 0

Views: 804

Answers (1)

Noam Helmer
Noam Helmer

Reputation: 6824

If you are using a declarative pipeline this kind of statement must reside inside a script block. In general any scripted pipeline syntax or plain groovy DSL should be encapsulated inside a script block when used in a declarative pipeline.
You can read more about it in the Pipeline Syntax documentation.

To solve it try using something like:

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

Upvotes: 1

Related Questions