简体   繁体   中英

Jenkins declarative pipeline parallel builds

I need to have two builds running in parallel with jenkins declarative pipeline. 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,

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. 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. 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'
        }
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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