How to trigger a jenkins build on specific node using pipeline plugin?

前端 未结 4 563
广开言路
广开言路 2020-12-03 10:13

I have a Jenkins pipeline job called \"TestPipeline\". I want to trigger a build on 2 different slaves which labeled \"tester1\' and \"tester2\". And the pipeline script is

相关标签:
4条回答
  • 2020-12-03 10:37

    I did the same, but using Node parameter, so I can use nodes list running job directly, or running pipeline (in pipeline I use Choice parameter to have list of available nodes). So in my case:

    1. Install Node and Label parameter plugin
    2. In test_job's configuration, select 'This build is parameterized' and add a Node parameter and set the parameter name to node_name
    3. In pipeline add Choice parameter, name it node, put there possible choices, so running pipeline you can choose on which node it should be run.

    Code to insert into pipeline script:

    build job: 'my_job', 
      parameters: [[$class: 'NodeParameterValue', name: 'node_name', labels: ["$node"], nodeEligibility: [$class: 'AllNodeEligibility']]]
    
    0 讨论(0)
  • 2020-12-03 10:43

    Please see the bug here. 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.

    However, I think it is only a workaround. I still believe this is a bug. Because the node step should do its job instead of letting other plugins to do for it.

    0 讨论(0)
  • 2020-12-03 10:54

    I just tested this on my installation, and it correctly ran each script on each node. You might want to check that you've configured your slaves correctly. I believe the documentation says they need to have the Launch slave agents via Java Web Start setting, you could verify that.

    0 讨论(0)
  • 2020-12-03 11:00

    Here is how i got it working

    1. create a 'job' 'test_job' with parameter type 'label' , name 'node', value can be any string. (this is the job to be triggered) .Set 'Restrict where this project can be run' to the label value

    2. create a 'pipeline' with parameter type "Node". From the pipeline section, use the following script

    Note the label for test_job is ${env.NODE_NAME} which will be set by the pipeline based on user's choice

    pipeline {
        agent any
        stages {
            stage('Build') {
                steps {
                  echo 'Building..'
                  script {
                    build job: 'test_job', parameters: [
                    [$class: 'LabelParameterValue', name: 'node', label: "${env.NODE_NAME}" ]
                    ]
    }}}}}
     

    0 讨论(0)
提交回复
热议问题