1 if控制语句

使用一个简单的If控制语句

  1. pipeline {
  2. agent any
  3. stages {
  4. stage('flow control') {
  5. steps {
  6. script {
  7. if ( == ) {
  8. println "pass"
  9. }else {
  10. println "failed"
  11. }
  12. }
  13. }
  14. }
  15. }
  16. }

构建结果

控制台输出

  1. Started by user darren ning
  2. Running in Durability level: MAX_SURVIVABILITY
  3. [Pipeline] Start of Pipeline
  4. [Pipeline] node
  5. Running on Jenkins in /root/.jenkins/workspace/jenkins_pipeline_demo
  6. [Pipeline] {
  7. [Pipeline] stage
  8. [Pipeline] { (flow control)
  9. [Pipeline] script
  10. [Pipeline] {
  11. [Pipeline] echo
  12. pass
  13. [Pipeline] }
  14. [Pipeline] // script
  15. [Pipeline] }
  16. [Pipeline] // stage
  17. [Pipeline] }
  18. [Pipeline] // node
  19. [Pipeline] End of Pipeline
  20. Finished: SUCCESS

2 多个stage嵌套的语法

  1. pipeline {
  2. agent any
  3. stages {
  4. stage('Non-Parallel Stage') {
  5. steps {
  6. echo 'This stage will be executed first.'
  7. }
  8. }
  9. stage('Parallel Stage') {
  10. failFast true
  11. parallel {
  12. stage('并行一') {
  13. steps {
  14. echo "并行一"
  15. }
  16. }
  17. stage('并行二') {
  18. steps {
  19. echo "并行二"
  20. }
  21. }
  22. stage('并行三') {
  23. stages {
  24. stage('Nested 1') {
  25. steps {
  26. echo "In stage Nested 1 within Branch C"
  27. }
  28. }
  29. stage('Nested 2') {
  30. steps {
  31. echo "In stage Nested 2 within Branch C"
  32. }
  33. }
  34. }
  35. }
  36. }
  37. }
  38. }
  39. }

执行

控制台输出

  1. Started by user darren ning
  2. Obtained when.jenkinsfile from git http://192.168.132.132/root/pipeline-parameter-test.git
  3. Running in Durability level: MAX_SURVIVABILITY
  4. [Pipeline] Start of Pipeline
  5. [Pipeline] node
  6. Running on Jenkins in /root/.jenkins/workspace/pipeline_parameter_project
  7. [Pipeline] {
  8. [Pipeline] stage
  9. [Pipeline] { (Declarative: Checkout SCM)
  10. [Pipeline] checkout
  11. No credentials specified
  12. > git rev-parse --is-inside-work-tree # timeout=
  13. Fetching changes from the remote Git repository
  14. > git config remote.origin.url http://192.168.132.132/root/pipeline-parameter-test.git # timeout=10
  15. Fetching upstream changes from http://192.168.132.132/root/pipeline-parameter-test.git
  16. > git --version # timeout=
  17. > git fetch --tags --progress http://192.168.132.132/root/pipeline-parameter-test.git +refs/heads/*:refs/remotes/origin/*
  18. > git rev-parse refs/remotes/origin/master^{commit} # timeout=
  19. > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=
  20. Checking out Revision 839c98003111f56e628fa806d80f0e8f2a97349b (refs/remotes/origin/master)
  21. > git config core.sparsecheckout # timeout=
  22. > git checkout -f 839c98003111f56e628fa806d80f0e8f2a97349b
  23. Commit message: "Update when.jenkinsfile"
  24. > git rev-list --no-walk 839c98003111f56e628fa806d80f0e8f2a97349b # timeout=
  25. [Pipeline] }
  26. [Pipeline] // stage
  27. [Pipeline] withEnv
  28. [Pipeline] {
  29. [Pipeline] stage
  30. [Pipeline] { (Non-Parallel Stage)
  31. [Pipeline] echo
  32. This stage will be executed first.
  33. [Pipeline] }
  34. [Pipeline] // stage
  35. [Pipeline] stage
  36. [Pipeline] { (Parallel Stage)
  37. [Pipeline] parallel
  38. [Pipeline] { (Branch: 并行一)
  39. [Pipeline] { (Branch: 并行二)
  40. [Pipeline] { (Branch: 并行三)
  41. [Pipeline] stage
  42. [Pipeline] { (并行一)
  43. [Pipeline] stage
  44. [Pipeline] { (并行二)
  45. [Pipeline] stage
  46. [Pipeline] { (并行三)
  47. [Pipeline] stage
  48. [Pipeline] { (Nested )
  49. [Pipeline] echo
  50. 并行一
  51. [Pipeline] }
  52. [Pipeline] echo
  53. 并行二
  54. [Pipeline] }
  55. [Pipeline] // stage
  56. [Pipeline] // stage
  57. [Pipeline] }
  58. [Pipeline] }
  59. [Pipeline] echo
  60. In stage Nested within Branch C
  61. [Pipeline] }
  62. [Pipeline] // stage
  63. [Pipeline] stage
  64. [Pipeline] { (Nested )
  65. [Pipeline] echo
  66. In stage Nested within Branch C
  67. [Pipeline] }
  68. [Pipeline] // stage
  69. [Pipeline] }
  70. [Pipeline] // stage
  71. [Pipeline] }
  72. [Pipeline] // parallel
  73. [Pipeline] }
  74. [Pipeline] // stage
  75. [Pipeline] }
  76. [Pipeline] // withEnv
  77. [Pipeline] }
  78. [Pipeline] // node
  79. [Pipeline] End of Pipeline
  80. Finished: SUCCESS

