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
    评论
Jenkins Pipeline是一种可将软件交付过程定义为代码的方式。它使用一种基于GroovyDSL(领域特定语言)来描述构建和部署过程。下面是一些Jenkins Pipeline的语法要点: 1. 定义Pipeline:在Jenkinsfile中使用`pipeline`关键字来定义Pipeline。可以选择Declarative Pipeline或Scripted Pipeline。 2. Declarative Pipeline:使用`pipeline`块包裹整个Pipeline,并使用`stages`块来定义各个阶段。每个阶段内可以包含多个步骤。 ```groovy pipeline { agent any stages { stage('Build') { steps { // 构建步骤 } } stage('Test') { steps { // 测试步骤 } } // 可以定义更多阶段 } } ``` 3. Scripted Pipeline:使用`node`或`stage`等关键字来定义节点或阶段,并在节点或阶段内编写Groovy脚本。 ```groovy node { // 节点级别的操作 stage('Build') { // 构建步骤 } stage('Test') { // 测试步骤 } // 可以定义更多阶段 } ``` 4. 步骤(Steps):在每个阶段或节点内,可以使用各种Jenkins提供的步骤来执行具体任务,例如构建、测试、部署等。步骤可以是Jenkins内置的或插件提供的。 ```groovy stage('Build') { steps { // 执行构建任务 sh 'mvn clean install' } } ``` 5. 环境变量:可以使用`environment`块来定义Pipeline的环境变量。 ```groovy pipeline { environment { MY_VAR = 'value' } stages { // 阶段定义 } } ``` 以上是Pipeline的基本语法要点,你可以根据实际需求来组织和扩展Pipeline。更详细的语法和示例可以参考Jenkins官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值