Limiting Jenkins pipeline to running only on specific nodes

后端 未结 3 1624
南方客
南方客 2021-02-03 17:25

I\'m building jobs that will be using Jenkins piplines extensively. Our nodes are designated per project by their tags, but unlike regular jobs the pipeline build does not seem

相关标签:
3条回答
  • 2021-02-03 17:49

    You specify the desired node or tag when you do the node step:

    node('specialSlave') {
       // Will run on the slave with name or tag specialSlave
    }
    

    See https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#node-allocate-node for an extended explanation of the arguments to node.

    Edit 2019: This answer (and question) was made back in 2017, back then there only was one flavor of Jenkins pipeline, scripted pipeline, since then declarative pipeline has been added. So above answer is true for scripted pipeline, for answers regarding declarative pipeline please see other answers below.

    0 讨论(0)
  • 2021-02-03 17:49

    For the record let's have the declarative pipeline example here as well (choosing a node which has the label 'X'):

    pipeline {
        agent { label 'X' }
    ...
    ...
    }
    
    0 讨论(0)
  • 2021-02-03 18:04

    To be clear, because Pipeline has two Syntax, there are two ways to achieve that.

    Declarative

    pipeline {
        agent none
    
        stages {
            stage('Build') {
                agent { label 'slave-node​' }
                steps {
                    echo 'Building..'
                    sh '''
                    '''
                }
            }
        }
    
        post {
            success {
                echo 'This will run only if successful'
            }
        }
    }
    

    Scripted

    node('your-node') {
      try {
    
        stage 'Build'
        node('build-run-on-this-node') {
            sh ""
        }
      } catch(Exception e) {
        throw e
      }
    }
    
    0 讨论(0)
提交回复
热议问题