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.
More on Declarative pipeline syntax
What is the minimum valid declarative pipeline?BeginnerWhat agent types exist in declarative pipelines?IntermediateWhen do you use `agent none` at the top level?IntermediateHow do nested sequential stages work and why are they useful?IntermediateList the `post` conditions and when each fires.IntermediateWhat are the most useful `when` conditions?Intermediate