Jenkins在不同的代理上并行构建
我有一个Jenkins声明性管道的小例子,该管道应同时在“ Windows”和“
Linux”代理上运行。目标是动态构建配置矩阵(例如,发布/调试配置;不同的CMake参数;等等),并使所有组合并行运行。但是,我被困在建立从预备变量执行并行步骤的管道中。
这是脚本的一个版本,其中并行阶段是在parallel{}块内显式指定的:
pipeline {    agent any
    stages {
        stage ("Parallel Build") {
            parallel {
                stage ("Windows") {
                    steps {
                        echo "TEST Windows"
                    }
                }
                stage ("Linux") {
                    steps {
                        echo "TEST Linux"
                    }
                }
            }
        }
    }
}
我的计划是在parallel{}块中动态创建阶段(取决于所需的配置),但是我不确定语法或是否完全可能。
像这样:
def stage_list = {    stage ("Windows") {          <=== How to correctly create the stage_list?
        steps {
            echo "TEST Windows"
        }
    }
    stage ("Linux") {
        steps {
            echo "TEST Linux"
        }
    }
}
pipeline { 
    agent any
    stages {    
        stage ("Parallel Build") {
                parallel stage_list <== How to replace a block with variable?
        }
    }
}
上面的代码会在詹金斯中返回错误:
WorkflowScript: 17: Expected a block for parallel @ line 17, column 9.           stage ("Parallel Build") {
           ^
WorkflowScript: 17: Expected one of "steps", "stages", or "parallel" for stage "Parallel Build" @ line 17, column 9.
           stage ("Parallel Build") {
           ^
有谁知道如何做到这一点?
:在前两个答案之后,我想更新一下我的问题。
我测试了创建stage_list变量的建议方法。但是,如果我将此调用parallel
stage_list放到原始结构中,则会得到与以前相同的错误。script像这样使用时运行良好
pipeline {     agent any
    stages {    
        stage ("Parallel Build") {
            steps {
                script {
                    parallel stepsForParallel
                }
            }            
        }
    }
}
有人可以向我解释差异吗?为什么它一起工作steps和 script,但没有他们?
出于文档方面的原因,我想用解决问题的方法来结束我的问题:
SmartToms回答和詹金斯关于Docker的管道语法的官方文档明确指出,声明性管道和脚本化管道是两种需要不同处理的单独方法(请注意每个示例下方的“切换脚本化管道”链接)。
解决我的问题的一种方法是使用脚本化的管道-
如果有人对此示例感兴趣,这里是一个管道的链接,其中包含了显示原理的管道脚本。
回答:
从本文档中可以这样进行:
// Creation of the stage_listdef stage_list = ["Windows", "Linux"]
// Creation of a map of stages
def stepsForParallel = stage_list.collectEntries {
    ["echoing ${it}" : transformIntoStage(it)]
}
// Run the stages in parallel
parallel stepsForParallel
// Creation of the stage
def transformIntoStage(inputString) {
    return {
        stage (inputString) {
            steps {
                echo "TEST "+inputString
            }
        }
    }
}
您可以在此处找到有关并行Jenkins声明式管道的更多信息。
:
为什么它一起工作
steps和script,但没有他们?
根据本文档,我认为parallel与列表一起使用是
旧 方法(在Declarative Pipeline 1.2之前),该方法要求在Declarative Pipeline中使用Scripted
Pipeline。
似乎新方法parallel(来自Declarative Pipeline 1.2)不能与列表一起使用。因此,要执行此操作,必须使用 旧的
Scripted Pipeline方法,因此,您需要封装自己的命令pipeline
stage_list,script而该命令本身必须由封装steps。
您可以在此处找到有关脚本化管道和声明性管道的更多信息。
以上是 Jenkins在不同的代理上并行构建 的全部内容, 来源链接: utcz.com/qa/405496.html







