脚本化的jenkinsfile并行阶段 [英] Scripted jenkinsfile parallel stage

查看:196
本文介绍了脚本化的jenkinsfile并行阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用常规DSL编写脚本化的Jenkinsfile,它将在一组阶段中具有并行步骤.

I am attempting to write a scripted Jenkinsfile using the groovy DSL which will have parallel steps within a set of stages.

这是我的詹金斯档案:

node {   
stage('Build') {
    sh 'echo "Build stage"'
}

stage('API Integration Tests') {
    parallel Database1APIIntegrationTest: {
        try {
            sh 'echo "Build Database1APIIntegrationTest parallel stage"'
        }
        finally {
            sh 'echo "Finished this stage"'
        }               

    }, Database2APIIntegrationTest: {
        try {
            sh 'echo "Build Database2APIIntegrationTest parallel stage"'
        }
        finally {
            sh 'echo "Finished this stage"'
        }

    }, Database3APIIntegrationTest: {
        try {
            sh 'echo "Build Database3APIIntegrationTest parallel stage"'
        }
        finally {
            sh 'echo "Finished this stage"'
        }
    }
}

stage('System Tests') {
    parallel Database1APIIntegrationTest: {
        try {
            sh 'echo "Build Database1APIIntegrationTest parallel stage"'
        }
        finally {
            sh 'echo "Finished this stage"'
        }               

    }, Database2APIIntegrationTest: {
        try {
            sh 'echo "Build Database2APIIntegrationTest parallel stage"'
        }
        finally {
            sh 'echo "Finished this stage"'
        }

    }, Database3APIIntegrationTest: {
        try {
            sh 'echo "Build Database3APIIntegrationTest parallel stage"'
        }
        finally {
            sh 'echo "Finished this stage"'
        }
    }
}
}

我想分三个阶段:构建;集成测试和系统测试. 在这两个测试阶段中,我希望并行执行3组测试,每组针对不同的数据库.

I want to have 3 stages: Build; Integration Tests and System Tests. Within the two test stages, I want to have 3 sets of the tests executed in parallel, each one against a different database.

我有3位可用的执行者.一个主服务器,两个代理,我希望每个并行步骤都可以在任何可用的执行器上运行.

I have 3 available executors. One on the master, and 2 agents and I want each parallel step to run on any available executor.

我注意到的是,在运行管道之后,我只看到3个阶段,每个阶段都标记为绿色.我不想查看该阶段的日志来确定该阶段中的任何并行步骤是否成功/不稳定/失败.

What I've noticed is that after running my pipeline, I only see the 3 stages, each marked out as green. I don't want to have to view the logs for that stage to determine whether any of the parallel steps within that stage were successful/unstable/failed.

我希望在测试阶段看到三个步骤-标记为绿色,黄色或红色(成功,不稳定或失败).

I want to be seeing the 3 steps within my test stages - marked as either green, yellow or red (Success, unstable or failed).

我已经考虑将测试扩展到自己的阶段,但是已经意识到不支持并行阶段(有人知道是否会支持吗?),所以我不能这样做,因为管道也将花费很多时间渴望完成.

I've considered expanding the tests out into their own stages, but have realised that parallel stages are not supported (Does anyone know whether this will ever be supported?), so I cannot do this as the pipeline would take far too long to complete.

任何见识将不胜感激,谢谢

Any insight would be much appreciated, thanks

推荐答案

在Jenkins脚本化管道中,parallel(...)获取一个Map,该Map描述了要构建的每个阶段.因此,您可以以编程方式预先构建构建阶段,该模式可实现灵活的串行/并行切换.
我使用了与此类似的代码,其中prepareBuildStages返回一个Map列表,每个List元素是按顺序执行的,而Map则描述了此时的并行阶段.

In Jenkins scripted pipeline, parallel(...) takes a Map describing each stage to be built. Therefore you can programatically construct your build stages up-front, a pattern which allows flexible serial/parallel switching.
I've used code similar to this where the prepareBuildStages returns a List of Maps, each List element is executed in sequence whilst the Map describes the parallel stages at that point.

// main script block
// could use eg. params.parallel build parameter to choose parallel/serial 
def runParallel = true
def buildStages

node('master') {
  stage('Initialise') {
    // Set up List<Map<String,Closure>> describing the builds
    buildStages = prepareBuildStages()
    println("Initialised pipeline.")
  }

  for (builds in buildStages) {
    if (runParallel) {
      parallel(builds)
    } else {
      // run serially (nb. Map is unordered! )
      for (build in builds.values()) {
        build.call()
      }
    }
  }

  stage('Finish') {
      println('Build complete.')
  }
}

// Create List of build stages to suit
def prepareBuildStages() {
  def buildList = []

  for (i=1; i<5; i++) {
    def buildStages = [:]
    for (name in [ 'one', 'two', 'three' ] ) {
      def n = "${name} ${i}"
      buildStages.put(n, prepareOneBuildStage(n))
    }
    buildList.add(buildStages)
  }
  return buildList
}

def prepareOneBuildStage(String name) {
  return {
    stage("Build stage:${name}") {
      println("Building ${name}")
      sh(script:'sleep 5', returnStatus:true)
    }
  }
}

生成的管道显示为:

The resulting pipeline appears as:

对于可嵌套在并行块中的内容有某些限制,请参见管道文档以获取详细信息.不幸的是,尽管它的灵活性不如脚本化(IMHO),但很多参考似乎都偏向于声明式管道. 管道示例页是最有用的.

There are certain restrictions on what can be nested within a parallel block, refer to the pipeline documentation for exact details. Unfortunately much of the reference seems biased towards declarative pipeline, despite it being rather less flexible than scripted (IMHO). The pipeline examples page was the most helpful.

这篇关于脚本化的jenkinsfile并行阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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