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

问题描述 投票:39回答:3

我正在建立将广泛使用Jenkins基准线的工作。我们的节点是通过项目的标签来指定的,但是与常规作业不同,管道构建似乎没有“限制在何处可以运行此项目”复选框。如何指定管道将在常规作业上以哪种方式运行?

jenkins jenkins-pipeline
3个回答
43
投票

[在执行node步骤时指定所需的节点或标签:

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

请参见https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#node-allocate-node,以获取对node参数的扩展解释。


33
投票

为了记录,我们这里也有declarative pipeline示例(选择一个带有标签'X'的节点):

pipeline {
    agent { label 'X' }
...
...
}

11
投票

要清楚,因为管道具有两个语法,所以有两种方法可以实现。

说明性
pipeline {
    agent none

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

    post {
        success {
            echo 'This will run only if successful'
        }
    }
}

脚本化
node('your-node') {
  try {

    stage 'Build'
    node('build-run-on-this-node') {
        sh ""
    }
  } catch(Exception e) {
    throw e
  }
}

© www.soinside.com 2019 - 2024. All rights reserved.