Jenkins(三)

2019-09-14  本文已影响0人  测试游记

欢迎关注我公众号呀~「测试游记」「zx94_11」

pipeline语法

pipeline语法可以分为

但是由于脚本化流水线需要学习Groovy,所以我放弃了。

虽然放弃了通篇学习一整门语言,但是为了在声明式流水线中使用简单的逻辑操作还是需要学习一点Groovy的基础内容。

基础Groovy

def x = "abc"
def y = 1
def func(String a, String b){
   return a + " " + b
}
// 调用
func b = "Lee", a = "Bruce"
def sayHello(String name = "humans"){
   print "hello ${name}"
}
sayHello()
def name = 'world'
print "hello ${name}" //输出:hello world
print 'hello ${name}' //输出:hello ${name}
def func = {print "hello world"} //定义闭包
func() //调用闭包

// 定义一个接收闭包参数的pipeline函数
def pipeline(a){
   a()
}
pipeline(func) // 调用pipeline
// 把func直接写入⬇️
pipeline({print "hello world"})
// 括号可以省略⬇️
pipeline{print "hello world"} 
def stage(String name, a){
   println name //输出字符串name
   a() //调用闭包函数a
}
// 正常使用stage函数⬇️
stage("stage name",{print "hello world"})
// Groovy提供的另一种使用stage函数的方法⬇️
stage("stage name"){
   print "hello world"
}

pipeline的组成

Jenkins pipeline其实就是基于Groovy语言实现的一种DSL(领域特定语言),用于描述整条流水线上如何进行的。

流水线的内容包括:执行编译,打包,测试,输出测试报告等。

pipeline {
   agent any
   stages {
      stage('build'){
         steps{
            echo "hello world"
         }
      }
   }
}

步骤

类似于echo,sh等步骤,Jenkins有很多插件可以通过一定的修改来当作步骤使用

适配Jenkins pipeline的插件列表:

https://github.com/jenkinsci/pipeline-plugin/blob/master/COMPATIBILITY.md

通过「流水线语法」可以可视化的进行插件步骤的编辑与生成,降低学习成本。

流水线语法自动生成1 流水线语法自动生成2

post部分

在pipeline执行之后会执行post部分

根据pipeline的完成状态,post可以分成不同的条件块

pipeline{
   agent any
   stages{
      stage('build'){
         steps{
            echo "build stage"
         }
         post{
            always{
               echo "stage post alwasy"
            }
         }
      }
   }
   post{
      changed{
         echo "pipeline post changed"
      }
      always{
         echo "pipeline post alwasy"
      }
      success{
         echo "pipeline post success"
      }
   }
}

支持的命令

没有例子的直接百度咯,仿照着写多测测就行了。

在声明式流水线中使用脚本

在一些不得不用代码的地方使用script就可以写Groovy代码了

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

修改Jenkinsfile并推送至GitHub。

运行
上一篇 下一篇

猜你喜欢

热点阅读