Jenkins 用戶文檔(運行多個步驟)

運行多個步驟

管道由多個步驟組成,容許你構建、測試和部署應用程序,Jenkins管道容許你以簡單的方式組成多個步驟,能夠幫助你爲任何類型的自動化過程建模。node

將「步驟」想象成執行單個操做的單個命令,當一個步驟成功時,它將進入下一步,當一個步驟沒法正確執行時,管道將失敗。shell

當管道中的全部步驟都已成功完成時,將認爲管道已成功執行。segmentfault

Linux、BSD和Mac OS

在Linux、BSD和Mac OS(類Unix)系統上,sh步驟用於在管道中執行shell命令。post

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'echo "Hello World"'
                sh '''
                    echo "Multiline shell steps works too"
                    ls -lah
                '''
            }
        }
    }
}

腳本管道(高級):測試

Jenkinsfile (Scripted Pipeline)
node {
    stage('Build') {
        sh 'echo "Hello World"'
        sh '''
            echo "Multiline shell steps works too"
            ls -lah
        '''
    }
}

Windows

基於Windows的系統應使用bat步驟來執行批處理命令。ui

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                bat 'set'
            }
        }
    }
}

腳本管道(高級):code

Jenkinsfile (Scripted Pipeline)
node {
    stage('Build') {
        bat 'set'
    }
}

超時、重試等等

有一些強大的步驟能夠「包裝」其餘步驟,這些步驟能夠輕鬆解決問題,例如重試(retry)步驟直到成功或退出(若是步驟花費太長時間(timeout))。ip

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any
    stages {
        stage('Deploy') {
            steps {
                retry(3) {
                    sh './flakey-deploy.sh'
                }

                timeout(time: 3, unit: 'MINUTES') {
                    sh './health-check.sh'
                }
            }
        }
    }
}

腳本管道(高級):部署

Jenkinsfile (Scripted Pipeline)
node {
    stage('Deploy') {
        retry(3) {
            sh './flakey-deploy.sh'
        }

        timeout(time: 3, unit: 'MINUTES') {
            sh './health-check.sh'
        }
    }
}

「Deploy」階段重試flakey-deploy.sh腳本3次,而後等待最多3分鐘對於執行health-check.sh腳本,若是運行情況檢查腳本在3分鐘內未完成,則管道將在「Deploy」階段標記爲已失敗。get

「包裝」步驟(如timeoutretry)可能包含其餘步驟,包括timeoutretry

咱們能夠將這些步驟組合在一塊兒,例如,若是咱們想要重試咱們的部署5次,但在階段失敗以前永遠不想花費超過3分鐘:

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any
    stages {
        stage('Deploy') {
            steps {
                timeout(time: 3, unit: 'MINUTES') {
                    retry(5) {
                        sh './flakey-deploy.sh'
                    }
                }
            }
        }
    }
}

腳本管道(高級):

Jenkinsfile (Scripted Pipeline)
node {
    stage('Deploy') {
        timeout(time: 3, unit: 'MINUTES') {
            retry(5) {
                sh './flakey-deploy.sh'
            }
        }
    }
}

完成

管道執行完成後,你可能須要運行清理步驟或根據管道的結果執行某些操做,這些操做能夠在post部分中執行。

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                sh 'echo "Fail!"; exit 1'
            }
        }
    }
    post {
        always {
            echo 'This will always run'
        }
        success {
            echo 'This will run only if successful'
        }
        failure {
            echo 'This will run only if failed'
        }
        unstable {
            echo 'This will run only if the run was marked as unstable'
        }
        changed {
            echo 'This will run only if the state of the Pipeline has changed'
            echo 'For example, if the Pipeline was previously failing but is now successful'
        }
    }
}

腳本管道(高級):

Jenkinsfile (Scripted Pipeline)
node {
    try {
        stage('Test') {
            sh 'echo "Fail!"; exit 1'
        }
        echo 'This will run only if successful'
    } catch (e) {
        echo 'This will run only if failed'

        // Since we're catching the exception in order to report on it,
        // we need to re-throw it, to ensure that the build is marked as failed
        throw e
    } finally {
        def currentResult = currentBuild.result ?: 'SUCCESS'
        if (currentResult == 'UNSTABLE') {
            echo 'This will run only if the run was marked as unstable'
        }

        def previousResult = currentBuild.previousBuild?.result
        if (previousResult != null && previousResult != currentResult) {
            echo 'This will run only if the state of the Pipeline has changed'
            echo 'For example, if the Pipeline was previously failing but is now successful'
        }

        echo 'This will always run'
    }
}

上一篇:建立你的第一個管道

下一篇:定義執行環境

相關文章
相關標籤/搜索