Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
796 views
in Technique[技术] by (71.8m points)

jenkins - How to conditionally build other projects?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...