天天看點

Jenkins:申明式腳本基本組成(二)2基本組成

2基本組成

下面針對幾個核心概念,逐一進行說明

  1. Pipeline

指令說明:代表整條流水線,包含整條流水線的邏輯。Pipeline{}表示申明式腳本子產品。

作用域:應用于全局最外層,表明該腳本為聲明式pipeline

是否必須:必須

參數:無

  1. Agent

指令說明:agent部分指定整個Pipeline或特定階段将在Jenkins環境中執行的位置,具體取決于該agent 部分的放置位置。該部分必須在pipeline塊内的頂層定義 ,但stage級使用是可選的。

作用域:可用在全局與stage内

是否必須:是,

參數:any,none, label, node,docker,dockerfile

為了支援Pipeline可能擁有的各種用例,該agent部分支援幾種不同類型的參數。這些參數可以應用于pipeline塊的頂層,也可以應用在每個stage指令内。

參數說明:

參數any:在任何可用的agent 上執行Pipeline或stage。例如:agent any

pipeline{

    agent any  //全局必須帶有agent表明此pipeline執行節點

    stages{

        stage("first stage"){

            agent { label 'master' }  //具體執行的步驟節點,非必須

            steps{

                echo "this is first step"

            }

        }

    }

參數None:當在pipeline塊的頂層使用none時,将不會為整個Pipeline運作配置設定全局agent ,每個stage部分将需要包含其自己的agent部分。

參數label:使用提供的label标簽,在Jenkins環境中可用的代理上執行Pipeline或stage。例如:agent { label 'my-defined-label' }

參數node:agent { node { label 'labelName' } },等同于 agent { label 'labelName' },但node允許其他選項(如customWorkspace)。

參數docker:定義此參數時,執行Pipeline或stage時會動态供應一個docker節點去接受Docker-based的Pipelines。 docker還可以接受一個args,直接傳遞給docker run調用。例如:agent { docker 'maven:3-alpine' }或

docker

agent {

    docker {

        image 'maven:3-alpine'

        label 'my-defined-label'

        args  '-v /tmp:/tmp'

    }

}

Dockerfile:使用從Dockerfile源存儲庫中包含的容器來建構執行Pipeline或stage 。為了使用此選項,Jenkinsfile必須從Multibranch Pipeline或“Pipeline from SCM"加載。

預設是在Dockerfile源庫的根目錄:agent { dockerfile true }。如果Dockerfile需在另一個目錄中建立,請使用以下dir選項:agent { dockerfile { dir 'someSubDir' } }。您可以通過docker build ...使用additionalBuildArgs選項,如agent { dockerfile { additionalBuildArgs '--build-arg foo=bar' } }。

參數示例:

//運作在任意的可用節點上

agent any

//全局不指定運作節點,由各自stage來決定

agent none

//運作在指定标簽的機器上,具體标簽名稱由agent配置決定

agent { label 'master' }

//node參數可以擴充節點資訊

agent {

     node {

         label 'master'

         customWorkspace 'xxx'

}

}

//使用指定運作的容器

agent { docker 'python'  }

常用設定:

這些是可以應用于兩個或多個agent的選項。除非明确定義,否則不需要。

label

    一個字元串。标記在哪裡運作pipeline或stage

    此選項适用于node,docker和dockerfile,并且 node是必需的。

customWorkspace

    一個字元串。自定義運作的工作空間内。它可以是相對路徑,在這種情況下,自定義工作區将位于節點上的工作空間根目錄下,也可以是絕對路徑。例如:

agent {

    node {

        label 'my-defined-label'

        customWorkspace '/some/other/path'

    }

}

reuseNode

    一個布爾值,預設為false。如果為true,則在同一工作空間中。

    此選項适用于docker和dockerfile,并且僅在 individual stage中使用agent才有效。

3.stages

指令說明:包含一個或多個stage的序列,Pipeline的大部分工作在此執行。建議stages至少包含至少一個stage指令,用于連接配接各個傳遞過程,如建構,測試和部署等。

作用域:全局或者stage階段内,每個作用域内隻能使用一次

是否必須:全局必須

參數:無

pipeline{

    agent any

    stages{

        stage("first stage"){

            stages{  //嵌套在stage裡

                stage("inside"){

                    steps{

                        echo "inside"

                    }

                }

            }

        }

        stage("stage2"){

            steps{

                echo "outside"

            }

        }

}

}

看下運作結果,發現嵌套的stage也是能夠展現在視圖裡面的:

Jenkins:申明式腳本基本組成(二)2基本組成
  1. stage

指令說明:代表流水線的階段。每個階段都必須有名稱。

作用域:被stages包裹,作用在自己的stage包裹範圍内

是否必須:必須

參數:需要一個string參數,表示此階段的工作内容

備注:stage内部可以嵌套stages,内部可單獨制定運作的agent

5.steps

指令說明:代表階段中的一個或多個具體步驟(step)的容器。steps部分至少包含一個步驟,本例中,echo就是一個步驟。在一個stage中有且隻有一個steps。

作用域:被stage包裹,作用在stage内部

是否必須:必須

參數:無

6.post

指令說明:定義Pipeline或stage運作結束時的操作。post-condition塊支援post部件:always,changed,failure,success,unstable,和aborted。這些塊允許在Pipeline或stage運作結束時執行步驟,具體取決于Pipeline的狀态。

作用域:作用在pipeline結束後者stage結束後

條件:always、changed、failure、success、unstable、aborted

參數說明:

參數always:運作,無論Pipeline運作的完成狀态如何。

參數changed:隻有目前Pipeline運作的狀态與先前完成的Pipeline的狀态不同時,才能運作。

參數fixed:上一次完成狀态為失敗或不穩定(unstable),目前完成狀态為成功時執行。

參數regression:上一次完成狀态為成功,目前完成狀态為失敗、不穩定或中止(aborted)時執行。

參數failure:僅當目前Pipeline處于“失敗”狀态時才運作,通常在Web UI中用紅色訓示表示。

參數success:僅當目前Pipeline具有“成功”狀态時才運作,通常在具有藍色或綠色訓示的Web UI中表示。

參數unstable:隻有目前Pipeline具有“不穩定”狀态,通常由測試失敗,代碼違例等引起,才能運作。通常在具有黃色訓示的Web UI中表示。

參數aborted:隻有目前Pipeline處于“中止”狀态時,才會運作,通常是由于Pipeline被手動中止。通常在具有灰色訓示的Web UI中表示。

參數cleanup:清理條件塊。不論目前完成狀态是什麼,在其他所有條件塊執行完成後都執行。post部分可以同時包含多種條件塊。以下是post部分的完整示例。

示例:

pipeline {

    agent any

    stages {

        stage('Build') {

            steps {

                sh 'echo Build stage ...'

            }

            post {

                always {

                    echo "post condition executed: always ..."

                }

                changed {

                    echo "post condition executed: changed ..."

                }

                aborted {

                    echo "post condition executed: aborted ..."

                }

                regression {

                    echo "post condition executed: regression ..."

                }

            }

        }

        stage('Test'){

            steps {

                sh 'echo Test stage ...'

            }

            post {

                aborted {

                    echo "post condition executed: aborted ..."

                }

                failure {

                    echo "post condition executed: failure ..."

                }

                success {

                    echo "post condition executed: success ..."

                }

            }

        }

        stage('Deploy') {

            steps {

                sh 'echo Deploy stage ...'

            }

        }

    }

        post {

        unstable {

            echo "post condition executed: unstable ..."

        }

        unsuccessful {

            echo "post condition executed: unsuccessful ..."

        }

        cleanup {

            echo "post condition executed: cleanup ..."

        }

    }

  }

繼續閱讀