我有一个应用需要在 Windows 和 Linux 上构建、测试和部署。
我有 40 个从站,其中 20 个是 Linux,20 个是 Windows,分别由“Lin”和“Win”标签定义。
我首先分配两个节点(Linux 和 Windows)。问题是我需要通过多个阶段使用它们。但是,我还没有找到一个好的方法来做到这一点。
这段代码应该有助于说明我需要做什么:
pipeline {
agent none
stages {
stage('Build') {
parallel (
"Linux Build": {
node('Lin') { // Say this allocates 'Jenkins-node-lin1'
...
}
},
"Windows Build": {
node('Win') { // And this allocates 'Jenkins-node-win1'
...
}
}
)
}
stage('Test') {
steps {
parallel (
"Linux Test": {
node('Lin') { // I need to reuse Jenkins-node-lin1 here
...
}
},
"Windows Test": {
node('Win') { // And Jenkins-node-win1 here as well
...
}
}
)
}
}
stage('Deploy') {
steps {
parallel (
"Linux Deploy": {
node('Lin') { // Same story down here
...
}
},
"Windows Deploy": {
node('Win') { // And this one too
...
}
)
}
}
} // End stages
}
我已经尝试了很多“解决方案”,但到目前为止还没有一个对我有用,说实话,我不确定这个功能是否已合并到 Jenkins 中。
最佳答案
我还没有尝试过这个,但我认为你可以做类似的事情(顺便说一句,你错过了你的步骤{} block ):
def linux_node
def windows_node
pipeline {
agent none
stages {
stage('Build') {
steps {
parallel (
"Linux Build": {
node('Lin') { // Say this allocates 'Jenkins-node-lin1'
linux_node = env.NODE_NAME
}
},
"Windows Build": {
node('Win') { // And this allocates 'Jenkins-node-win1'
windows_node = env.NODE_NAME
}
}
)
}
}
stage('Test') {
steps {
parallel (
"Linux Test": {
node(linux_node) { // I need to reuse Jenkins-node-lin1 here
...
}
},
"Windows Test": {
node(windows_node) { // And Jenkins-node-win1 here as well
...
}
}
)
}
}
stage('Deploy') {
steps {
parallel (
"Linux Deploy": {
node(linux_node) { // Same story down here
...
}
},
"Windows Deploy": {
node(windows_node) { // And this one too
...
}
)
}
}
} // End stages
}
我不确定您需要与某个节点有亲和性的原因,但如果是这样,某些文件在工作区中可用,那么这样做确实有点冒险。不保证将使用相同的工作空间。通常是这样,但有时工作空间可能会发生变化。
另一个选项是存储您需要在各个阶段之间保留的文件,并在下一阶段取消存储它们。那么他们最终选择哪个经纪人并不重要。 (除非您有其他节点亲和性的原因)。
关于jenkins - 在 Jenkins 管道的阶段之间重用多个节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46354821/