Saturday, November 30, 2019

How to schedule a job to a node with pipeline

During jenkins pipeline execution, we might run a build task,which is another freestyle job, multiple times on multiple nodes.

If it is a normal groovy code the keeping them in the node{} block is enough.

But, with a freestyle job running using build task, it doesn't work.

The solution is as follows.
  1. Install Node and Label parameter plugin
  2. In test_job's configuration, select 'This build is parameterized' and add a Label parameter and set the parameter name to 'node'
  3. In pipeline script, use code:
build job: 'test_job', parameters: [[$class: 'LabelParameterValue', name: 'node', label: 'tester1']]
build job: 'test_job', parameters: [[$class: 'LabelParameterValue', name: 'node', label: 'tester2']]
And the job will be built as I wanted.You can replace the node value using the environment variable also as below.
Sample pipeline part of code I used.


node('master'){
                    echo "NODE_NAME = ${env.NODE_NAME}"
                    build job: 'cleanRecycleBin', quietPeriod: 2, parameters: [[$class: 'LabelParameterValue', name: 'node', label: "${env.NODE_NAME}"]]
                    build job: 'cleanDaysOlder', parameters: [[$class: 'LabelParameterValue', name: 'node', label: "${env.NODE_NAME}"], string(name: 'sPath', value: 'C:\\Program Files (x86)\\Jenkins\\workspace\\BuildDM'), string(name: 'days', value: '2'), string(name: 'sExt', value: 'zip')], quietPeriod: 2
                    build job: 'cleanDaysOlder', parameters: [[$class: 'LabelParameterValue', name: 'node', label: "${env.NODE_NAME}"], string(name: 'sPath', value: 'C:\\Program Files (x86)\\Jenkins\\workspace\\JPMC-BuildDM'), string(name: 'days', value: '2'), string(name: 'sExt', value: 'zip')], quietPeriod: 2
                    build job: 'cleanDaysOlder', parameters: [[$class: 'LabelParameterValue', name: 'node', label: "${env.NODE_NAME}"], string(name: 'sPath', value: 'C:\\Program Files (x86)\\Jenkins\\workspace\\BuildDM_lynx'), string(name: 'days', value: '2'), string(name: 'sExt', value: 'zip')], quietPeriod: 2
                    
                }



Reference

No comments:

Post a Comment