4

We have a lot of jenkins tasks that process some date-dependent data, for example, aggregations per hour, per day.

All of them are configured to run periodically but native jenkins isn't able to trigger periodical job automaticaly with dynamic parameters, and we have to calculate required parameters inside the script at execution time, e.g in the bash script code:

PREVHOUR=$(date --date="-1 hour" "+%Y-%m-%d %H")

We can also use $BUILD_ID environment variable to get build start time.

The problem is: When all of the slots (workers) are busy, jenkins puts this job into queue. And the parameter calculations will be wrong when such a task is executed next hour after triggering.

So, we can't find the way to get TRIGGER time, not build starting time.

Of Course, there are few inconvenient solutions as:

  • run simple periodical job on a reserved machine which triggers other jobs by url with params
  • file that track the last run time of script

We have tried to find plug-ins that will fit our needs, and have found this plugin, but it works only in manual (UI "build now" click) mode.

Is there any plug-in for Jenkins to calculate dinamic parameters at periodical trigger time?

Thanks!

Ivan Klass
  • 6,407
  • 3
  • 30
  • 28

2 Answers2

4

You can use ScriptTrigger plugin. It evaluates Groovy script periodically and supports parameterized builds.

  1. Add a TRIGGER_TIME parameter to a project.
  2. Use Groovy script:

    now = new Date();
    param = new File("trigger.time");
    param.write("TRIGGER_TIME="+now.toString());
    return true;
    

    It will write trigger time to a properties file "trigger.time" on every polling and trigger a new build.

  3. Set Properties File Path (in advanced settings) to a "trigger.time", and every scheduled build will get TRIGGER_TIME parameter with designated time.
  • 1
    Thank you very much! Sounds great, but I worry about concurrent builds: as I understand, when a few free slots appear, new builds will read one property file, won't they? I mean, they will probably make calculations for the last trigger time of that kind of build. But I see the way to prevent it : append time value to file on every triggering and read and remove first value by the script. Possibly, we should pay attention to file write-locking/waiting for lock release inside the script. – Ivan Klass May 16 '12 at 03:34
  • If you like an answer, please show it by clicking the up-arrow above the points. – inger Mar 11 '13 at 16:48
  • How can the `TRIGGER_TIME` be used to poll SCM? The answer seem to have told how to create `TRIGGER_TIME`. – Heinz Apr 10 '17 at 17:51
2

Two suggestions:

  1. Trigger job, IMHO, is a perfectly reasonable solution. You may use a much more convenient method of triggering the jobs though - Parameterized Trigger Plugin.

  2. Another approach is to use EnvInject Plugin. For example, you can calculate your parameters in a script, save them into a property file, and then inject the environment variables from the file with the plugin. See this answer.

Community
  • 1
  • 1
malenkiy_scot
  • 16,415
  • 6
  • 64
  • 87