简体   繁体   中英

How can Jenkinsfile extract value from console output?

How can I get Jenkins to extract a value from the console output that is generated by a command in a Jenkinsfile?

Specifically, how can I get Jenkins to extract the commit hash from the results of the checkout scm command in the following very simple Jenkinsfile?


Jenkinsfile

 node { // Clean workspace before doing anything deleteDir() try { stage ('Clone') { checkout scm } } catch (err) { currentBuild.result = 'FAILED' throw err } } 


Output Of checkout scm command:

The Jenkins log prints the following as a result of running the checkout scm command in the above simplified Jenkinsfile:

 Cloning the remote Git repository Cloning with configured refspecs honoured and without tags Cloning repository http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git > git init /var/jenkins_home/workspace/le-repo_sample-issue-branch-2WOFDGRDQWR367VAM7O26H2DKPTRVPDKRTGNRIS4AQNNFP7QIX2Q # timeout=10 Fetching upstream changes from http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git > git --version # timeout=10 using GIT_ASKPASS to set credentials > git fetch --no-tags --progress http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git +refs/heads/sample-issue-branch:refs/remotes/origin/sample-issue-branch > git config remote.origin.url http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git # timeout=10 > git config --add remote.origin.fetch +refs/heads/sample-issue-branch:refs/remotes/origin/sample-issue-branch # timeout=10 > git config remote.origin.url http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git # timeout=10 Fetching without tags Fetching upstream changes from http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git using GIT_ASKPASS to set credentials > git fetch --no-tags --progress http://<bitbucket-ip-on-lan>:7990/scm/JSP/jenkinsfile-simple-repo.git +refs/heads/sample-issue-branch:refs/remotes/origin/sample-issue-branch Checking out Revision 77cf12f42136efc77fecbcd1f761a54254278cb3 (sample-issue-branch) > git config core.sparsecheckout # timeout=10 > git checkout -f 77cf12f42136efc77fecbcd1f761a54254278cb3 Commit message: "add whitespace" > git rev-list --no-walk e975fb4391677bc09f2056b3e8a6be62eda0b222 # timeout=10 [Bitbucket] Notifying commit build result 


Restated Question:

Specifically, what do we add to the Jenkinsfile in order for the log to additionally print out the following at the end:

 Commit hash is: 77cf12f42136efc77fecbcd1f761a54254278cb3 

This is obviously over simplified. The printed output in real life will go into a variable and be passed as an argument into a script. But for this OP, what specific syntax will enable Jenkins to extract the commit hash?

If you need commit hash in particular, the checkout step's documentation mentions an easy way to get it (rather than parse console output etc):

This step returns a Map of any variables the SCM plugin would set in a Freestyle job, so if your SCM is git, you can do:

  def scmVars = checkout scm def commitHash = scmVars.GIT_COMMIT // or def commitHash = checkout(scm).GIT_COMMIT 

Once you have it in a variable it's just a matter of echoing it to see it in the log.

echo "Commit hash is: ${commitHash}"

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