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
575 views
in Technique[技术] by (71.8m points)

groovy - How to make a Jenkins/Hudson job surveil some others jobs and decide whether to build or not?

PROBLEM

Given that all the jobs have string LEVEL_X in it's job name, where X is a number > 1. I want every job with X = n to surveil every job with X = n-1, and start building if each of them is finished with success. And I want the job with X = n to surveil the other jobs with an interval of 1 minute.

First of all, I am interested in knowing what the best way is to do that, second I want the solution if one can be implemented by a small script, maybe a groovy script which can be run in system groovy script using the GROOVY PLUGIN.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Here are some hints and code snippets:

  • There is a Groovy Script console at http://<jenkins-server>/script that will help you with debugging your scripts.
  • Here is a link to Jenkins Java API.
  • Code snippet that outputs all job names:

    def hi = hudson.model.Hudson.instance
       hi.getItems(hudson.model.Project).each {project ->
       println(project.displayName)
    }
    
  • Code snippet that extracts n from LEVEL_n (implemented as closure):

    def level = { name ->
      def ret = 0
      name.eachMatch(~'LEVEL_([1-9]+[0-9*])', {ret = it[1].toInteger()})
      return ret
    }
    
  • Code snippet that gets statuses for all the latest builds:

    def hi = hudson.model.Hudson.instance
    hi.getItems(hudson.model.Project).each {project ->
      println(project.lastBuild.result)
    }
    
  • Link to the method that starts a build.

Note: things get a bit hairier if you are using Matrix builds. But as long as you don't this should be enough.


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

1.4m articles

1.4m replys

5 comments

57.0k users

...