3 case语句

pipeline脚本

  1. pipeline {
  2. agent any
  3. stages {
  4. stage('Case lab') {
  5. steps {
  6. echo 'This stage will be executed first.'
  7. script{
  8. switch("$contraller"){
  9. case "Job1":
  10. println "This is Job1"
  11. break
  12. case "Job2":
  13. println "This is Job2"
  14. break
  15. case "Job3":
  16. println "This is Job3"
  17. break
  18. default:
  19. echo "############ wrong Job name ############"
  20. break
  21. }
  22. }
  23. }
  24.  
  25. }
  26. }
  27. }

在job配置几个参数

选择Job1构建

控制台输出

  1. Started by user darren ning
  2. Obtained when.jenkinsfile from git http://192.168.132.132/root/pipeline-parameter-test.git
  3. Running in Durability level: MAX_SURVIVABILITY
  4. [Pipeline] Start of Pipeline
  5. [Pipeline] node
  6. Running on Jenkins in /root/.jenkins/workspace/pipeline_parameter_project
  7. [Pipeline] {
  8. [Pipeline] stage
  9. [Pipeline] { (Declarative: Checkout SCM)
  10. [Pipeline] checkout
  11. No credentials specified
  12. > git rev-parse --is-inside-work-tree # timeout=
  13. Fetching changes from the remote Git repository
  14. > git config remote.origin.url http://192.168.132.132/root/pipeline-parameter-test.git # timeout=10
  15. Fetching upstream changes from http://192.168.132.132/root/pipeline-parameter-test.git
  16. > git --version # timeout=
  17. > git fetch --tags --progress http://192.168.132.132/root/pipeline-parameter-test.git +refs/heads/*:refs/remotes/origin/*
  18. > git rev-parse refs/remotes/origin/master^{commit} # timeout=
  19. > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=
  20. Checking out Revision ad52c819b4e74df5566cc767b8d580ccea363470 (refs/remotes/origin/master)
  21. > git config core.sparsecheckout # timeout=
  22. > git checkout -f ad52c819b4e74df5566cc767b8d580ccea363470
  23. Commit message: "Update when.jenkinsfile"
  24. > git rev-list --no-walk 839c98003111f56e628fa806d80f0e8f2a97349b # timeout=
  25. [Pipeline] }
  26. [Pipeline] // stage
  27. [Pipeline] withEnv
  28. [Pipeline] {
  29. [Pipeline] stage
  30. [Pipeline] { (Case lab)
  31. [Pipeline] echo
  32. This stage will be executed first.
  33. [Pipeline] script
  34. [Pipeline] {
  35. [Pipeline] echo
  36. This is Job1
  37. [Pipeline] }
  38. [Pipeline] // script
  39. [Pipeline] }
  40. [Pipeline] // stage
  41. [Pipeline] }
  42. [Pipeline] // withEnv
  43. [Pipeline] }
  44. [Pipeline] // node
  45. [Pipeline] End of Pipeline
  46. Finished: SUCCESS

