1

I have a Jenkins job1 which triggers job2

stage("trigger job2") {
       steps {
           build job: job2,
                parameters: [
                    string(name: "test1", value: "test1"),
                    string(name: "test2", value: "test2"),
                ],

and job2 triggered and running, I need to know if job2's last stage fails; for example, in my case, the last stage is "RESULT". Now, if the RESULT stage on job2 is red/failed, return this one pass to job1 and in job1 stage trigger job2 should also display in red.

I tried these cases but they do not work.

Jenkins version is Jenkins 2.346.1

What I tried:

JOB1: modified

 def p = build job: job2, propagate: true,
                parameters: [
                    string(name: "test1", value: "test1"),
                    string(name: "test2", value: "test2"),
                ],
            script {
                    build.waitForCompletion()
            }
            if (p.result == 'FAILURE') {
                currentBuild.result = "FAILURE"
                error 'Job2 failed, marking Job1 as failed in the current stage'
                }
            }

Try 2:

JOB1 modified

def result = build.waitForCompletion()
            if (result.result == 'FAILURE') {
                currentBuild.result = "FAILURE"
                error 'Job2 failed, marking Job1 as failed in the current stage'
            }

I tried 3-4 different cases, but they didn't work, could someone advise me?

tripleee
  • 175,061
  • 34
  • 275
  • 318
0xGJx0
  • 11
  • 3
  • 2
    Does this answer your question? [Jenkins downstream error not propagated to upstream](https://stackoverflow.com/questions/74657976/jenkins-downstream-error-not-propagated-to-upstream) (basic result propagation to an upstream job). If more complex data passing is needed, there's another question & answers also related to both result(s) and parameters: https://stackoverflow.com/questions/39539613/return-parameters-results-from-a-jobtriggered-by-pipeline-back-to-the-same-pip – David-kn Jan 22 '23 at 22:16

1 Answers1

0

I would say it is the default behaviour in Jenkins. Jenkins job fails be default if any of its stages fail. Job2 will fail if its last stage fails. As far as I know and what doc says it is enough to write this:

build job: job2,
            parameters: [
                string(name: "test1", value: "test1"),
                string(name: "test2", value: "test2"),
            ]

to make job1 fail when job2 fails. Stage which triggers job2 (in job1) should be red as you want.

helvete
  • 2,455
  • 13
  • 33
  • 37