Jenkins pipeline vs Groovy DSL

Declarative Pipeline

  • agent
  • post In the top-level pipeline block and each stage block.
    • post-condition Conditions
      • always, changed, fixed, regression, aborted, failure, success, unstable, unsuccessful, and cleanup
  • Supported Credentials Type
    • Secret Text
    • Secret File
    • Username and password
    • SSH with Private Key
  • when
    • Inside a stage directive
    • Built-in Conditions
      • branch ANT style path glob match
      • buildingTag
      • changelog
      • changeset
      • changeRequest
      • environment
      • equals
      • expression
      • tag
      • not
      • allOf
      • anyOf
      • triggeredBy
        • Execute the stage when the current build has been triggered by the param given
          • when { triggeredBy 'SCMTrigger' }
          • when { triggeredBy 'TimerTrigger' }
          • when { triggeredBy 'BuildUpstreamCause' }
          • when { triggeredBy cause: "UserIdCause", detail: "vlinde" }
  • matrix
    • the matrix section must include an axes section and a stages section.
    • axes
      // One-axis with 3 cells
      matrix {
        axes {
            axis {
                name 'PLATFORM'
                values 'linux', 'mac', 'windows'
            }
        }
        // ...
      }
      
      // Two-axis with 12 cells (three by four)
      matrix {
        axes {
            axis {
                name 'PLATFORM'
                values 'linux', 'mac', 'windows'
            }
            axis {
                name 'BROWSER'
                values 'chrome', 'edge', 'firefox', 'safari'
            }
        }
        // ...
      }
      
      // Three-axis matrix with 24 cells (three by four by two)
      matrix {
        axes {
            axis {
                name 'PLATFORM'
                values 'linux', 'mac', 'windows'
            }
            axis {
                name 'BROWSER'
                values 'chrome', 'edge', 'firefox', 'safari'
            }
            axis {
                name 'ARCHITECTURE'
                values '32-bit', '64-bit'
            }
        }
        // ...
      }
      
    • stages
      • One-axis with 3 cells, each cell runs three stages - “build”, “test”, and “deploy”
        matrix {
            axes {
                axis {
                    name 'PLATFORM'
                    values 'linux', 'mac', 'windows'
                }
            }
            stages {
                stage('build') {
                    // ...
                }
                stage('test') {
                    // ...
                }
                stage('deploy') {
                    // ...
                }
            }
        }
        
      • Two-axis with 12 cells (three by four)
        matrix {
            axes {
                axis {
                    name 'PLATFORM'
                    values 'linux', 'mac', 'windows'
                }
                axis {
                    name 'BROWSER'
                    values 'chrome', 'edge', 'firefox', 'safari'
                }
            }
            stages {
                stage('build-and-test') {
                    // ...
                }
            }
        }
        
      • excludes (optional)
      • Three-axis matrix with 24 cells, exclude ‘32-bit, mac’ (4 cells excluded)
        matrix {
          axes {
              axis {
                  name 'PLATFORM'
                  values 'linux', 'mac', 'windows'
              }
              axis {
                  name 'BROWSER'
                  values 'chrome', 'edge', 'firefox', 'safari'
              }
              axis {
                  name 'ARCHITECTURE'
                  values '32-bit', '64-bit'
              }
          }
          excludes {
              exclude {
                  axis {
                      name 'PLATFORM'
                      values 'mac'
                  }
                  axis {
                      name 'ARCHITECTURE'
                      values '32-bit'
                  }
              }
          }
          // ...
        }
        
    • Matrix cell-level directives (optional)
      • agent,environment,input,opitons,post,tools,when

Jenkins core

  • archiveArtifacts
  • $class

Command chains for groovy DSL

pipeline 本质是一个多层嵌套的闭包,

  • using a command chain based DSL
    // equivalent to: turn(left).then(right)
    turn left then right
    
    // equivalent to: take(2.pills).of(chloroquinine).after(6.hours)
    take 2.pills of chloroquinine after 6.hours
    
    // equivalent to: paint(wall).with(red, green).and(yellow)
    paint wall with red, green and yellow
    
    // with named parameters too
    // equivalent to: check(that: margarita).tastes(good)
    check that: margarita tastes good
    
    // with closures as parameters
    // equivalent to: given({}).when({}).then({})
    given { } when { } then { }
    
    // equivalent to: select(all).unique().from(names)
    select all unique() from names
    
    // equivalent to: take(3).cookies
    // and also this: take(3).getCookies()
    take 3 cookies
    
  • illustrate creating such a DSL
    show = { println it }
    square_root = { Math.sqrt(it) }
    
    def please(action) {
      [the: { what ->
        [of: { n -> action(what(n)) }]
      }]
    }
    
    // equivalent to: please(show).the(square_root).of(100)
    please show the square_root of 100
    // ==> 10.0
    
  • it 是 groovy 闭包中特殊参数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值