的hash
变量是全球性的,它的值是在其中似乎是合乎逻辑我不同执行人可以定义一个全局变量来阅读。所以它看起来像我做的事情是OK的,它会以这种方式工作,除非我错过了什么。
这里是我如何验证(详细信息跳过简洁):
我创建了一个类似的管道和杀害跑的第一个节点执行:
def gitHash;
node {
withCredentials(...) {
//Step 1:
//Check out from the SCM
stage('Prepare') {
echo "Checking out the project from source control.."
scmInfo = checkout scm
gitHash = scmInfo.GIT_COMMIT
echo "Project checked out, the GIT hash of the last commit is: ${gitHash}"
}
}
}
stage('Ask deploy') {
input 'Deploy?'
}
node {
withCredentials(...) {
stage('Deploy') {
echo "TODO, hash ${gitHash}"
}
}
}
的输出詹金斯是以下的(详情跳过):
Obtained Jenkinsfile from 7adc4bb98524b31de93e0c1ae16bf967ca3df47c
Running on jnlp-13775fa128a47 in /root/workspace/...
[Pipeline] {
[Pipeline] withCredentials
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Prepare)
[Pipeline] echo
Project checked out, the GIT hash of the last commit is: 7adc4bb98524b31de93e0c1ae16bf967ca3df47c
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] stage
[Pipeline] { (Ask deploy)
[Pipeline] input
Deploy?
Proceed or Abort
Approved by admin
[Pipeline] }
[Pipeline] // stage
[Pipeline] node
Running on jnlp-1383bdf520c9d in /root/workspace/...
[Pipeline] {
[Pipeline] withCredentials
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] echo
TODO, hash 7adc4bb98524b31de93e0c1ae16bf967ca3df47c
[Pipeline] End of Pipeline
Finished: SUCCESS
正如所看到的第一个节点上执行程序jnlp-13775fa128a47
运行第二个是上jnlp-1383bdf520c9d
,但可以在那里读取全局作用域变量的值。
实际上这是同一个构建管道,但节点可能偶尔会在不同的执行程序中执行。你的意思是?那么变量会丢失呢? –
是的。例如,如果您需要从“部署”或其他阶段执行不同执行程序中的“构建”阶段,则存在散列变量不是全局变量并且不可用的问题。在这种情况下,您需要定义一个全局变量并通过$ VARIABLE或$ {VARIABLE}来获取值。 –
嗯......我认为全球范围内的变量对于不同的执行者应该是透明的。请你检查一下我的回答/更新,并分享一下如果这个假设有什么错误? –