0

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?

Deepthi
  • 495
  • 4
  • 12

2 Answers2

1

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") 
                    ...
           ]
ycr
  • 12,828
  • 2
  • 25
  • 45
  • Thanks for your response! This happened even when i had not enabled concurrent builds on project C. Finally what worked is using locks. – Deepthi Mar 16 '23 at 08:25
0

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.

Deepthi
  • 495
  • 4
  • 12