Interview Prep/Jenkins/Declarative pipeline syntax

How do you capture a command's output or exit code with `sh`?

BeginnerConceptDeclarative pipeline syntax
Answer
script {
  def sha = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
  def rc  = sh(script: 'grep -q TODO src/*.js', returnStatus: true)
  if (rc == 0) { unstable('TODOs found') }
  env.SHORT_SHA = sha
}

returnStdout includes the trailing newline, so always .trim(). returnStatus prevents a non-zero exit from failing the build.

All Jenkins questions Roadmap: Jenkins

More on Declarative pipeline syntax