【问题标题】:Running same Jenkins job on multiple agents in parallel in declarative pipeline在声明式管道中并行在多个代理上运行相同的 Jenkins 作业
【发布时间】:2018-02-22 14:53:23
【问题描述】:

这是我所拥有的:

#!/usr/bin/env groovy

pipeline {
    agent none
    stages {
        stage('Checkout SCM') {
            agent { label 'win' && 'apple' && 'rhel' }
            steps {
                echo "Cloning Repository"
                checkout([$class: 'GitSCM',
                    branches: [[name: "*/develop"]],
                    doGenerateSubmoduleConfigurations: false,
                    extensions: [[$class: 'WipeWorkspace']],
                    submoduleCfg: [],
                    userRemoteConfigs: [[credentialsId: 'UNAME', url: 'URL']],
                    browser: [$class: 'BitbucketWeb', repoUrl: 'URL'],
                ])}}

        stage('Building Win64, Linux764, MacOS') {
            agent { label 'win&&rhel&&apple' }
            steps {
                   script {
                        echo '************************'
                        echo '*****BUILDING JOBS******'
                        echo '************************'
                        sh 'python build.py'
                        sh 'cd ion-js && npm run prepublishOnly'
                      }}}
    }
} 

但是我得到了There are no nodes with the label ‘win && rhel && apple’ 错误。有谁碰巧知道如何运行声明式 jenkins 管道,其中一个阶段在多个代理标签上并行运行?

我想同时将同一个 git repo 签出到 3 个不同的节点。我试过agent { label 'win' && 'apple' && 'rhel' }agent { label 'win&&apple&&rhel' } 但它只是说找不到那个标签。

Here they say you can use || 和使用 && 应该可以工作,但我不确定我错过了什么。我可以编写 3 个不同的结帐阶段,但我认为有更好的方法

【问题讨论】:

  • 我也遇到了类似的问题,你知道怎么解决吗?

标签: jenkins groovy jenkins-pipeline


【解决方案1】:

要添加更多来自 Micah 的回答, 您可以定义一个函数来为您创建一个阶段,并将在您选择的不同代理节点上执行生成阶段,而不是使用不同的标签重复阶段。

def agents  = ['win64', 'linux64', 'macos']
 
def generateStage(nodeLabel) {
    return {
        stage("Runs on ${nodeLabel}") {
            node(nodeLabel) {
               script {
                    echo "Running on ${nodeLabel}"
                    echo '************************'
                    echo '*****BUILDING JOBS******'
                    echo '************************'
                    sh 'python build.py'
                    sh 'cd ion-js && npm run prepublishOnly'
                }
            }
        }
    }
}
def parallelStagesMap = agents.collectEntries {
    ["${it}" : generateStage(it)]
}
pipeline {
    agent none
    stages {
        stage('non-parallel stage') {
            steps {
                echo 'This stage will be executed first.'
            }
        }
        stage('parallel stage') {
            steps {
                script {
                    parallel parallelStagesMap
                }
            }
        }       
    }
}

由于我们在调用parallelStageMap时有parallel关键字,相同的阶段将在代理节点上并行执行。

专业提示:您可以在函数内定义更多步骤,这些步骤在所有代理上都可以执行。 如果你想定义标签和舞台名称,你可以添加另一个名为stagename的参数,并可以解析成函数generateStage

【讨论】:

  • 我在使用您的解决方案时收到错误 No such DSL method 'steps' found。在“generateStage”函数中,我替换了steps -> stage,并且成功了。
  • 从答案中删除步骤,假设编辑被接受
  • 是的,steps 是声明式管道语法,并且因为 parallel 步骤包含在 script 块中,所以预计 generateStage 将在关闭。
【解决方案2】:

我尝试过同样的事情,但没有成功。我知道的唯一解决方案是有一个parallel 块并为每个代理/节点/标签多次定义阶段。

stage('Building Win64, Linux764, MacOS') {
  parallel {
    stage('Win64') {
      agent {
        label 'win-10-x64'
      }
      steps {
      ...
      }
    }
    stage('Linux64') {
      agent {
        label 'linux-x64'
      }
      steps {
      ...
      }
    }
    stage('MacOS') {
      agent {
        label 'macos'
      }
      steps {
      ...
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多