Giel Berkers
Giel Berkers

Reputation: 2960

Jenkins : How can I make a build fail if another job is unsuccessful?

I have two separate Jobs in Jenkins, but one job is only allowed to run if another jobs' last state was successful.

Please note: I do not want to automatically trigger the job; both jobs are triggered manually.

How can I achieve this?

note: I am using Jenkins declarative pipeline syntax.

Upvotes: 4

Views: 4770

Answers (2)

Rishi
Rishi

Reputation: 1060

There are two ways, I can think of:

    A. Using Parameterized Trigger Plugin : You can use it as a build step, and mark the checkbox for "Block until the triggered projects finish their builds". That should be exactly what you are looking for.

    B. You can try the Multijob Project option to accomplish this. Multijob Project helps you to control the way you want to run a give set of jobs- either within a phase in parallel or in different phases such that one executes after another-dependent or independent of previous phase's outcome (in your case, it's dependent).
    1. Go to your Jenkins home page.
    2. Look for 'New Item'.
    3. Select 'Multijob Project' from the options. Give some name to this Multijob, say, JobSuite.

enter image description here

    4. Configure 'JobSuite' as per your requirements for the below options. These options available are quite self-explanatory and you may add parameter(s) with which you want to trigger this job. For your reference, I added a parameter. If your jobs run in some slave, put a check and pick up the node for which you would like to restrict the run, else leave it unchecked

enter image description here

    5. Scroll down and you will see a button called 'Add build step'. Select 'Multijob phase'.

enter image description here

    6. Give the multijob phase some name and by default, you will be able to configure one job. Enter the first job's name, say job1, you may Add Parameter and pass parameters. Just as an example, I am trying to pass a parameter through 'Predefined Parameters' that will be taken as an input while JobSuite is triggered.

    Fill up 'Kill the phase on'= 'Failure(stop the phase execution if the job is failed)' and 'Continuation condition to next phase when jobs statuses are' = 'Successful'. This will make sure, that next phase runs ONLY if the current phase passes.

    NOTE: If you want to run more jobs in parallel under this phase, click on 'Add Job' but in your case, we want a sequential run for job1 and job2. Hence, we need two phases.

enter image description here enter image description here

    7. Repeat step 5 to add another phase that will carry the second job: job2. Apply and Save.

enter image description here

    8. You may add some Post-build Actions, like email-notification, etc as required.
    9. Run the Mutijob Project- JobSuite with the required parameters. You will see that job2 runs only if job1 was successful.

I hope this helps you.

Upvotes: 4

saurabh14292
saurabh14292

Reputation: 1401

Use Pre-requisite check build step and configure Upstream job to check the status

enter image description here

Refer the plugin here : https://wiki.jenkins.io/display/JENKINS/Prerequisite+build+step+plugin

If adding a plugin is an obstacle (eg restricted admin access), I suggest you log the details of status of upstream job into a file or in DB and fetch the same as a first step of downstream job.

Upvotes: 1

Related Questions