How to schedules jobs with specific parameters in a Jenkins multibranch pipeline

J4N picture J4N · Jun 15, 2018 · Viewed 7.2k times · Source

We were having 2 FreeStyle projects on Jenkins:

One to generate builds(daily builds+manual builds), one other to execute tests.

We are moving to a Multibranch pipeline on jenkins, so my understanding is that we have one project per repository, and that we should use options to have different behavior.

So I can create parameters, to indicate if we want to run the tests, if we want to build the setups, that part I'm ok with it.

My issue is that I need that by default, the tests are NOT executed(because they take a lot of time to generate, and I don't want that developers can by mistake just let the "Execute tests" option checked.

And I need that this option is checked when executing the daily build in the night.

So 2 questions:

  1. How to schedule?
  2. How to provide the parameters value used for this schedule?

Answer

Vitalii Vitrenko picture Vitalii Vitrenko · Jun 27, 2018

You can create a separate multi-branch job that will run by schedule and trigger your main job overriding all necessary parameters. It will look something like this

pipeline {
    agent any
    triggers {
        pollSCM('0 0 * * *')
    }
    stages {
        stage('Triggering the main job') {
            steps {
                build job: "main/${BRANCH_NAME.replace('/', '%2F')}", 
                      parameters: [string(name: 'RUN_TESTS', value: 'true')]
            }
        }
    }
}

You should put this file along with your main Jenkinsfile in the repository and configure a separate multi-branch pipeline job to use this file.