Jenkinsfile and multiple nodes

不羁的心 提交于 2020-12-29 07:14:00

问题


I have some code that needs running (build, test, and packages in actuality but for example just running tox) on different OSes. Currently my Jenkinsfile looks like thus:

pipeline {

    // Where to run stuff.
    agent { 
        node {
            label 'CentOS7' 
            customWorkspace '/home/build/jenkins/workspace/pipelines/ook'
        }
    }

    // What to run goes here.
    stages {
        stage('Tox') {
            steps {
                sh 'tox -v --recreate' 
            }
        }
    }

    // Clean up after ourselves.
    post {
        failure {
            mail subject: "\u2639 ${env.JOB_NAME} (${env.BUILD_NUMBER}) has failed",
                    body: """Build ${env.BUILD_URL} is failing!
    Somebody should do something about that\u2026""",
                          to: "devs@example.com",
                     replyTo: "devs@example.com",
                        from: 'jenkins@example.com'
            }
        }
    }
}

The middle bit, I want to run on two different nodes: one for OS 1 and one for OS 2.

How do I do that?


回答1:


Sure, you would want to label your slave nodes somehow. I didn't look up what tox is, but maybe like 'os_linux' and 'os_mac', and then you can use the node step in your Jenkinsfile to run some commands in the context of each slave. So your Tox stage might look like:

stage('Tox') {
  steps {
    node('os_linux') {
      sh 'tox -v --recreate' 
    }
    node('os_mac') {
      sh 'tox -v --recreate' 
    }
  }
}

This will run the tasks in serial, and Jenkinsfile syntax also supports doing those two tox commands in parallel on different nodes. Use the "Pipeline Syntax" link in the left nav of your Jenkins UI (only on pipeline jobs) to play around with node and parallel. Rock on.



来源:https://stackoverflow.com/questions/43764447/jenkinsfile-and-multiple-nodes

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