Jenkins 用户文档(运行多个步骤)

运行多个步骤

管道由多个步骤组成,允许你构建、测试和部署应用程序,Jenkins管道允许你以简单的方式组成多个步骤,可以帮助你为任何类型的自动化过程建模。

将“步骤”想象成执行单个操作的单个命令,当一个步骤成功时,它将进入下一步,当一个步骤无法正确执行时,管道将失败。

当管道中的所有步骤都已成功完成时,将认为管道已成功执行。

Linux、BSD和Mac OS

在Linux、BSD和Mac OS(类Unix)系统上,sh步骤用于在管道中执行shell命令。

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步骤来执行批处理命令。

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

脚本管道(高级):

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

超时、重试等等

有一些强大的步骤可以“包装”其他步骤,这些步骤可以轻松解决问题,例如重试(retry)步骤直到成功或退出(如果步骤花费太长时间(timeout))。

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”阶段标记为已失败。

“包装”步骤(如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'
    }
}

上一篇:创建你的第一个管道

下一篇:定义执行环境

相关推荐