Running a parameterized job at a particular time in jenkins
Asked Answered
H

6

19

I have a parameterized job which I want to schedule it to run at a particular time. Is there anyway I can choose my parameters and schedule it to run.

Thanks in advance

Heartland answered 29/5, 2013 at 9:51 Comment(0)
H
26

At least two ways to handle this,
but you have to know the desired values in advance:

  • Set the job's default values to what you need,
    then schedule it to run whenever you like.

  • Instead of scheduling the parameterized job,
    create another job that will trigger the
    parameterized job while passing the relevant
    values to the parameters
    (This also allows you to initiate the target-job
    with different sets of values).

EDIT:

This third method will allow you to update the values of the parameters as needed:

  • Set the job to read values of parameters from a configuration-file
    (via the EnvInject Plugin), then update the content of that configuration-file
    before running the job.

Note it will be bad-practice to modify this file before each run,
as there are better ways to automatically initiate Jenkins-jobs with parameters.

EDIT 2:

A few years had passed, and now we have the Parameterized Scheduler
to help schedule a job at different times with different parameters
(see examples there for both "Classic" and Declarative Pipelines).

Homoeo answered 29/5, 2013 at 22:2 Comment(4)
Hey @gonen, This might be silly but how do we 'Set the job's default values' ?Quillen
@Maulzey, each parameter in a Jenkins-job has a default value - the content of a text-field, the state of a checkbox (set or unset), the first item of a dropbox and such. If not sure, the default values are those you see when you choose 'Build now'.Homoeo
TY ! Got it sorted :DQuillen
Thanks. I had to use the additional job which calls the other job with parameters as my jobs are more generic which handle multiple branches and environments however this is increasing the quickly growing number of jobs I have already.Savagism
F
4

When build triggers are used to start a build, there's no way to pass parameters. Since the job is parameterized, the auto-start job will run with the default parameters.

If this is good enough , then you can simply set the schedule you want it to run via the 'Build Triggers' section of the job config page, you would then choose the 'Build periodically' checkbox and add the appropriate values (using syntax of cron). Let the default params take effect.

However, you could POST to jenkins as a trigger, and pass the exact values you wish. You then woudl have to use CRON directly for this with a shell script likely, so you can set the param values.

For example,

http://example.com/jenkins/job/this_job/buildWithParameters?PARAMETER=Value

Faradic answered 29/5, 2013 at 20:2 Comment(0)
K
4

Building on Gonen's answer a little bit, you can use the Build Flow Plugin to make this a bit easier on yourself. After installing, create a new job and set the type to "Build Flow". Configure this job to run at the desired, scheduled time. In the "Define build flow using flow DSL" section for this job, specify the job(s) you want it to build:

build( "Job You Want Built", param1:"whatever", Param2:"whatever")

You can specify more jobs to be built by adding more lines, or repeat the same line and change the parameters to build the job more than once with different params.

Once you're done, your Build Flow job will run at the scheduled time and kick off the desired jobs with the specified parameters.

Ketty answered 30/4, 2014 at 18:16 Comment(0)
B
4

The Jenkins crontab allows scheduling a job to run at a particular time but does not yet allow parameters to be passed.

However there is this Jenkins feature request to extend it to pass parameters. Once available it would let you do what you want without the need to create additional Jenkins jobs as some of the other answers suggest.

You can vote for this feature to be included in Jenkins sooner if it will help you.

Becca answered 8/2, 2016 at 16:17 Comment(4)
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From ReviewMagnetism
@Magnetism The feature I've linked to will do what the author wants once implemented so I thought it was helpful and relevant for others to know it exists. I have re-worded my answer to make this clearer.Becca
The operative phrase is "once implemented". Yes, this is helpful and relevant: as a comment, I'd vote it up.Magnetism
Ticket was closed as fixed with reference to Parameterized Scheduler which is the proposed solution in this answer.Gavette
E
4

You can use the Parameter Scheduler Plugin

It allows you to specify parameters after the cron schedule, for example:

H(0-29)/10 * * * * % name=value; othername=othervalue

The name value pairs after the % are interpreted as parameters.

Exsanguinate answered 11/2, 2016 at 18:0 Comment(0)
I
1

It is possible to manually set values for parameters of parameterized job once and those values will be re-used in subsequent scheduled runs.

To do this, you need to define a default value for parameter like this:

parameters {
    string(name: 'testUserName', defaultValue: params.testUserName ?: 'defaultTestUser')
}

See my detailed answer here

Irrigation answered 15/6, 2020 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.