一 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的更多相关文章

  1. jenkins pipeline语法

    目录 一.声明式 二.脚本式 基本 判断 异常处理 Steps node withEnv 一.声明式 声明式Pipeline必须包含在名为pipeline的语句块中,典型的声明式Pipeline语法如 ...

  2. Jenkins pipeline:pipeline 语法详解

    jenkins  pipeline 总体介绍 pipeline 是一套运行于jenkins上的工作流框架,将原本独立运行于单个或者多个节点的任务连接起来,实现单个任务难以完成的复杂流程编排与可视化. ...

  3. Jenkins pipeline 语法详解

    原文地址http://www.cnblogs.com/fengjian2016/p/8227532.html pipeline 是一套运行于jenkins上的工作流框架,将原本独立运行于单个或者多个节 ...

  4. Jenkins Pipeline 语法

    Pipeline语法 先讲Declarative Pipeline,所有声明式管道都必须包含在pipeline块中: 123 pipeline { /* insert Declarative Pipe ...

  5. 6.Jenkins进阶之流水线pipeline语法入门学习(1)

    目录一览: 0x00 前言简述 Pipeline 介绍 Pipeline 基础知识 Pipeline 扩展共享库 BlueOcean 介绍 0x01 Pipeline Syntax (0) Groov ...

  6. devops-2:Jenkins的使用及Pipeline语法讲解

    DevOps-Jenkins Jenkins简介 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件项目可以进行持续 ...

  7. 让LINQ中的查询语法使用自定义的查询方法

    使用LINQ时有两种查询语法:查询语法和方法语法 查询语法:一种类似 SQL 语法的查询方式 方法语法:通过扩展方法和Lambda表达式来创建查询 例如: List<, , , }; //查询语 ...

  8. 关于CUDA C 项目中“ error C2059: 语法错误:“<” ”问题的解决方法

    该问题的关键在于理解CUDA项目中C\C++文件需要由c++编译器进行编译,而CUDA C的源文件需要由CUDA的编译器nvcc.exe进行编译. 发生该语法错误的原因是cu文件被C++编译器所编译, ...

  9. Java基础语法 - 面向对象 - 类的主方法main方法

    主方法是类的入口点,它指定了程序从何处开始,提供对程序流向的控制.Java编译器通过主方法来执行程序. 主方法的语法如下: /* a.主方法是静态的,如果要直接在主方法中调用其它方法,则该方法必须也是 ...

随机推荐

  1. CDH6.3 Centos7

    按照官方文档安装即可 CentOS7 上搭建 CDH(6.3.0) 官方文档:https://docs.cloudera.com/documentation/enterprise/6/6.3/topi ...

  2. vagrant(二)配置文件vagrantfile详解 以及安装php、nginx、mysql

    上一篇文章完整的讲叙了如何安装一个vagrant的环境.这里主要说一说vagrant的配置文件Vagrantfile. 一 配置详解 在我们的开发目录下有一个文件Vagrantfile,里面包含有大量 ...

  3. JavaScript-黑科技

    单行写一个评级 var rate = 3; "★★★★★☆☆☆☆☆".slice(5 - rate, 10 - rate); 随机字符串 Math.random().toStrin ...

  4. C++ 穷举算法 鸡兔同笼

    #include "stdio.h" int qiongju(int head, int foot, int *chicken, int *rabbit) { int re, i, ...

  5. 【C++11新特性】 C++11智能指针之weak_ptr

    如题,我们今天要讲的是C++11引入的三种智能指针中的最后一个:weak_ptr.在学习weak_ptr之前最好对shared_ptr有所了解.如果你还不知道shared_ptr是何物,可以看看我的另 ...

  6. iis7反向代理

    很多站长通常在Linux系统下使用nginx作为前端server,通过反向代理间接访问其他webserver.那么如果用户安装的是Windows系统的话,又改如何实现反向代理的设置呢?搜索引擎大全 下 ...

  7. CF 778D Parquet Re-laying——构造

    题目:http://codeforces.com/problemset/problem/778/D 完全没思路……就看了题解. 很好地思路是考虑操作可逆,所以起始状态和最终状态都变到一个中转状态,即都 ...

  8. 前端每日实战:18# 视频演示如何用纯 CSS 创作 404 文字变形为 NON 文字的交互特效

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/ZoxjXm 可交互视频教程 此视频 ...

  9. (转)Docker network命令

    转:https://blog.csdn.net/gezhonglei2007/article/details/51627821 原文地址:https://docs.docker.com/engine/ ...

  10. mysql8.0 新特性,对json类型的常用操作

    mysql8 新特性-json数据类型操作 -- 根据key(可多个)获取value SELECT JSON_EXTRACT('{"id": 14, "name" ...