21

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.    
Eric Ipsum
  • 553
  • 1
  • 6
  • 20

5 Answers5

19

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.

Bharat
  • 2,362
  • 2
  • 32
  • 54
Claudio A
  • 231
  • 1
  • 5
  • 1
    Hum, it's the same solution I provided before you :( (see my post + my comment) – Bruno Lavit Jan 05 '16 at 08:25
  • u have got super solution from Claudio and Bruno...:). To build on morning 10.30am : cron job will b like this: 30 10 * * * %build_param=value To build on evening 6:30pm: cron job will b like this: 30 18 * * * %build_param=value – noor Jan 05 '16 at 11:25
  • 2
    Now it's an official plugin: https://wiki.jenkins.io/display/JENKINS/Parameterized+Scheduler+Plugin – VadimBelov Jan 25 '18 at 15:16
  • 1
    is this plugin working now? I could not be able to schedule pramaterized build with below text in 'build periodically with parameters schedule text box' 49 11 * * * % slavename=VMOMEGAPERF01 – merlachandra Apr 29 '19 at 06:31
9

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)

Bruno Lavit
  • 9,486
  • 2
  • 27
  • 37
  • thanks for your wise suggestion. But is this possible to use like this : H/15 * * * * %Server=qa1 H/30 * * * * %Server=qa2 I want to give two schedule in different server. – Eric Ipsum Jan 04 '16 at 16:19
  • or use any if else condition like execute at morning in qa1 server and in others time execute in qa2 server – Eric Ipsum Jan 04 '16 at 16:23
  • Maybe you can try to mix my solution with this plugin: https://wiki.jenkins-ci.org/display/JENKINS/NodeLabel+Parameter+Plugin. It allows to manage the node name with a parameter. – Bruno Lavit Jan 04 '16 at 16:28
5

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

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?

congusbongus
  • 10,870
  • 5
  • 64
  • 86
0

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

Kirill
  • 3,834
  • 2
  • 28
  • 51