Jenkins pipeline git checkout在默认checkout被跳过时访问GIT_COMMIT

cpjpxq1n  于 11个月前  发布在  Jenkins
关注(0)|答案(1)|浏览(144)

意见:

Jenkinsfile

pipeline {
    agent { node 'master' }

    stages {
        stage('Hello') {
            steps {
                echo env.GIT_COMMIT
            }
        }
    }
}

字符串
输出量:

[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] echo
e5b4b2deed1c8db486cef5641ef14c61fd48f9ed
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline


Jenkinsfile

pipeline {
    agent { node 'master' }
    
    options {
        skipDefaultCheckout(true)
    }

    stages {
        stage('Hello') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: '/tmp/r']]])
                echo env.GIT_COMMIT
            }
        }
    }
}


输出量:

[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/HelloWorld_master
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] checkout
Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
No credentials specified
 > git rev-parse --resolve-git-dir /var/jenkins_home/workspace/HelloWorld_master/.git # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url /tmp/r # timeout=10
Fetching upstream changes from /tmp/r
 > git --version # timeout=10
 > git --version # 'git version 2.30.2'
 > git fetch --tags --force --progress -- /tmp/r +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision e5b4b2deed1c8db486cef5641ef14c61fd48f9ed (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f e5b4b2deed1c8db486cef5641ef14c61fd48f9ed # timeout=10
Commit message: "init"
First time build. Skipping changelog.
[Pipeline] echo
null
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline

问题:

我使用的是Jenkins版本2.349,项目类型为多分支管道。
当我读到Jenkins git插件的page时,它应该注入环境变量,但似乎只有当默认检出没有被跳过时才会这样做,而不是当我们显式检出时。我只能找到通过使用脚本{ }块访问这些SCM值的方法(在我的情况下是GIT_COMMIT)。有没有一种方法可以在不跳到脚本块的情况下获得这些变量?

busg9geu

busg9geu1#

checkout()调用返回一个带有commit属性的对象:

def result = checkout(...)
echo "Commit: ${result.GIT_COMMIT}"

字符串
请注意,这可能取决于安装的git插件的版本。

相关问题