Tim
Tim

Reputation: 2066

Failed to get the output of jenkins pipeline sh step result inside Declarative Pipeline

I would like to get an output of my sh result by using this way:

pipeline {
   agent any

   stages {
      stage('Hello') {
         steps {
            echo 'Hello World'
            result=sh(script:'ls -al', returnStdout: true)
         }
      }
   }
}

Then it prints an error to me:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 8: Expected a step @ line 8, column 13.
           result=sh(script:'ls -al', returnStdout: true)
           ^

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:129)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:123)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:517)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:480)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:268)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)

Finished: FAILURE

If an use the traditional script way, it works very well.

node {

stage("Fetch code from git") {
        echo 'Hello World'
        result = sh(script:'ls -al', returnStdout: true)
        echo result
}

}

Upvotes: 1

Views: 1205

Answers (3)

Dashrath Mundkar
Dashrath Mundkar

Reputation: 9174

do something like this

pipeline {

  agent any

  stages {
    stage('Hello') {
     steps {
      sh 'echo Hello World'
      scripts {
        result = sh(script:'ls -al', returnStdout: true)
       }
      }
    }
  }
}

Upvotes: 0

Matthew Schuchard
Matthew Schuchard

Reputation: 28739

Since you are assigning the return value of the method invocation to a variable, this now becomes scripted, and you will need to encapsulate the step within a script block for declarative DSL:

pipeline {
   agent any

   stages {
      stage('Hello') {
         steps {
            echo 'Hello World'
            script {
              result=sh(script:'ls -al', returnStdout: true)
            }
         }
      }
   }
}

Upvotes: 3

Ashwin M
Ashwin M

Reputation: 21

The shell commands must be run inside a sh block in a declarative pipeline.

eg:

pipeline {
   agent any

   stages {
      stage('Hello') {
         steps {
            sh 'echo "Hello World"'
            result=sh(script:'ls -al', returnStdout: true)
         }
      }
   }
}

You can also refer to https://www.jenkins.io/doc/pipeline/tour/running-multiple-steps/ for more information.

Upvotes: 0

Related Questions