Script Pipeline的部分语法实现

Tool工具环境变量使用

  • 在shell中想使用jenkins中已配置好的tool,声明式Pipeline中的语法如下:
pipeline{
    ……
      tools {
          nodejs 'node-v16.15.1'
      }
    ……
}

但是在脚本式中,引用不到tools的环境,所以通过下面的方式解决:

withEnv(["PATH+NODEHOME=${tool 'node-v16.15.1'}/bin"]) {
    sh "tyarn"
}

Post{}的实现

  • 在脚本式中并不能直接实现声明式中post{},所以通过下面的方式解决:
node("nodelable"){
  try{
    stage(){
      ……
    }
  } catch (e){
      echo "failed"
      throw e
  } finally {
      // 执行post的东西
  }
}

GenericTrigger

pipeline放入scm中,需要首先手动执行一下build,jenkins会将trigger的配置写入job配置中

  • 声明式GenericTrigger
pipeline{
    triggers {
        GenericTrigger(
            genericVariables: [
            [key: 'MODIFIED', value: '$.commits[*].modified', expressionType: 'JSONPath'],
            [key: 'ADDED', value: '$.commits[*].added', expressionType: 'JSONPath'],
            [key: 'REMOVED', value: '$.commits[*].removed', expressionType: 'JSONPath'],
            [key: 'REF', value: '$.ref', expressionType: 'JSONPath', regexpFilter: 'refs/heads/'],
            [key: 'GIT_USER', value: '$.user_name', expressionType: 'JSONPath']
            ],

            causeString: 'Triggered on $GIT_USER',

            token: env.JOB_NAME,

            printContributedVariables: false,
            printPostContent: false,

            silentResponse: false,

            regexpFilterText: '$MODIFIED$ADDED$REMOVED',
            regexpFilterExpression: '"([a-zA-Z0-9]+\\-?)+\\/'
        )
    }
}
  • 脚本式pipelineTriggers
node("lable"){
  properties([
    pipelineTriggers([
      [$class: 'GenericTrigger',
        genericVariables: [
          [key: 'MODIFIED', value: '$.commits[*].modified', expressionType: 'JSONPath'],
          [key: 'ADDED', value: '$.commits[*].added', expressionType: 'JSONPath'],
          [key: 'REMOVED', value: '$.commits[*].removed', expressionType: 'JSONPath'],
          [key: 'REF', value: '$.ref', expressionType: 'JSONPath', regexpFilter: 'refs/heads/'],
          [key: 'GIT_USER', value: '$.user_name', expressionType: 'JSONPath']
        ],

        causeString: 'Triggered on $GIT_USER',

        token: env.JOB_NAME,

        printContributedVariables: false,
        printPostContent: false,

        silentResponse: false,

        regexpFilterText: '$MODIFIED$ADDED$REMOVED',
        regexpFilterExpression: '"([a-zA-Z0-9]+\\-?)+\\/'
      ]
    ])
 ])
}

声明式管道不能循环调用 library pipeline

  • 声明式管道在scripts里不能执行以下script(内容是错误的):
    only one pipeline { … } block can actually be executed per run
pipeline{
    stage("Build And Push Images"){
      steps{
        script{
          CHANGE_MODULES.each {
              switch (it){
                  case "h5" :
                      webbuild envi: "${REF}", buildnumber: currentBuild.getNumber(), proname: "h5"
                  case "scm" :
                      webbuild envi: "${REF}", buildnumber: currentBuild.getNumber(), proname: "scm"
                  case "xlbr" :
                      webbuild envi: "${REF}", buildnumber: currentBuild.getNumber(), proname: "xlbr"
                  default:
                      println("尽快添加当前模块:" + it)
              }
           }
        }
      }
    }
}
  • 将job pipeline及library pipeline替换为脚本式即可:
node("lable"){
  stage("A"){
    ……
    CHANGE_MODULES.each {
      println("执行" + it + "的构建")
      webbuildscript envi: "${REF}", buildnumber: currentBuild.getNumber(), proname: it, codepath: "${WORKSPACE}/webcode"
    }
    ……
  }
}

kubernetesDeploy获取环境变量替换

stage{
  withEnv(["NAMESPACE=com-${config.envi}","TAG=${config.envi}${config.buildnumber}","PROJECT_CENG=web","PROJECT_NAME=wxmp-${config.proname}","PROJECT_PORT=80"]){
    echo 'Deploy to Kubernetes'
    kubernetesDeploy configs: "weixin-mp.yaml", kubeConfig: [path: ''], kubeconfigId: 'com-cluster', secretName: '', ssh: [sshCredentialsId: '*', sshServer: ''], textCredentials: [certificateAuthorityData: '', clientCertificateData: '', clientKeyData: '', serverUrl: 'https://']
   }
}

你可能感兴趣的:(Script Pipeline的部分语法实现)