How do I prevent two pipeline jenkins jobs of the

2019-01-17 05:46发布

I want not to allow two jobs of the same type (same repository) not to run in parallel on the same node.

How can I do this using groovy inside Jenkinsfile?

10条回答
Explosion°爆炸
2楼-- · 2019-01-17 06:24

The answer provided in https://stackoverflow.com/a/43963315/6839445 is deprecated.

The current method to disable concurrent builds is to set options:

options { disableConcurrentBuilds() }

Detailed description is available here: https://jenkins.io/doc/book/pipeline/syntax/#options

查看更多
祖国的老花朵
3楼-- · 2019-01-17 06:25

You got at the disableConcurrentBuilds property:

properties properties: [
  ...
  disableConcurrentBuilds(),
  ...
]

Then the job would wait the older one to finish first

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-17 06:31

Example using options block in the declarative pipeline syntax:

pipeline {

  options { 
    disableConcurrentBuilds() 
  }

...
}
查看更多
ら.Afraid
5楼-- · 2019-01-17 06:38

Install Jenkins Lockable Resources Plugin.

In your pipeline script wrap the part in the lock block and give this lockable resource a name.

lock("test-server"){
    // your steps here
}

Use the name of whatever resource you are locking. In my experience its usually a test server or test database.

查看更多
劫难
6楼-- · 2019-01-17 06:40

I think there are more than just one approach to this problem.

Pipeline

  • Use latest version of Lockable Resources Plugin and its lock step, as suggested in other answer.
  • If building the same project:
    • Uncheck Execute concurrent builds if necessary.
  • If building different projects:
    • Set different node or label for each project.

Jenkins

  • Limit number of node's executors to 1?

Plug-ins

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-01-17 06:40

The "Throttle Concurrent Builds Plugin" now supports pipeline since throttle-concurrents-2.0. So now you can do something like this:

// Fire me twice, one immediately after the other
// by double-clicking 'Build Now' or from a parallel step in another job.
stage('pre'){
    echo "I can run in parallel"
    sleep(time: 10, unit:'SECONDS')
}
throttle(['my-throttle-category']) {

    // Because only the node block is really throttled.
    echo "I can also run in parallel" 

    node('some-node-label') {

        echo "I can only run alone"

        stage('work') {

            echo "I also can only run alone"
            sleep(time: 10, unit:'SECONDS')

        }
    }
}
stage('post') {
    echo "I can run in parallel again"
    // Let's wait enough for the next execution to catch
    // up, just to illustrate.
    sleep(time: 20, unit:'SECONDS')
}

From the pipeline stage view you'll be able to appreciate this:

enter image description here

However, please be advised that this only works for node blocks within the throttle block. I do have other pipelines where I first allocate a node, then do some work which doesn't need throttling and then some which does.

node('some-node-label') {

    //do some concurrent work

    //This WILL NOT work.
    throttle(['my-throttle-category']) {
        //do some non-concurrent work
    }
}

In this case the throttle step doesn't solve the problem because the throttle step is the one inside the node step and not the other way around. In this case the lock step is better suited for the task

查看更多
登录 后发表回答