简体   繁体   中英

How do you print out the value of a class property in a Jenkinsfile?

I'm creating a Jenkinsfile for use with GitHub Enterprise. I used the GUI settings in the pipeline job to specify a Jenkinsfile from a GitHub repo. I'm using the scripted syntax instead of the declarative syntax. I am able to checkout the repo in my Jenkinsfile using checkout scm . I want to use some information about the checkout in my script, such as the branch name and commit hash. However, I can't figure out how to access variables of the scm class.

When I run the job, it fails in the Checkout stage. The checkout from git seems to work properly, but it fails without printing any errors. If I delete the echo scm.GIT_BRANCH line it works fine.

node {
  stage('Checkout') {
    checkout scm
    echo scm.GIT_BRANCH
  }
}

Here's the output:

Started by user spark
Obtained nightly/Jenkinsfile from git https://github.enterprise.instance.com/spark/ci_flow_test
[Pipeline] node
Running on jenkins-server in /home/spark/ci_flow_test/pipeline_test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Checkout)
[Pipeline] checkout
 > /apps/git/git18/bin/git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > /apps/git/git18/bin/git config remote.origin.url https://github.enterprise.instance.com/spark/ci_flow_test # timeout=10
Fetching upstream changes from https://github.enterprise.instance.com/spark/ci_flow_test
 > /apps/git/git18/bin/git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > /apps/git/git18/bin/git fetch --tags --progress https://github.enterprise.instance.com/spark/ci_flow_test +refs/heads/*:refs/remotes/origin/*
 > /apps/git/git18/bin/git rev-parse refs/remotes/origin/working^{commit} # timeout=10
 > /apps/git/git18/bin/git rev-parse refs/remotes/origin/origin/working^{commit} # timeout=10
Checking out Revision 396f172c6061ba2760a71cba817df24836ec7e3b (refs/remotes/origin/working)
Commit message: "try echo"
 > /apps/git/git18/bin/git config core.sparsecheckout # timeout=10
 > /apps/git/git18/bin/git checkout -f 396f172c6061ba2760a71cba817df24836ec7e3b
 > /apps/git/git18/bin/git rev-list 778c36171927020bd1afbd7206d86bf94abd1ed8 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] mail
[Pipeline] echo
Post script
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: FAILURE

You can use the checkout scm return value to get git infomation

node {

    stage('Checkout') {
        def d = checkout scm

        echo "branch: " + d.GIT_BRANCH
        echo "commit: " + d.GIT_COMMIT
    }
}

// supported fields
GIT_AUTHOR_EMAIL 
GIT_AUTHOR_NAME 
GIT_BRANCH 
GIT_COMMIT
GIT_COMMITTER_EMAIL 
GIT_COMMITTER_NAME 
GIT_LOCAL_BRANCH 
GIT_PREVIOUS_COMMIT
GIT_PREVIOUS_SUCCESSFUL_COMMIT
GIT_URL

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