Jenkins2 学习系列15 -- 声明式Pipeline补充

2019-07-22  本文已影响0人  飞凡的陀螺

如果需要在pipeline中进行逻辑判断或者写Groovy脚本代码,需要写在script步骤中,如下:

pipeline {
  agent any
  stages {
     stage('example')  {
        steps {
           script {
               def browsers = ['chrome', 'firefox']
                for (int i = 0; i < browsers .size(); i ++) {
                    echo "testing ${browsers[i]}"
                }
           }
        }
     }
   }
}

绝大多数时候没有必要写script,建议都提取到不同的stage或使用共享库
pipeline内置了一些step
文件相关的有deleteDir, dir, fileExists, isUnix, pwd, writeFile,readFile

steps{
   script {
       println pwd()
       json_file = "${env.WORKSPACE}/testdata/test_json.json"
       if (fileExists(json_file) == true && isUnix()) {
         echo("json file is exists")
       } else {
         error("here haven't find json file")
       }

       dir ("/var/logs") {
          deleteDir()
       }
}

命令相关的有

returnStatus 和 returnStdout 参数一般不会同时使用,因为返回值只能有一个,如果同时存在则只有returnStatus生效

其他:

  withEnv(['MYTOOL_HOME=/usr/local/mytool']) {
    sh '$MYTOOL_HOME/bin/start'
  }
timeout(50) {
   waitUnit {
      script {
          def r = sh script: 'curl http://example', returnStatus: true
          return (r == 0)
      }
   }
}
echo "hello"
sleep(120) // 休眠120秒
sleep(time: '2', unit: 'HOURS') //单位有  NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS
echo "hello again"

参考

https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/

上一篇下一篇

猜你喜欢

热点阅读