部分矩阵/并行完成后继续下一阶段

问题描述 投票:0回答:1

我正在开发一个 Jenkins Pipeline 脚本,该脚本需要在 3 台服务器上并行运行,但需要在至少一台服务器完成其正在执行的工作时继续在 4 台不同的服务器上工作。我不确定 Jenkins 是否可以做到这一点,但如果可以的话,那将对我目前正在从事的工作有很大帮助。

我已经尝试使用谷歌搜索并查看 Jenkins 中矩阵和并行阶段的文档,但我要么在寻找错误的术语,要么信息或执行此操作的能力不存在。

jenkins jenkins-pipeline jenkins-plugins jenkins-groovy
1个回答
0
投票

您应该能够以不同的方式实现这一点,遵循一种使用 Lockable Resource Plugin 的方法。这个想法是从每个初始阶段开始,您将调用外部阶段执行,无论谁先到,都将获得一个锁,以防止其他人执行。

pipeline {
    agent any
    stages {
        stage('Run Tests') {
            parallel {
                stage('1') {
                    steps {
                        echo "Running something here"
                        runOtherStepsOrStages()
                        
                    }
                }
                stage('2') {
                    steps {
                        echo "run-tests.sh"
                        runOtherStepsOrStages()
                    }
                }
                 stage('3') {
                    steps {
                        echo "run-tests.sh"
                        runOtherStepsOrStages()
                    }
                }
            }
        }
    }
}

def runOtherStepsOrStages(){
    // Only one Stage will be able to aquare the lock, once aquiredother will skip, you can add another flag to make sure it's not executed again after the stage is completed
    lock(resource: 'myLock', skipIfLocked: true) {
        echo "Storing the steps/stages externally to reuse."
        sleep 30
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.