基于Jenkinsfile上的列表的阶段

时间:2018-10-29 14:49:30

标签: jenkins-pipeline

我有这样的东西:

def projects = [
  "foo",
  "bar",
  "foobar",
  "etc",
]

pipeline {
  agent any
  stages {
    stage('lint') {
      parallel {
        stage('lint: foo') {
          steps {
            sh "lint foo"
          }
        }
        stage('lint: bar') {
          steps {
            sh "lint bar"
          }
        }
        stage('lint: foobar') {
          steps {
            sh "lint foobar"
          }
        }
      }
    }
  }
}

我真的不想在那儿重复一遍,有没有办法为每个项目“生成” stage代码?

我可以执行类似的操作,而无需使用parallel部分:

pipeline {
  agent any
  stages {
    stage('initialize') {
      steps {
        script {
          for (int i = 0; i < projects.size(); i++) {
            stage("lint: ${projects[i]}") {
              sh "lint ${project}"
            }
          }
        }
      }
    }
  }
}

任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

对于脚本化管道,您可以使用类似以下内容的

def projects = [
  "foo",
  "bar",
  "foobar",
  "etc",
]

Map branches = [:]
projects.each { String project ->
    branches[project] = {
        stage(project) {
            node {
                // I assume we need to checkout something, e.g. using git or checkout step...
                // git ...
                sh "lint ${project}"
            }
        }
    }
}

parallel branches