执行多个步骤(step)

Pipelines 由多个步骤(step)组成,允许你构建、测试和部署应用。Jenkins Pipeline 允许您使用一种简单的方式组合多个步骤,以帮助您实现多种类型的自动化构建过程。

可以把“步骤(step)”看作一个执行单一动作的单一的命令。当一个步骤运行成功时继续运行下一个步骤。当任何一个步骤执行失败时,Pipeline 的执行结果也为失败。

当所有的步骤都执行完成并且为成功时,Pipeline 的执行结果为成功。

Linux、BSD 和 Mac OS

在 Linux、BSD 和 Mac OS(类 Unix ) 系统中的 shell 命令, 对应于 Pipeline 中的一个 sh 步骤(step)。

Jenkinsfile (Declarative Pipeline)

  1. pipeline {
  2. agent any
  3. stages {
  4. stage('Build') {
  5. steps {
  6. sh 'echo "Hello World"'
  7. sh '''
  8. echo "Multiline shell steps works too"
  9. ls -lah
  10. '''
  11. }
  12. }
  13. }
  14. }

Toggle Scripted Pipeline(Advanced)

Jenkinsfile (Scripted Pipeline)

  1. node {
  2. stage('Build') {
  3. sh 'echo "Hello World"'
  4. sh '''
  5. echo "Multiline shell steps works too"
  6. ls -lah
  7. '''
  8. }
  9. }

Windows

基于 Windows 的系统使用 bat 步骤表示执行批处理命令。

Jenkinsfile (Declarative Pipeline)

  1. pipeline {
  2. agent any
  3. stages {
  4. stage('Build') {
  5. steps {
  6. bat 'set'
  7. }
  8. }
  9. }
  10. }

Toggle Scripted Pipeline(Advanced)

Jenkinsfile (Scripted Pipeline)

  1. node {
  2. stage('Build') {
  3. bat 'set'
  4. }
  5. }

超时、重试和更多

Jenkins Pipeline 提供了很多的步骤(step),这些步骤可以相互组合嵌套,方便地解决像重复执行步骤直到成功(重试)和如果一个步骤执行花费的时间太长则退出(超时)等问题。

Jenkinsfile (Declarative Pipeline)

  1. pipeline {
  2. agent any
  3. stages {
  4. stage('Deploy') {
  5. steps {
  6. retry(3) {
  7. sh './flakey-deploy.sh'
  8. }
  9. timeout(time: 3, unit: 'MINUTES') {
  10. sh './health-check.sh'
  11. }
  12. }
  13. }
  14. }
  15. }

Toggle Scripted Pipeline(Advanced)

Jenkinsfile (Scripted Pipeline)

  1. node {
  2. stage('Deploy') {
  3. retry(3) {
  4. sh './flakey-deploy.sh'
  5. }
  6. timeout(time: 3, unit: 'MINUTES') {
  7. sh './health-check.sh'
  8. }
  9. }
  10. }

“Deploy”阶段(stage)重复执行 flakey-deploy.sh 脚本3次,然后等待 health-check.sh 脚本最长执行3分钟。如果 health-check.sh 脚本在 3 分钟内没有完成,Pipeline 将会标记在“Deploy”阶段失败。

内嵌类型的步骤,例如 timeoutretry 可以包含其他的步骤,包括 timeoutretry

我们也可以组合这些步骤。例如,如果我们想要重试部署任务 5 次,但是总共花费的时间不能超过 3 分钟。

Jenkinsfile (Declarative Pipeline)

  1. pipeline {
  2. agent any
  3. stages {
  4. stage('Deploy') {
  5. steps {
  6. timeout(time: 3, unit: 'MINUTES') {
  7. retry(5) {
  8. sh './flakey-deploy.sh'
  9. }
  10. }
  11. }
  12. }
  13. }
  14. }

Toggle Scripted Pipeline(Advanced)

Jenkinsfile (Scripted Pipeline)

  1. node {
  2. stage('Deploy') {
  3. timeout(time: 3, unit: 'MINUTES') {
  4. retry(5) {
  5. sh './flakey-deploy.sh'
  6. }
  7. }
  8. }
  9. }

完成时动作

当 Pipeline 运行完成时,你可能需要做一些清理工作或者基于 Pipeline 的运行结果执行不同的操作,这些操作可以放在 post 部分。

Jenkinsfile (Declarative Pipeline)

  1. pipeline {
  2. agent any
  3. stages {
  4. stage('Test') {
  5. steps {
  6. sh 'echo "Fail!"; exit 1'
  7. }
  8. }
  9. }
  10. post {
  11. always {
  12. echo 'This will always run'
  13. }
  14. success {
  15. echo 'This will run only if successful'
  16. }
  17. failure {
  18. echo 'This will run only if failed'
  19. }
  20. unstable {
  21. echo 'This will run only if the run was marked as unstable'
  22. }
  23. changed {
  24. echo 'This will run only if the state of the Pipeline has changed'
  25. echo 'For example, if the Pipeline was previously failing but is now successful'
  26. }
  27. }
  28. }

Toggle Scripted Pipeline(Advanced)

Jenkinsfile (Scripted Pipeline)

  1. node {
  2. try {
  3. stage('Test') {
  4. sh 'echo "Fail!"; exit 1'
  5. }
  6. echo 'This will run only if successful'
  7. } catch (e) {
  8. echo 'This will run only if failed'
  9. // Since we're catching the exception in order to report on it,
  10. // we need to re-throw it, to ensure that the build is marked as failed
  11. throw e
  12. } finally {
  13. def currentResult = currentBuild.result ?: 'SUCCESS'
  14. if (currentResult == 'UNSTABLE') {
  15. echo 'This will run only if the run was marked as unstable'
  16. }
  17. def previousResult = currentBuild.previousBuild?.result
  18. if (previousResult != null && previousResult != currentResult) {
  19. echo 'This will run only if the state of the Pipeline has changed'
  20. echo 'For example, if the Pipeline was previously failing but is now successful'
  21. }
  22. echo 'This will always run'
  23. }
  24. }