简体   繁体   中英

Capturing shell script output from Jenkins Pipeline

I am trying to extract git branch and commit information in my Jenkinsfile as following:

def commit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
def branch = sh(returnStdout: true, script: 'git rev-parse --abbrev-ref HEAD').trim()

I am trying to print it afterwards like this:

println("Branch: ${branch}, Commit: ${commit}")

Instead of getting real values, I am left with this:

Branch: org.jenkinsci.plugins.pipeline.modeldefinition.ClosureModelTranslator@545511bf, Commit: org.jenkinsci.plugins.pipeline.modeldefinition.ClosureModelTranslator@545511bf

Am I doing something wrong and how can I retrieve values I need properly?

Edit: No, the suggested duplicate is not an answer, because I am aware of the shell commands used to retrieve the info I need. My Problem is the way that info is being delivered to me, as a ClosureModelTranslator instead of a String .

does this full pipeline work for you? working for me with Pipeline plugin 2.4.

pipeline {
  agent { label 'docker' }
  stages {
    stage("test_capture_output_and_print") {
      steps {
        script {
          def commitSha = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
          println("commitSha: ${commitSha}")
        }
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM