在声明性管道中并行地在多个代理上运行相同的Jenkins作业

时间:2018-02-22 14:53:23

标签: jenkins groovy jenkins-pipeline

这就是我所拥有的:

#!/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管道,其中一个阶段并行运行在多个代理标签上?

我想同时向3个不同的节点签出相同的git repo。我已经尝试了agent { label 'win' && 'apple' && 'rhel' }agent { label 'win&&apple&&rhel' },但它只是说它无法找到该标签。

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

2 个答案:

答案 0 :(得分:1)

我尝试过同样的事情却没有成功。我所知道的唯一解决方案是拥有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 {
      ...
      }
    }
  }
}

答案 1 :(得分:0)

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

def agents  = ['win64', 'linux64', 'macos']
 
def generateStage(nodeLabel) {
    return {
        stage("Runs on ${nodeLabel}") {
            node(nodeLabel) {
                steps {
                   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 中。

相关问题