【问题标题】:Can I specify node using Scripted Pipeline in Jenkins?我可以在 Jenkins 中使用 Scripted Pipeline 指定节点吗?
【发布时间】:2019-03-19 02:00:26
【问题描述】:

我注意到 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 ''' '''
}

但 Jenkins 似乎找不到我要运行的节点。

【问题讨论】:

    标签: jenkins jenkins-pipeline


    【解决方案1】:

    这个简单的例子怎么样?

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

    【讨论】: