Run Jenkins stage on different nodes

被刻印的时光 ゝ 提交于 2019-12-06 10:18:39

问题


I have the following Jenkinsfile of a multibranch pipeline architecture

#!/usr/bin/groovy

pipeline {
    agent {
        node {
            label 'ubuntu'
            customWorkspace "/src/$BUILD_NUMBER"
        }
    }
    environment {
        SRC_DIR = "$WORKSPACE"
        BUILD_DIR="/build/$BUILD_NUMBER"
    }

    stages {
        stage('Build') {
            steps {
                dir(BUILD_DIR) {
                    sh '$SRC_DIR/build.sh'
                }
            }
        }

        stage('Test') {
            steps {
                dir(BUILD_DIR) {
                   sh '$SRC_DIR/test.sh'
                }
            }
        }
    }
}

I am trying to run the 'Build' stage on Ubuntu and Red Hat nodes in parallel, and the 'Test' stage on the Ubuntu node only.

Can anybody help me in specifying how to choose which stage are run on which nodes. I found few solutions online but they recommended rewriting the build stage twice: once for the Red Hat node and the other for the Ubuntu node. Isn't there a way to do this without code duplication ?

Thank you very much


回答1:


Sure, you would want to label your slave nodes somehow. Basically configure all the node on Jenkins and give them meaningful names.

  stage('Build') {
   steps {
     node('os_linux') {
       sh './build.sh' 
     }
     node('os_redhat') {
       sh './build.sh' 
   }
  }

This will run the tasks in serial, and Jenkinsfile syntax also supports executing commands in parallel on different nodes.

Thanks,




回答2:


A bit late to the party, but still ... You can use script {} so you can create the label you need. Something like this:

stage('Build') {
    steps {
        script {
            dev label = 'RHEL'
            if (env.ENV == 'ubuntu') {
                label = 'Ubuntu'
            }
            node("${label}") {
                dir(BUILD_DIR) {
                    sh '$SRC_DIR/build.sh'
                }
            }
        }
    }
}


来源:https://stackoverflow.com/questions/48284128/run-jenkins-stage-on-different-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!