首页 > 解决方案 > 我可以在 Jenkins 中使用 Scripted Pipeline 指定节点吗?

问题描述

我注意到 Jenkins 管道文件 -- Jenkinsfile 有两种语法

我已经使声明性脚本工作以指定节点来运行我的任务。但是我不知道如何将我的脚本修改为脚本语法。

我的声明性脚本

pipeline {
    agent none

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

                '''
            }
        }
        stage('Test') {
            agent { label 'my-label​' }
            steps {
                echo 'Testing..'
                sh '''

                '''
            }
        }
        stage('Deploy') {
            agent { label 'my-label​' }
            steps {
                echo 'Deploying....'
                sh '''

                '''
            }
        }
    }
}

我曾尝试以这种方式使用:

node('my-label') {
  stage 'SCM'
  git xxxx

  stage 'Build'
  sh ''' '''
}

但似乎詹金斯找不到我的节点来运行。

标签: jenkinsjenkins-pipeline

解决方案


这个简单的例子怎么样?

stage("one") {
    node("linux") {
        echo "One"
    }
}
stage("two") {
    node("linux") {
        echo "two"
    }
}
stage("three") {
    node("linux") {
        echo "three"
    }
}

或者下面的答案,如果有多个具有相同标签的节点并且运行被另一个作业中断,则可以保证这些阶段在同一个节点上运行。上面的示例将在每个阶段后释放节点,下面的示例将保留所有三个阶段的节点。

node("linux") {
    stage("one") {
        echo "One"
    }
    stage("two") {
        echo "two"
    }
    stage("three") {
        echo "three"
    }
}

推荐阅读