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 闭包中特殊参数

你可能感兴趣的:(jenkins)