如果checkout scm失败,如何在Jenkins声明性管道中获取git commit hash

时间:2019-09-13 11:04:57

标签: jenkins jenkins-pipeline jenkins-groovy

在Jenkins声明性管道中,带有选项{ skipDefaultCheckout() }的方法是否可以获取Jenkins甚至在调用checkout scm之前尝试签出的git commit?

我可以看到env变量: BRANCH_NAME但不提交

1 个答案:

答案 0 :(得分:1)

您可以尝试以下操作:

repoUrl = 'ssh://git@repo.addr/repo.git'
node {
    branchHash = sh(
        script: "echo -n \$( git ls-remote ${repoUrl} refs/heads/${env.BRANCH_NAME} | cut -f1 )",
        returnStdout: true
    ).trim()
}

pipeline {}
相关问题