限制Jenkins管道仅在特定节点上运行

Oph*_*ens 39 jenkins jenkins-pipeline

我正在建立将广泛使用Jenkins piplines的工作.我们的节点按标签按项目指定,但与常规作业不同,管道构建似乎没有"限制此项目可以运行的位置"复选框.如何指定管道将以与常规作业相同的方式运行在哪个节点上?

Jon*_*n S 43

您执行此node步骤时指定所需的节点或标记:

node('specialSlave') {
   // Will run on the slave with name or tag specialSlave
}
Run Code Online (Sandbox Code Playgroud)

有关参数的扩展说明,请参阅https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#code-node-code-allocate-nodenode.


red*_*ven 33

为了记录,我们在这里也有声明性管道示例(选择一个标签为'X'的节点):

pipeline {
    agent { label 'X' }
...
...
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您想在具有多个标签之一的节点上运行:“agent { label 'X||Y' }” (4认同)

ein*_*rne 11

需要明确的是,由于管道有两个语法,因此有两种方法可以实现。

陈述式

pipeline {
    agent none

    stages {
        stage('Build') {
            agent { label 'slave-node?' }
            steps {
                echo 'Building..'
                sh '''
                '''
            }
        }
    }

    post {
        success {
            echo 'This will run only if successful'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

脚本化

node('your-node') {
  try {

    stage 'Build'
    node('build-run-on-this-node') {
        sh ""
    }
  } catch(Exception e) {
    throw e
  }
}
Run Code Online (Sandbox Code Playgroud)