Jenkins – prevent a pipeline/job from being triggered by multiple upstream projects -restrict one build to one upstream project

huangapple go评论49阅读模式
英文:

Jenkins - prevent a pipeline/job from being triggered by multiple upstream projects -restrict one build to one upstream project

问题

我在网上进行了很多搜索,但没有找到答案。

我们有两个项目AB,它们作为其管道步骤触发一个共同的项目C
现在,如果A触发了C(共同项目),而B也在大致相同的时间触发了C,那么C只会运行一个构建,其结果将被A和B同时使用。C中的构建状态显示"由上游项目A启动"和"由上游项目B启动"。

我如何使项目C首先完成由A触发的构建,然后开始由B触发的新构建?

英文:

I searched a lot online but did not find an answer to this.

We have two projects A and B, which trigger a common project C as it's pipeline step.
Now if A has triggered C (common project), and B also triggers C at around the same time, only one build of C is run and the result is consumed by both A and B. The build status in C shows "Started by upstream project A" and "Started by upstream project B" both.

How do I make project C first complete the build triggered by A and then start a new build for the trigger by B?

答案1

得分: 1

这是发生在启用了“不允许并发构建”时的情况,除非Jenkins将运行视为唯一的运行,否则不会将其他作业排队。Jenkins会认为您尝试多次运行相同的作业。为了欺骗Jenkins,您需要在每次触发作业时更改一个参数,如果使用相同的参数,Jenkins会认为它是相同的作业。因此,在目标作业C中引入一个新的参数,并可能传递一些随机值,使其变得唯一。

在目标作业C中:

string(name: 'UniqueString', defaultValue: 'o', description: 'something')

在触发作业A和B时,传递一些随机值,我正在传递:

def randomValue = "${JOB_NAME}-${BUILD_ID}"
build wait: false, job: 'otherjob', parameters: [ string(name: 'UniqueString', value: "$randomValue")
                    ...
           ]
英文:

This happens when you have enabled Do not allow concurrent builds, unless Jenkins sees the run as a unique run it will not queue additional Jobs, Jenkins will think you are trying to run the same Job multiple times. In order to trick Jenkins you need to change a parameter each time you trigger the Job, If you use the same parameters it will think it's the same Job. So introduce a new Parameter in the target JOb (Job C) and maybe pass some random value to that which will make it unique.

In the target Job C

string(name: 'UniqueString', defaultValue: 'o', description: 'something')

In the Triggering Job A and B. Pass some random value, I'm passing

def randomValue = "${JOB_NAME}-${BUILD_ID}"
build wait: false, job: 'otherjob', parameters: [ string(name: 'UniqueString',value: "$randomValue") 
                    ...
           ]

答案2

得分: 0

我通过在Jenkins中使用可锁资源插件来解决了我的问题。
因此,项目A和B的Jenkinsfile如下所示:

pipeline {
agent { ... }

stages {
    stage('Invoke project C') {
        steps {
            lock('project-c-lock') {
                script {
                    ...
                    ...
                }
            }      
        }
    }
}

因此,这个步骤只执行一次,并进行同步。具有相同的锁定资源名称非常重要。

英文:

I solved my issue by using the lockable resource plugin in Jenkins.
So my Jenkinsfile for projects A and B look like this

pipeline {
agent { ... }

stages {
    stage('Invoke project C') {
        steps {
            lock('project-c-lock') {
                script {
                    ...
					...
                }
            }      
        }
    }

}

Thus this step is executed only once and synchronization takes place. It's important to have the same lock resource name.

huangapple
  • 本文由 发表于 2023年3月15日 19:35:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定