How to trigger a Jenkins job by another job at a certain time?

梦想与她 提交于 2019-12-08 03:00:13

问题


I have two jobs, JobA and JobB, JobA runs every day at 13.00 and registers some payments. I want JobA to trigger JobB which verifies the payments, if and only if JobA is successful and JobB needs to be run the next day at 04.00

Any idea how to do this?

BR


回答1:


I have not been able to find anything that will do this out of the box. You can, of course, schedule a job to build periodically, but that's not all that you want.

You could try one of these 2 ideas (I have not implemented either myself).

  1. Set JobB to build periodically at 4am, but deactivate the job. Create an intermediary job with a build trigger to build if JobA is successful (e.g. set JobA in the projects to watch section). The intermediary job would run code to activate JobB - groovy code using the groovy plugin would be easiest, or you could use the rest api via shell / batch script. Then as the last build step in JobB, run a similar script to deactivate the job again.
  2. The Schedule Build Plugin allows you to schedule future builds. However, it appears that this is a manual process. If you can figure out how to programatically fire off a scheduled build through this plugin, you could add that code to an intermediary job that is setup in the same way as mentioned in option #1.



回答2:


You can use build job: step in pipeline (https://jenkins.io/doc/pipeline/steps/pipeline-build-step/) and quite period in seconds to trigger the job at required time.

EDIT:

The way i did def currentDate = GregorianCalendar.getInstance()

If you want next day at 5 am def plannedDate = new GregorianCalendar(currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DAY_OF_MONTH) + 1, 5, 0)

def quietPeriod = (plannedDate.getTime().getTime() - currentDate.getTime().getTime())/1000



来源:https://stackoverflow.com/questions/37003317/how-to-trigger-a-jenkins-job-by-another-job-at-a-certain-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!