【问题标题】:How do you print out the value of a class property in a Jenkinsfile?如何在 Jenkinsfile 中打印出类属性的值?
【发布时间】:2019-09-08 02:44:54
【问题描述】:

我正在创建一个用于 GitHub Enterprise 的 Jenkinsfile。我使用管道作业中的 GUI 设置来指定来自 GitHub 存储库的 Jenkinsfile。 我正在使用脚本语法而不是声明性语法。 我可以使用checkout scm 在我的 Jenkinsfile 中签出 repo。我想在我的脚本中使用一些关于结帐的信息,例如分支名称和提交哈希。但是,我不知道如何访问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

【问题讨论】:

    标签: jenkins github groovy jenkins-pipeline


    【解决方案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
    

    【讨论】:

    • 这导致失败,输出与我原来的帖子相同。 GIT_BRANCH 和 GIT_COMMIT 参数是否可能仅在多分支管道作业中可用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 2021-09-18
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多