简体   繁体   English

Jenkins声明式管道并行构建

[英]Jenkins declarative pipeline parallel builds

I need to have two builds running in parallel with jenkins declarative pipeline. 我需要有两个与jenkins声明式管道并行运行的构建。 To avoid collision between builds workspaces in scripted pipeline following construction can be used : 为了避免脚本化管道中的构建工作空间之间发生冲突,可以使用以下构造:

lock('some_lock') {
      checkout git 'gitrepo'
      build 
}

In declarative pipeline, checkout step is not called directly, so even if we will make lock like this, 在声明性管道中,不会直接调用checkout步骤,因此即使我们像这样进行锁定,

steps {
    lock('some_lock') {
           build here ongoing
    }
}

we can have situation when build A is building, build B is waiting for the lock, but it will still perform checkout, because in declarative pipeline you don't specify when checkout will happen. 我们可能会遇到以下情况:构建A正在构建,构建B正在等待锁定,但它仍将执行检出,因为在声明性管道中,您无需指定检出何时发生。 Can it be avoided ? 可以避免吗?

I know in theory Jenkins should not use the same workspace for this kind of situations. 我知道理论上詹金斯在这种情况下不应该使用相同的工作空间。 But it happen time to time unfortunately. 但不幸的是,它有时会发生。

I think the part in declarative pipelines that is getting in your way is the "Declarative: Checkout SCM" stage that happens automatically by default. 我认为声明式管道中遇到的问题是默认情况下自动发生的“声明式:签出SCM”阶段。 If so, you can get around your issue by turning this feature off and checking out your source code manually, like this: 如果是这样,您可以通过关闭此功能并手动签出源代码来解决问题,如下所示:

pipeline {
  agent { label 'docker' }
  options {
    skipDefaultCheckout true // this is how you avoid the 'Declarative: Checkout SCM' stage
  }
  stages {
    stage('commit_stage') {
      steps {
        lock('some_lock') {
          checkout scm // this is how you replicate what 'Declarative: Checkout SCM' does as a step
          echo 'build stuff here'
        }
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM