Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to conditionally build other projects?

Tags:

jenkins

I have a fairly complicated Jenkins job that builds, unit tests and packages a web application. Depending on the situation, I would like to do different things once this job completes. I have not found a re-usable/maintainable way to do this. Is that really the case or am I missing something?

The options I would like to have once my complicated job completes:

  1. Do nothing

  2. Start my low-risk-change build pipeline:

    • copies my WAR file to my artifact repository
    • deploys to production
  3. Start my high-risk-change build pipeline:

    • copies my WAR file to my artifact repository
    • deploys to test
    • run acceptance tests
    • deploy to production

I have not found an easy way to do this. The simplest, but not very maintainable approach would be to make three separate jobs, each of which kicks off a downstream build. This approach scares me for a few reasons including the fact that changes would have to be made in three places instead of one. In addition, many of the downstream jobs are also nearly identical. The only difference is which downstream jobs they call. The proliferation of jobs seems like it would lead to an un-maintainable mess.

I have looked at using several approaches to keep this as one job, but none have worked so far:

  1. Make the job a multi-configuration project (https://wiki.jenkins-ci.org/display/JENKINS/Building+a+matrix+project). This provides a way to inject the job with a parameter. I have not found a way to make the "build other projects" step respond to a parameter.

  2. Use the Parameterized-Trigger plugin (https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+Trigger+Plugin). This plugin lets you trigger downstream-jobs based on certain triggers. The triggers appear to be too restrictive though. They're all based on the state of the build, not arbitrary variables. I don't see any option provided here that would work for my use case.

  3. Use the Flexible Publish plugin (https://wiki.jenkins-ci.org/display/JENKINS/Flexible+Publish+Plugin). This plugin has the opposite problem as the parameterized-trigger plugin. It has many useful conditions it can check, but it doesn't look like it can start building another project. Its actions are limited to publishing type activities.

  4. Use Flexible Publish + Any Build Step plugin (https://wiki.jenkins-ci.org/display/JENKINS/Any+Build+Step+Plugin). The Any Build Step plugin allows making any build action available to the Flexible Publish plugin. While more actions were made available once this plugin was activated, those actions didn't include "build other projects."

Is there really not an easy way to do this? I'm surprised that I haven't found it and even more surprised that I haven't really seen any one else trying to do this? Am I doing something unusual? Is there something obvious that I am missing?

like image 758
xnickmx Avatar asked Oct 26 '12 17:10

xnickmx


People also ask

What is conditional step in Jenkins?

The Conditional BuildStep plugin lets users add conditional logic to Freestyle jobs from within the Jenkins web UI. It does this by: Adding two types of Conditional BuildStep ("Single" and "Multiple") - these build steps contain one or more other build steps to be run when the configured condition is met.


1 Answers

If I understood it correct you should be able to do this by following these Steps:

  1. First Build Step:
    1. Does the regular work. In your case: building, unit testing and packaging of the web application
    2. Depending on the result let it create a file with a specific name.
    3. This means if you want the low-risk-change to run afterwards create a file low-risk.prop
  2. Second Build Step:
    1. Create a Trigger/call builds on other projects Step from the Parameterized-Trigger plugin.
    2. Entery the name of your low-risk job into the Projects to build field
    3. Click on: Add Parameter
    4. Choose: Parameters from properties File
    5. Enter low-risk.prop into the Use properties from file Field
    6. Enable Don't trigger if any files are missing
  3. Third Build Step:
    1. Check if a low-risk.prop file exists
    2. Delete the File

Do the same for the high-risk job

Now you should have the following Setup:

  • if a file called low-risk.prop occurs during the first Build Step the low-risk job will be started
  • if a file called high-risk.prop occurs during the first Build Step the high-risk job will be started
  • if there's no .prop File nothing happens

And that's what you wanted to achieve. Isn't it?

like image 62
jadephantom Avatar answered Oct 17 '22 03:10

jadephantom