Limiting Jenkins pipeline to running only on specific nodes

你。 提交于 2019-12-03 08:08:46

问题


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 to have the "Restrict where this project can be run" checkbox. How can I specify on which node the pipeline will run the way I do for regular jobs?


回答1:


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/#code-node-code-allocate-node for an extended explanation of the arguments to node.




回答2:


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' }
...
...
}



回答3:


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
  }
}


来源:https://stackoverflow.com/questions/42652533/limiting-jenkins-pipeline-to-running-only-on-specific-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!