1. 程式人生 > >Jenkins pipeline 並行執行任務流

Jenkins pipeline 並行執行任務流

logs inux st2 stage1 聲明 master src demo usb

筆者在《Jenkins 在聲明式 pipeline 中並行執行任務》一文中介紹了如何在聲明式 pipeline 中執行並行的任務。前一段時間,Jenkins 發布了 1.3 版的聲明式 pipeline(declarative pipeline),這個版本繼續增強了並行執行任務的能力:並行執行的任務可以是個任務流。官方稱這一功能為 "sequential stages",本文將解釋 "sequential stages",並通過 demo 演示其用法。

之前的任務並行方式

就是筆者在《Jenkins 在聲明式 pipeline 中並行執行任務》一文中介紹的方式,我們在一個 stage 中設置多個子 stage 並行執行:

stages {
    stage(Stage1) {
        ...
    }
    stage(並行執行的 Stage) {
        parallel {
            stage(Stage2.1) {
                agent { label "test1" }
                steps {
                    echo "在 agent test1 上執行的並行任務 1."
                }
            }
            stage(Stage2.2
) { agent { label "test2" } steps { echo "在 agent test2 上執行的並行任務 2." } } } } stage(Stage3) { ... } }

上面代碼中任務的執行過程如下圖所示:

技術分享圖片

任務 2.1 和任務 2.2 並行執行。

並行執行任務流

過去並行執行的任務都是單個的,但實際情況中我們還需要任務流級別的並行能力,如下圖所示:

技術分享圖片

上圖中顯示有兩條任務流在並行的執行,我們可以通過下面的代碼來實現:

pipeline {
    agent none

    stages {
        stage(Stage1) {
            agent { label "master" }
            steps {
                timestamps {
                    echo 這是第一個被執行的 stage.
                    sleep 5
                }
            }
        }
        stage("build, deploy and test on Windows and Linux") {
            parallel {
                stage("windows") {
                    agent {
                        label "master"
                    }
                    stages {
                        stage("build") {
                            steps {
                                timestamps {
                                    echo "build on windows."
                                }
                            }
                        }
                        stage("deploy") {
                            steps {
                                timestamps {
                                    echo "deploy on windows."
                                }
                            }
                        }
                        stage("test") {
                            steps {
                                timestamps {
                                    echo "test on windows."
                                    sleep 5
                                }
                            }
                        }
                    }
                }

                stage("linux") {
                    agent {
                        label "worker1"
                    }
                    stages {
                        stage("build") {
                            steps {
                                timestamps {
                                    echo "build on linux."
                                }
                            }
                        }
                        stage("deploy") {
                             steps {
                                 timestamps {
                                     echo "deploy on linux."
                                 }
                            }
                        }
                        stage("test") {
                            steps {
                                timestamps {
                                    echo "test on linux."
                                    sleep 5
                                }
                            }
                        }
                    }
                }
            }
        }
        stage(Stage3) {
            agent { label "worker1" }
            steps {
                timestamps {
                    echo 這是最後一個被執行的 stage.
                }
            }
        }
    }
}

為了顯示任務的執行時間,筆者使用了 timestamper 插件。下圖顯示了筆者精簡後的運行日誌:

技術分享圖片

紅框中的內容說明我們的兩個任務流是完全並行執行的。這就是 1.3 版的聲明式 pipeline 中增加的 "sequential stages" 功能。

總結

如今 jenkins 對聲明式 pipeline 中並行任務的執行支持的非常給力(雖然經歷了一個稍顯漫長的過程)。筆者在 2017 年初調研時發現聲明式 pipeline 無法支持並行的任務,後來開始支持比較初級的並行任務,筆者在《Jenkins 在聲明式 pipeline 中並行執行任務》一文中進行了介紹。到今年(2018) 7 月份聲明式 pipeline 發布了版本 1.3,這個版本中開始支持本文介紹的任務流級別的並行。至此筆者認為 jenkins 聲明式 pipeline 中任務的並行執行功能已經比較完善了。

參考:
Sequential Stages (declarative pipeline 1.3 的新功能)

Jenkins pipeline 並行執行任務流