选择Job3

控制台输出

  1. Started by user darren ning
  2. Obtained when.jenkinsfile from git http://192.168.132.132/root/pipeline-parameter-test.git
  3. Running in Durability level: MAX_SURVIVABILITY
  4. [Pipeline] Start of Pipeline
  5. [Pipeline] node
  6. Running on Jenkins in /root/.jenkins/workspace/pipeline_parameter_project
  7. [Pipeline] {
  8. [Pipeline] stage
  9. [Pipeline] { (Declarative: Checkout SCM)
  10. [Pipeline] checkout
  11. No credentials specified
  12. > git rev-parse --is-inside-work-tree # timeout=
  13. Fetching changes from the remote Git repository
  14. > git config remote.origin.url http://192.168.132.132/root/pipeline-parameter-test.git # timeout=10
  15. Fetching upstream changes from http://192.168.132.132/root/pipeline-parameter-test.git
  16. > git --version # timeout=
  17. > git fetch --tags --progress http://192.168.132.132/root/pipeline-parameter-test.git +refs/heads/*:refs/remotes/origin/*
  18. > git rev-parse refs/remotes/origin/master^{commit} # timeout=
  19. > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=
  20. Checking out Revision ad52c819b4e74df5566cc767b8d580ccea363470 (refs/remotes/origin/master)
  21. > git config core.sparsecheckout # timeout=
  22. > git checkout -f ad52c819b4e74df5566cc767b8d580ccea363470
  23. Commit message: "Update when.jenkinsfile"
  24. > git rev-list --no-walk ad52c819b4e74df5566cc767b8d580ccea363470 # timeout=
  25. [Pipeline] }
  26. [Pipeline] // stage
  27. [Pipeline] withEnv
  28. [Pipeline] {
  29. [Pipeline] stage
  30. [Pipeline] { (Case lab)
  31. [Pipeline] echo
  32. This stage will be executed first.
  33. [Pipeline] script
  34. [Pipeline] {
  35. [Pipeline] echo
  36. This is Job3
  37. [Pipeline] }
  38. [Pipeline] // script
  39. [Pipeline] }
  40. [Pipeline] // stage
  41. [Pipeline] }
  42. [Pipeline] // withEnv
  43. [Pipeline] }
  44. [Pipeline] // node
  45. [Pipeline] End of Pipeline
  46. Finished: SUCCESS

选择Job4

