jenkins管道git checkout访问git_commit当跳过默认结帐时

发布于 2025-02-02 18:35:31 字数 2245 浏览 6 评论 0原文

观察:

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插件的

Observations:

Jenkinsfile

pipeline {
    agent { node 'master' }

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

Output:

[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
            }
        }
    }
}

Output:

[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

Question:

I am using jenkins version 2.349 and the project is of type Multi-branch pipeline.

As I read on the Jenkins git plugin's page, it is supposed to inject the environment variables but it seems it only does when the default checkout isn't skipped but not when we explicitly checkout. I could only find ways to access those SCM values (in my case GIT_COMMIT) by using a script { } block. Is there a way to get those variables without jumping into a script block?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冧九 2025-02-09 18:35:31

Checkout()调用返回带有提交属性的对象:

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

请注意,这可能取决于已安装的Git插件的版本。

The checkout() call returns an object with a commit property:

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

Note that this may depend on the version of the git plugin installed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文