add build parameter in jenkins build schedule
Asked Answered
D

5

30

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.    
Deloresdeloria answered 4/1, 2016 at 15:40 Comment(0)
D
28

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.

Determinant answered 4/1, 2016 at 20:54 Comment(4)
Hum, it's the same solution I provided before you :( (see my post + my comment)Propane
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=valueHerta
Now it's an official plugin: wiki.jenkins.io/display/JENKINS/Parameterized+Scheduler+PluginBelated
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=VMOMEGAPERF01Chaffinch
P
10

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)

Propane answered 4/1, 2016 at 16:0 Comment(3)
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.Deloresdeloria
or use any if else condition like execute at morning in qa1 server and in others time execute in qa2 serverDeloresdeloria
Maybe you can try to mix my solution with this plugin: wiki.jenkins-ci.org/display/JENKINS/NodeLabel+Parameter+Plugin. It allows to manage the node name with a parameter.Propane
M
6

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
Muraida answered 23/5, 2019 at 14:23 Comment(1)
The BUILD_CAUSE variable depends on the EnvInject PluginKeefe
B
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?

Blavatsky answered 22/11, 2018 at 4:5 Comment(0)
M
1

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

Mellie answered 15/6, 2020 at 13:22 Comment(1)
That's pretty cool. If you don't want to go so far as changing the code, or want to leave defaults but still have the ability to re-use params from runs, you can use the Rebuilder plugin.Illene

© 2022 - 2024 — McMap. All rights reserved.