在pipeline的脚本里,并没有Job4的选项,看一下构建的结果

  1. Started by user darren ning
  2. Obtained when.jenkinsfile from git http://192.168.132.132/root/pipeline-parameter-test.git
  3. Running in Durability level: MAX_SURVIVABILITY
  4. [Pipeline] Start of Pipeline
  5. [Pipeline] node
  6. Running on Jenkins in /root/.jenkins/workspace/pipeline_parameter_project
  7. [Pipeline] {
  8. [Pipeline] stage
  9. [Pipeline] { (Declarative: Checkout SCM)
  10. [Pipeline] checkout
  11. No credentials specified
  12. > git rev-parse --is-inside-work-tree # timeout=
  13. Fetching changes from the remote Git repository
  14. > git config remote.origin.url http://192.168.132.132/root/pipeline-parameter-test.git # timeout=10
  15. Fetching upstream changes from http://192.168.132.132/root/pipeline-parameter-test.git
  16. > git --version # timeout=
  17. > git fetch --tags --progress http://192.168.132.132/root/pipeline-parameter-test.git +refs/heads/*:refs/remotes/origin/*
  18. > git rev-parse refs/remotes/origin/master^{commit} # timeout=
  19. > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=
  20. Checking out Revision ad52c819b4e74df5566cc767b8d580ccea363470 (refs/remotes/origin/master)
  21. > git config core.sparsecheckout # timeout=
  22. > git checkout -f ad52c819b4e74df5566cc767b8d580ccea363470
  23. Commit message: "Update when.jenkinsfile"
  24. > git rev-list --no-walk ad52c819b4e74df5566cc767b8d580ccea363470 # timeout=
  25. [Pipeline] }
  26. [Pipeline] // stage
  27. [Pipeline] withEnv
  28. [Pipeline] {
  29. [Pipeline] stage
  30. [Pipeline] { (Case lab)
  31. [Pipeline] echo
  32. This stage will be executed first.
  33. [Pipeline] script
  34. [Pipeline] {
  35. [Pipeline] echo
  36. ############ wrong Job name ############ #使用的dedault的输出
  37. [Pipeline] }
  38. [Pipeline] // script
  39. [Pipeline] }
  40. [Pipeline] // stage
  41. [Pipeline] }
  42. [Pipeline] // withEnv
  43. [Pipeline] }
  44. [Pipeline] // node
  45. [Pipeline] End of Pipeline
  46. Finished: SUCCESS

上面就是嵌套语句以及使用if和case语句进行流程控制的几个实验

DEVOPS技术实践_21:Pipeline的嵌套以及流程控制的if和case语句的更多相关文章

  1. DEVOPS技术实践_22:根据参数传入条件控制执行不同stage

    前面学习了参数的传递和调用,下面研究一下根据参数作为条件执行不同的stage 使用叫when 和expression控制某一个stage的运行, 运行场景例如写了多个stage,这个pipeline脚 ...

  2. JAVA之旅(二)——if,switch,for,while,do while,语句嵌套,流程控制break , continue ,函数,重载的示例总结

    JAVA之旅(二)--if,switch,for,while,do while,语句嵌套,流程控制break , continue ,函数,重载的示例总结 JAVA的思想真的很重要,所以要专心的学-- ...

  3. DEVOPS技术实践_23:判断文件下载成功作为执行条件

    在实际生产中,我们经常会需要通过判断一个结果作为一个条件去执行另一个内容,比如判断一个文件是否存在,判官一个命令是否执行成功等等 现在我们选择其中一个场景进行实验,当某个目录下存在,则执行操作 1. ...

  4. DEVOPS技术实践_19:Pipeline的多参数json调用

    在上一篇学习了把参数写进Json文件,然后通过去Json文件,调用参数的方法 1. 三元运算符介绍 调用的方法是通过一个三元运算符实现的 gender = prop.GENDER? prop.GEND ...

  5. DEVOPS技术实践_06:sonar与Jenksin集成

    代码质量管理平台 一.checkout和打包功能 1.1 gitlab在新建一个文件 后续在写入内容 1.2 Jenkins新建一个任务 两个参数 1.3 流水线配置 copy仓库地址: http:/ ...

  6. DEVOPS技术实践_04:Jenkins参数化构建

    一.参数化构建 1.1 各个参数的信息 凭据参数存储一个用户的账号密码信息,等等,运用最多的是选项参数 1.2 使用选项参数 构建已经变成参数化构建 1.3 获取这个值,修改Jenkinsfile文件 ...

  7. DEVOPS技术实践_03:Jenkins自动构建

    一.提交代码自动构建 当开发人员在gitlab提交代码后,会自动触发jenkin构建 点击项目---->点击diy_maven-TEST----->点击配置--->构建触发器---- ...

  8. DEVOPS技术实践_20:串联多个job执行

    在jenkins可能会有战役中场景,就是在一个job执行完之后,把这个执行结果作为另一个job的执行条件 比如A执行完,如果A执行成功,则执行B,如果失败则执行C 1 前期准备 A任务 import ...

  9. DEVOPS技术实践_18:Jenkins的Pinpeline对于参数的使用

    因为最近使用Pipeline声明式语法构建项目,但是最近项目的参数设置较多,特地的来学习一下关于参数的调用和测试,主要式从一个大神那里学习的,结尾回贴上大神的博客链接 1 构建一个pipeline项目 ...

