Sowmya Md
Sowmya Md

Reputation: 43

syntax issue while loading groovy script

While loading groovy script in declarative pipeline, I keep getting a syntax issue:

[Pipeline] script
[Pipeline] {
[Pipeline] container
[Pipeline] {
[Pipeline] pwd
[Pipeline] sh
11:29:01  + /home/jenkins/workspace/CTO/CSF/Analytics/CSF-CJUP-TEST-CI/Jenkins/interactive.groovy
11:29:01  /home/jenkins/workspace/CTO/CSF/Analytics/CSF-CJUP-TEST-CI/Jenkins/interactive.groovy: line 2: syntax error near unexpected token `('
11:29:01  /home/jenkins/workspace/CTO/CSF/Analytics/CSF-CJUP-TEST-CI/Jenkins/interactive.groovy: line 2: `def exampleMethod() {'
[Pipeline] }
[Pipeline] // container
[Pipeline] }

Relavent code:

steps {
    container('tools') {
        if ( env.RELEASE == 'true' ) {
            sh "${pwd()}/Jenkins/interactive.groovy"
            def interactive = load "${pwd()}/Jenkins/interactive.groovy"
            interactive.exampleMethod()
        }
    }
}

interactive.groovy

def exampleMethod() {
   println("exampleMethod")
}

def otherExampleMethod() {
    println("otherExampleMethod")
}
return this

Upvotes: 1

Views: 54

Answers (1)

smelm
smelm

Reputation: 1334

The problem is this line:

sh "${pwd()}/Jenkins/interactive.groovy"

since you are not using a shebang in the groovy file (something like #!/usr/bin/groovy in the first line) the shell tries to interpret it as a bash script, which causes the error.

Just remove this line and use the load step below.

Upvotes: 1

Related Questions