Script Pipeline的部分语法实现

2022-06-21  本文已影响0人  InGramViking

Tool工具环境变量使用

pipeline{
    ……
      tools {
          nodejs 'node-v16.15.1'
      }
    ……
}

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

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

Post{}的实现

node("nodelable"){
  try{
    stage(){
      ……
    }
  } catch (e){
      echo "failed"
      throw e
  } finally {
      // 执行post的东西
  }
}

GenericTrigger

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

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]+\\-?)+\\/'
        )
    }
}
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

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)
              }
           }
        }
      }
    }
}
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://']
   }
}
上一篇下一篇

猜你喜欢

热点阅读