管道由多个步骤组成,容许你构建、测试和部署应用程序,Jenkins管道容许你以简单的方式组成多个步骤,能够帮助你为任何类型的自动化过程建模。node
将“步骤”想象成执行单个操做的单个命令,当一个步骤成功时,它将进入下一步,当一个步骤没法正确执行时,管道将失败。shell
当管道中的全部步骤都已成功完成时,将认为管道已成功执行。segmentfault
在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的系统应使用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
“包装”步骤(如timeout
和retry
)可能包含其余步骤,包括timeout
或retry
。
咱们能够将这些步骤组合在一块儿,例如,若是咱们想要重试咱们的部署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' } }