如何在Jenkinsfile中打印类属性的值?

时间:2019-04-17 19:17:57

标签: jenkins github groovy jenkins-pipeline

我正在创建一个用于GitHub Enterprise的Jenkinsfile。我使用管道作业中的GUI设置从GitHub存储库中指定Jenkinsfile。 我使用的是脚本语法而不是声明性语法。 我可以使用checkout scm在我的Jenkinsfile中签出回购协议。我想在脚本中使用一些有关签出的信息,例如分支名称和提交哈希。但是,我不知道如何访问scm类的变量。

运行作业时,它在Checkout阶段失败。从git检出似乎可以正常工作,但是它失败,没有打印任何错误。如果我删除echo scm.GIT_BRANCH行,则可以正常工作。

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

以下是输出:

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

1 个答案:

答案 0 :(得分:1)

您可以使用checkout scm返回值获取git信息

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