如何使用声明性 Jenkins 管道在同一节点上运行多个阶段? [英] How to run multiple stages on the same node with declarative Jenkins pipeline?

查看:21
本文介绍了如何使用声明性 Jenkins 管道在同一节点上运行多个阶段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标
在同一节点上运行声明式 Jenkins 管道的多个阶段.

Goal
Run multiple stages of a declarative Jenkins pipeline on the same node.

设置
这只是显示问题的一个最小示例.有 2 个 Windows 节点windows-slave1"和windows-slave2"都标有windows"标签.

Setup
This is just a minimal example to show the problem. There are 2 Windows nodes "windows-slave1" and "windows-slave2" both labeled with the label "windows".

注意:我真正的 Jenkinsfile 不能使用全局代理,因为有些阶段需要在不同的节点上运行(例如 Windows 与 Linux).

NOTE: My real Jenkinsfile cannot use a global agent because there are groups of stages that require to run on different nodes (e.g. Windows vs. Linux).

预期行为
Jenkins 根据标签选择Stage 1"中的节点之一,并在Stage 2"中使用相同的节点,因为变量 windowsNode 已更新为Stage 1"中选择的节点.

Expected Behaviour
Jenkins selects one of the nodes in "Stage 1" based on the label and uses the same node in "Stage 2" because the variable windowsNode was updated to the node selected in "Stage 1".

实际行为
Stage 2"有时在与Stage 1"相同的节点上运行,有时在不同的节点上运行.请参阅下面的输出.

Actual Behaviour
"Stage 2" sometimes runs on the same and sometimes on a different node than "Stage 1". See the output below.

Jenkinsfile

#!groovy

windowsNode = 'windows'

pipeline {
  agent none
  stages {
    stage('Stage 1') {
      agent {
        label windowsNode
      }
      steps {
        script {
          // all subsequent steps should be run on the same windows node
          windowsNode = NODE_NAME
        }
        echo "windowsNode: $windowsNode, NODE_NAME: $NODE_NAME"
      }
    }
    stage('Stage 2') {
      agent {
        label windowsNode
      }
      steps {
        echo "windowsNode: $windowsNode, NODE_NAME: $NODE_NAME"
      }
    }
  }
}

输出

[Pipeline] stage
[Pipeline] { (Stage 1)
[Pipeline] node
Running on windows-slave2 in C:Jenkinsworkspace	est-agent-allocation@2
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] echo
windowsNode: windows-slave2, NODE_NAME: windows-slave2
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Stage 2)
[Pipeline] node
Running on windows-slave1 in C:Jenkinsworkspace	est-agent-allocation
[Pipeline] {
[Pipeline] echo
windowsNode: windows-slave2, NODE_NAME: windows-slave1
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
Finished: SUCCESS

您知道设置有什么问题吗?我猜这就是 Jenkinsfile 的解析和执行方式.

Any ideas what's wrong with the setup? I guess it's how the Jenkinsfile is parsed and executed.

其他建议?最初设置 windowsNode 时,可能有一个 Jenkins API 可以根据windows"标签选择一个节点.

Other suggestions? Maybe there is a Jenkins API to select a node based on the "windows" label when setting windowsNode initially.

推荐答案

Declarative Pipeline 插件从 1.3 版本开始正式支持.它被正式称为顺序阶段".

Since version 1.3 of Declarative Pipeline plugin, this is officially supported. It's officially called "Sequential Stages".

pipeline {
    agent none

    stages {
        stage("check code style") {
            agent {
                docker "code-style-check-image"
            }
            steps {
                sh "./check-code-style.sh"
            }
        }

        stage("build and test the project") {
            agent {
                docker "build-tools-image"
            }
            stages {
               stage("build") {
                   steps {
                       sh "./build.sh"
                   }
               }
               stage("test") {
                   steps {
                       sh "./test.sh"
                   }
               }
            }
        }
    }
}

官方公告在这里:https://jenkins.io/blog/2018/07/02/whats-new-declarative-piepline-13x-sequential-stages/

这篇关于如何使用声明性 Jenkins 管道在同一节点上运行多个阶段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