pipeline语法之environment,dir(),deleteDir()方法,readJSON,writeJSON
一 environment指令指定一系列键值对,这些对值将被定义为所有步骤的环境变量或阶段特定步骤
environment{…}, 大括号里面写一些键值对,也就是定义一些变量并赋值,这些变量就是环境变量。环境变量的作用范围,取决你environment{…}所写的位置,你可以写在顶层环境变量,让所有的stage下的step共享这些变量,也可以单独定义在某一个stage下,只能供这个stage去调用变量,其他的stage不能共享这些变量。一般来说,我们基本上上定义全局环境变量,如果是局部环境变量,我们直接用def关键字声明就可以,没必要放environment{…}里面。
//全局
pipeline {
agent any
environment {
unit_test = true
}
stages {
stage('Example') {
steps {
script{
if(unit_test == true){ println "变量为真 "
}
}}
}
}
}
二 dir ,deleteDir
dir()方法:就是改变当前的工作目录,在dir语句块里执行的其他路径或者相对路径
deleteDir()方法:默认递归删除WORKSPACE下的文件和文件夹,没有参数,使用这个方法会留下一个后遗症:
执行这个job的时候,你之前已经在这个工作目录下面,你再建一个目录就会报错:mkdir: 无法创建目录"testdata": 没有那个文件或目录,这是个很矛盾的报错
解决方法:使用cd重新切换到当前目录,再执行mkdir操作
举例如下:
pipeline{
agent any
stages{
stage("deleteDir") {
steps{
script{
println env.WORKSPACE
dir("${env.WORKSPACE}/testdata"){ //切换到当前工作目录下的testdata目录
sh "pwd" //sh命令可以 sh "command..." 也可以 sh("command....")
}
sh("ls -al ${env.WORKSPACE}")
deleteDir() // clean up current work directory //清空目录
sh("ls -al ${env.WORKSPACE}")
}
}
}
}
}
执行结果
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/pipe-example
[Pipeline] {
[Pipeline] stage
[Pipeline] { (deleteDir)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
/root/.jenkins/workspace/pipe-example #println env.WORKSPACE
[Pipeline] dir Running in /root/.jenkins/workspace/pipe-example/testdata
[Pipeline] { [Pipeline] sh + pwd /root/.jenkins/workspace/pipe-example/testdata [Pipeline] }
[Pipeline] // dir
[Pipeline] sh
+ ls -al /root/.jenkins/workspace/pipe-example
总用量 4
drwxr-xr-x 4 root root 42 9月 4 11:33 .
drwxr-xr-x 28 root root 4096 9月 4 11:24 ..
drwxr-xr-x 2 root root 22 9月 4 11:28 testdata
drwxr-xr-x 2 root root 6 9月 4 11:33 testdata@tmp
[Pipeline] deleteDir
[Pipeline] sh
+ ls -al /root/.jenkins/workspace/pipe-example
总用量 4
drwxr-xr-x 2 root root 6 9月 4 11:33 .
drwxr-xr-x 28 root root 4096 9月 4 11:33 ..
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
三 readJSON,writeJSON
要使用这两个方法,必须安装插件Pipeline Utility Steps,否则报错:java.lang.NoSuchMethodError: No such DSL method 'readJSON'
我们先来使用readJSON
1 先建一个json文件,路径在工作目录的testdata/test_json.json
{
"NAME":"xinxin",
"AGE":30,
"CITY":"Beijing",
"GENDER":"male"
}
2 重写方法,有两种,路径放在工作目录下面的module/pipeline-demo-module.groovy
import hudson.model.*;
def find_files(filetype) {
def files = findFiles(glob:filetype)
for (file in files) {
println file.name
}
}
def read_json_file(file_path) { //读取文件的情况
def propMap = readJSON file : file_path
propMap.each {
println ( it.key + " = " + it.value )
}
}
def read_json_file2(json_string) { //读取字符串的情况
def propMap = readJSON text : json_string
propMap.each {
println ( it.key + " = " + it.value )
}
}
return this;
最后设置pipeline stage文件内容
import hudson.model.*; println env.JOB_NAME
println env.BUILD_NUMBER pipeline{ agent any
stages{
stage("init") {
steps{
script{
model_test = load env.WORKSPACE + "/module/pipeline-demo-module.groovy" //采用路径的拼接来读取
}
}
}
stage("Parse json") { //分别调用两种方式读取
steps{
script{
json_file = env.WORKSPACE + "/testdata/test_json.json"
model_test.read_json_file(json_file)
println "================================"
json_string = '{"NAME":"xinxin","AGE":30,"CITY":"Beijing","GENDER":"male"}'
model_test.read_json_file2(json_string)
}
}
}
}
}
结果:
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] echo
pipe-example
[Pipeline] echo
58
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/pipe-example
[Pipeline] {
[Pipeline] stage
[Pipeline] { (init)
[Pipeline] script
[Pipeline] {
[Pipeline] load
[Pipeline] { (/root/.jenkins/workspace/pipe-example/module/pipeline-demo-module.groovy)
[Pipeline] }
[Pipeline] // load
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Parse json)
[Pipeline] script
[Pipeline] {
[Pipeline] readJSON
[Pipeline] echo
NAME = xinxin
[Pipeline] echo
AGE = 30
[Pipeline] echo
CITY = Beijing
[Pipeline] echo
GENDER = male
[Pipeline] echo
================================
[Pipeline] readJSON
[Pipeline] echo
NAME = xinxin
[Pipeline] echo
AGE = 30
[Pipeline] echo
CITY = Beijing
[Pipeline] echo
GENDER = male
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
下面看看writeJSON
看名称就知道是是将json格式的文件或者字符串写入文件 wirteJSON方法有两个必须的字段,分别是json和file, json是一个json对象,是你需要把这个json写入到文件的内容, 第二个file字段是一个文件的路径,这个文件肯定在jenkins的workspace范围之内。 第三个字段pretty是可选,也就是可以选择把json对象插入到一个指定的位置。
1 修改module/pipeline-demo-module.groovy
import hudson.model.*;
import groovy.json.*; //这个省略,在使用JsonOutput类的时候必须导入 def find_files(filetype) { def files = findFiles(glob:filetype)
for (file in files) {
println file.name
}
} def read_json_file(file_path) {
def propMap = readJSON file : file_path
propMap.each {
println ( it.key + " = " + it.value )
}
} def read_json_file2(json_string) {
def propMap = readJSON text : json_string
propMap.each {
println ( it.key + " = " + it.value )
}
}
def write_json_to_file(input_json, tofile_path) { //增加的部分
def input = ''
if(input_json.toString().endsWith(".json")) {
input = readJSON file : input_json
}else {
//def output = new JsonOutput()
//def new_json_object = output.toJson(input_json)
//input = new_json_object
input = readJSON text : input_json
}
writeJSON file: tofile_path, json: input
} return this;
2 修改pipeline stage文件内容
println env.JOB_NAME
println env.BUILD_NUMBER pipeline{ agent any
stages{
stage("init") {
steps{
script{
model_test = load env.WORKSPACE + "/module/pipeline-demo-module.groovy"
}
}
}
stage("write json") {
steps{
script{
json_file = env.WORKSPACE + "/testdata/test_json.json"
tojson_file = env.WORKSPACE + "/testdata/new_json.json" //new_json.json文件可以事先不存在,它会自动创建
model_test.write_json_to_file(json_file,tojson_file)
println "================================"
json_string = '{"NAME":"xinxin","AGE":30,"CITY":"Beijing","GENDER":"male"}'
tojson_file = env.WORKSPACE + "/testdata/new_json1.json"
model_test.write_json_to_file(json_string,tojson_file)
}
}
}
}
}
执行结果
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] echo
pipe-example
[Pipeline] echo
65
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/pipe-example
[Pipeline] {
[Pipeline] stage
[Pipeline] { (init)
[Pipeline] script
[Pipeline] {
[Pipeline] load
[Pipeline] { (/root/.jenkins/workspace/pipe-example/module/pipeline-demo-module.groovy)
[Pipeline] }
[Pipeline] // load
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (write json)
[Pipeline] script
[Pipeline] {
[Pipeline] readJSON
[Pipeline] writeJSON
[Pipeline] echo
================================
[Pipeline] readJSON
[Pipeline] writeJSON
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
查看系统上面的文件
[root@localhost testdata]# ls
new_json1.json new_json.json test_json.json
[root@localhost testdata]# cat new_json.json
{"NAME":"xinxin","AGE":30,"CITY":"Beijing","GENDER":"male"}[root@localhost testdata]#
[root@localhost testdata]# cat new_json1.json
{"NAME":"xinxin","AGE":30,"CITY":"Beijing","GENDER":"male"}[root@localhost testdata]#
pipeline语法之environment,dir(),deleteDir()方法,readJSON,writeJSON的更多相关文章
- jenkins pipeline语法
目录 一.声明式 二.脚本式 基本 判断 异常处理 Steps node withEnv 一.声明式 声明式Pipeline必须包含在名为pipeline的语句块中,典型的声明式Pipeline语法如 ...
- Jenkins pipeline:pipeline 语法详解
jenkins pipeline 总体介绍 pipeline 是一套运行于jenkins上的工作流框架,将原本独立运行于单个或者多个节点的任务连接起来,实现单个任务难以完成的复杂流程编排与可视化. ...
- Jenkins pipeline 语法详解
原文地址http://www.cnblogs.com/fengjian2016/p/8227532.html pipeline 是一套运行于jenkins上的工作流框架,将原本独立运行于单个或者多个节 ...
- Jenkins Pipeline 语法
Pipeline语法 先讲Declarative Pipeline,所有声明式管道都必须包含在pipeline块中: 123 pipeline { /* insert Declarative Pipe ...
- 6.Jenkins进阶之流水线pipeline语法入门学习(1)
目录一览: 0x00 前言简述 Pipeline 介绍 Pipeline 基础知识 Pipeline 扩展共享库 BlueOcean 介绍 0x01 Pipeline Syntax (0) Groov ...
- devops-2:Jenkins的使用及Pipeline语法讲解
DevOps-Jenkins Jenkins简介 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件项目可以进行持续 ...
- 让LINQ中的查询语法使用自定义的查询方法
使用LINQ时有两种查询语法:查询语法和方法语法 查询语法:一种类似 SQL 语法的查询方式 方法语法:通过扩展方法和Lambda表达式来创建查询 例如: List<, , , }; //查询语 ...
- 关于CUDA C 项目中“ error C2059: 语法错误:“<” ”问题的解决方法
该问题的关键在于理解CUDA项目中C\C++文件需要由c++编译器进行编译,而CUDA C的源文件需要由CUDA的编译器nvcc.exe进行编译. 发生该语法错误的原因是cu文件被C++编译器所编译, ...
- Java基础语法 - 面向对象 - 类的主方法main方法
主方法是类的入口点,它指定了程序从何处开始,提供对程序流向的控制.Java编译器通过主方法来执行程序. 主方法的语法如下: /* a.主方法是静态的,如果要直接在主方法中调用其它方法,则该方法必须也是 ...
随机推荐
- 【Http2.0】Http2.0
序言 目前HTTP/2.0(简称h2)已经在广泛使用(截止2018年8月根据Alexa流行度排名的头部1千万网站中,h2占比约29%,https://w3techs.com/technologies/ ...
- jmeter+ant+jenkins搭建接口自动化测试环境
jmeter+ant+jenkins搭建接口自动化测试环境(基于win) 1.jmeter jmeter依赖java运行环境,所以需要提前下载jdk并配置好环境变量 官网下载(http://jmete ...
- sql判断中文、数字、英文
IF OBJECT_ID('DBO.GET_NUMBER2') IS NOT NULL DROP FUNCTION DBO.GET_NUMBER2 GO )) ) AS BEGIN BEGIN ,'' ...
- Buuctf | BUU LFI COURSE 1
跟着赵师傅学CTF,这里是我的学习记录 ?file=/flag ?file=/var/log/nginx/access.log :包含ngnix的日志记录 在user-agent里面插入 :bbbbb ...
- Java中static修饰类的问题
Java中static修饰类的问题 众所周知,Java中static关键字可以修饰方法与变量: 修饰变量的时候,这个变量属于类变量,可以直接通过类名.变量名来引用. 修饰方法的时候可以直接通过类名.方 ...
- No orientation specified, and the default is horizontal.异常处理(转)
参考:http://blog.csdn.net/sky_monkey/article/details/21466975 整的错误提示信息为: No orientation specified, and ...
- CentOS 如何将.deb 文件 转换.rpm, centos安装deb包
CentOS 如何将.deb 文件 转换.rpm [root@localhost tmp]#tar zxvf alien_8.88.tar.gz yum install alien [root@loc ...
- 关于tomcat中的三个端口的作用及其相关细节
[一]端口内容 tomcat的端口号相关信息: Tomcat admin port——管理端口,允许你远程配置tomcat HTTP——正常的http协议 AJP——Apache JServ Prot ...
- MongoDB简单认识
MongoDB 为何物 NoSQL 泛指非关系型数据库,该词是关系型数据库(即 SQL)的相对称呼.MongoDB 是非关系型数据库中较为人熟知的一种. 它拥有很多优秀特性,例如高性能.高可用.支持丰 ...
- 中介者模式(Mediator Pattern)
用于减少多个对象或类之间的通信复杂性. 此模式提供了一个中介类,它通常处理不同类之间的所有通信,并支持通过松散耦合来维护代码.中介者模式属于行为模式类别. 实现实例 在这里通过一个聊天室的示例来演示中 ...