22
votes

I have a jenkins job. i want to build my job in a specific time with a build parameter.

I want to do this by using the Build periodically option.

I have input like this:

*/1 * * * * Parameter1

If I do this, jenkins show an error.

Is this possible without using any plugin.

if not, than which plugin will be better

Alternatively is there a way to give parameter's here in the schedule?

My actual requirement is like this:

 build in morning using one parameter
 build in evening using another parameter.    
5

5 Answers

20
votes

Basically, with the 'Build periodically' option you can't schedule a Jenkins job with parameters.

However, to schedule a job at different times that needs to use different environments, you have to use the parameterized-scheduler plugin or search for it in (Manage Jenkins -> Manage Plugins -> Parameterized Scheduler).

Examples:

 # Parameter1
 H/15 * * * * %Parameter1
 # Parameter2
 H/30 * * * * %Parameter2

Remember you have to have your parameters already setup because the plugin is visible only for jobs with parameters.

The Node and Label parameter plugin can help since it allows you to select individual nodes assuming your different servers qa1 and qa2 are already configured. Hope that clarifies things for you.

10
votes

With the native Jenkins crontab, it's not possible.

But it should be possible with this plugin: https://github.com/jwmach1/parameterized-scheduler

You have to fork the repo and build this plugin + do a manual installation.

This tutorial explains how to build a custom plugin: https://wiki.jenkins-ci.org/display/JENKINS/Plugin+tutorial

(Setting Up Environment + Building a Plugin)

5
votes

Maybe not exactly what you want, but it is an interesting hack I found out so I decided to share. You can programmatically set parameters of Jenkins job depending on the environment.

# check if job was trigered by timer
if [ $(env | grep -E '^BUILD_CAUSE=TIMERTRIGGER$') ] ; then 
  # your logic here, utilise the power of bash   
  if [ $(date +"%H") -eq 16 ] ; then PARAM=VALUE_1 ; fi
  if [ $(date +"%H") -eq 17 ] ; then PARAM=VALUE_2 ; fi
fi
1
votes

Without plugins, you can try cloning the job, and creating a build schedule with different parameter values. I.e. you might have job_morning and job_evening.

See How do I clone a job in jenkins?

0
votes

Scheduling parameterized job is possible if there is a default value.

Here I will give an example using Jenkinsfile. Suppose in your pipeline script you have a param testUserName defined:

pipeline {
    parameters {
        string(name: 'testUserName', defaultValue: 'defaultTestUser', 
                description: 'Username to use for test scenarios')
    }

    stages {
        stage('Run tests') {
            steps {
                sh "mvn verify --batch-mode -Dtest.user=${params.testUserName}"
            }
        }
    }
}

When you press the "Build now" button, the job will run for the first time without asking for params (defaultValue will be used). After downloading and processing Jenkinsfile within that first run, the button name will change to "Build with Parameters". You click the button and type another user (not the default one defined in Jenkinsfile). The problem is that the value you typed will not persist between job runs. It will always reset to defaultValue.

To prevent value reset between job runs replace

defaultValue: 'defaultTestUser'

to

defaultValue: params.testUserName ?: 'defaultTestUser'

Now the job will always run with a value previously specified on "Build with Parameters". Found this solution on dev.to