随机推荐

  1. AtCoder Grand Contest 019 B - Reverse and Compare【思维】

    AtCoder Grand Contest 019 B - Reverse and Compare 题意:给定字符串,可以选定任意i.j且i<=j(当然i==j时没啥卵用),然后翻转i到j的字符 ...

  2. [***]HZOJ 柱状图

    神仙题. 作者的正解: *logn).   算法三:对于100%的数据:  我们枚举屋顶位置再三分高度的做法,复杂度的瓶颈在于花费的计算.假设屋顶在i处,高度为hi,如果j<i,有hj-j=hi ...

  3. 20182019-acmicpc-asia-dhaka-regional F .Path Intersection 树链剖分

    直接进行树链剖分,每次对路径区间内的所有点值+1,线段树进行维护,然后查询线段树的最大值的个数!!! 查询线段树区间最大值个数,可以先维护区间和,在维护区间最值,如果区间和等于区间最值乘以区间长度,那 ...

  4. 虎牙在全球 DNS 秒级生效上的实践

    本文整理自虎牙中间件团队在 Nacos Meetup 的现场分享,阿里巴巴中间件受权发布. 这次分享的是全球 DNS 秒级生效在虎牙的实践,以及由此产生的一些思考,整体上,分为以下5各部分: 背景介绍 ...

  5. Tyvj 1864 [Poetize I]守卫者的挑战

    P1864 [Poetize I]守卫者的挑战时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜寻 ...

  6. 开源中国 2014 最受关注开源软件排行榜 TOP 50

    开源中国 2014 最受关注开源软件排行榜 TOP 50 开源中国 2014 年最受关注软件排行榜 TOP 50 正式出炉!2014 年结束了,我们来了解一下过去一年里开源中国最受欢迎的 50 款软件 ...

  7. gensim的word2vec如何得出词向量(python)

    首先需要具备gensim包,然后需要一个语料库用来训练,这里用到的是skip-gram或CBOW方法,具体细节可以去查查相关资料,这两种方法大致上就是把意思相近的词映射到词空间中相近的位置. 语料库t ...

  8. HDU 1071

    题意:就是求给你一个抛物线的三个点,第一个给定的点是抛物线的顶点,让你求直线p2p3与抛物线的定积分 思路:因为题目条件给了顶点,所以直接用抛物线的顶点式去求. 本弱弱数学太差.还得复习一下公式 #i ...

  9. Springboot 自定义多个404页面

    在Springboot中,可以通过修改配置.或者在static文件夹下添加error文件夹引入个性化的404模版.但是如果需要针对不同url地址规则,返回不同样式的404页面,则难以实现了.针对这个问 ...

  10. (二)C#编程基础复习——变量和常量

    今天要复习一下C#基础中的变量和常量,所谓变量,就是用来存储特定类型的数据,分为值类型和引类型,可以根据需要随时改变变量中所村存储的数据值,变量必须先声明,然后才能赋值:常量就是固定不变的值,常量的变 ...