- 执行多个步骤(step)
- Linux、BSD 和 Mac OS
- Windows
- 超时、重试和更多
- 完成时动作
执行多个步骤(step)
Pipelines 由多个步骤(step)组成,允许你构建、测试和部署应用。Jenkins Pipeline 允许您使用一种简单的方式组合多个步骤,以帮助您实现多种类型的自动化构建过程。
可以把“步骤(step)”看作一个执行单一动作的单一的命令。当一个步骤运行成功时继续运行下一个步骤。当任何一个步骤执行失败时,Pipeline 的执行结果也为失败。
当所有的步骤都执行完成并且为成功时,Pipeline 的执行结果为成功。
Linux、BSD 和 Mac OS
在 Linux、BSD 和 Mac OS(类 Unix ) 系统中的 shell 命令, 对应于 Pipeline 中的一个 sh
步骤(step)。
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'echo "Hello World"'
sh '''
echo "Multiline shell steps works too"
ls -lah
'''
}
}
}
}
Toggle Scripted Pipeline(Advanced)
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'
}
}
}
}
Toggle Scripted Pipeline(Advanced)
Jenkinsfile (Scripted Pipeline)
node {
stage('Build') {
bat 'set'
}
}
超时、重试和更多
Jenkins Pipeline 提供了很多的步骤(step),这些步骤可以相互组合嵌套,方便地解决像重复执行步骤直到成功(重试)和如果一个步骤执行花费的时间太长则退出(超时)等问题。
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'
}
}
}
}
}
Toggle Scripted Pipeline(Advanced)
Jenkinsfile (Scripted Pipeline)
node {
stage('Deploy') {
retry(3) {
sh './flakey-deploy.sh'
}
timeout(time: 3, unit: 'MINUTES') {
sh './health-check.sh'
}
}
}
“Deploy”阶段(stage)重复执行 flakey-deploy.sh
脚本3次,然后等待 health-check.sh
脚本最长执行3分钟。如果 health-check.sh
脚本在 3 分钟内没有完成,Pipeline 将会标记在“Deploy”阶段失败。
内嵌类型的步骤,例如 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'
}
}
}
}
}
}
Toggle Scripted Pipeline(Advanced)
Jenkinsfile (Scripted Pipeline)
node {
stage('Deploy') {
timeout(time: 3, unit: 'MINUTES') {
retry(5) {
sh './flakey-deploy.sh'
}
}
}
}
完成时动作
当 Pipeline 运行完成时,你可能需要做一些清理工作或者基于 Pipeline 的运行结果执行不同的操作,这些操作可以放在 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'
}
}
}
Toggle Scripted Pipeline(Advanced)
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'
}
}