jenkins管道git checkout访问git_commit当跳过默认结帐时
观察:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Checkout()调用返回带有提交属性的对象:
请注意,这可能取决于已安装的Git插件的版本。
The checkout() call returns an object with a commit property:
Note that this may depend on the version of the git plugin installed.