根据作业输入参数动态生成并行阶段

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

我正在尝试根据列表输入参数设置一些动态并行的 Jenkins 管道,但我在文档中找不到任何内容,我想知道这里的人们是否已经找到了执行此操作的方法。

举个例子,我想做的可能是这样的:

@Library([...]) _
properties([]
  + string(name:'VARIANTS', defaultValue:'varA', description:'Comma-delimited list of variants to build.'),
  ...
)

pipeline {
  agent any
  environment {
    ...
  }
  stages {
    stage('Execute all variants in parallel') {
      /**
       * 1. Parse the `VARIANTS` input into a list of strings
       * 2. Execute, in parallel, a build for each variant from the list
       * */
    }
  }
}

据我所知,

parallel
不适用于此,所以我的本能是使用
matrix
,但我的研究表明矩阵不支持此类列表。对于这种行为有什么提示或技巧吗?

jenkins automation jenkins-pipeline
1个回答
0
投票

据我所知,并行对此不起作用......

如果您提供由字符串映射的闭包映射,该字符串将在并行执行闭包时作为阶段名称,则

parallel
步骤将起作用:

pipeline {
    agent any
    parameters {
        string(name:'VARIANTS', defaultValue:'varA, varB', description:'Comma-delimited list of variants to build.')
    }
    stages {
        stage('Execute all variants in parallel') {
            steps {
                // We need to escape the declarative pipeline syntax to execute a scripted block
                script {
                    // Split the comma-delimited list of variants into a list of strings
                    List<String> selectedVariants = params.VARIANTS.split(',').collect { it.trim() }
                    // Create a map of stages to execute in parallel
                    Map<String, Closure> stages = selectedVariants.collectEntries { variant ->
                        ["Building: $variant": {
                            // Execute the stage for the selected variant
                            echo "Making ${variant}..."
                            // Add your build steps here
                        }]
                    }
                    // Execute the stages in parallel
                    parallel stages
                }
            }
        }
    }
}

然后输入:

varA, varB, varC

enter image description here

您将会得到想要的结果

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.