Jenkins高级篇之Pipeline方法篇-Pipeline Utility Steps-4-方法readYaml和writeYaml

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011541946/article/details/83928473

本篇继续来介绍Pipeline Utility Steps这个插件支持的特定的方法,前面介绍了读取JSON和读properties文件,这篇来介绍读写yaml类型文件。读取yaml的方法是readYaml,写yaml文件的方法是writeYaml,yaml配置类型文件在linux系统中还是很常见的。

1.方法readYaml

我在Eclipse项目中的/testdata目录下,提前做好了一个test.yaml的文件,并写入如下数据并保存。

name: 'Anthony'
age : 18
city: 'Beijing'
isMale: true

module方法中readYaml方法,我写了支持参数传入的是一个yaml文件路径和直接传入一个yaml格式的字符串内容。这个module里面完整的方法,请看replay脚本框或者github上代码。

def read_yaml_file(yaml_file) {
	def datas = ""
	if(yaml_file.toString().endsWith(".yml")){
		datas = readYaml file : yaml_file
		
	}else {
		datas = readYaml text : yaml_file
	}
	datas.each {
		println ( it.key + " = " + it.value )
	 }
}

看到没,上面我为了打印读取的yaml内容,再次使用了groovy语言中的闭包语法,直接遍历里面元素的key和value。

我的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 + "/pipeline/module/pipeline-demo-module.groovy"
				}
			}
		}
		stage("read yaml file") {
			steps{
				script{
					yaml_file = env.WORKSPACE + "/testdata/test.yml"
					model_test.read_yaml_file(yaml_file)
					println "=========================="
					yaml_string = """
                                        age: 18
                                        city: 'Shanghai'
                                        isMale: false
                                        name: 'Lucy'
                                        """
					model_test.read_yaml_file(yaml_string)
				}
			}
		}
	}
}


关于上面这个文档注释符里面的yaml格式内容,我要特意指出这里不能用tab来代替键盘空格键产生的空格,不然就会报错。

我的Jenkins测试job:http://65.49.216.200:8080/job/pipeline-project-demo/57/

或者http://65.49.216.200:8080/job/pipeline-project-demo/54/

2.方法writeYaml

继续介绍写yaml方法,下面介绍如何把一个字典类型数据写入到yaml文件中。

module方法

def write_to_yaml(map_data, yaml_path) {
	writeYaml file: yaml_path , data: map_data
}

pipeline stage test代码

import hudson.model.*;

println env.JOB_NAME
println env.BUILD_NUMBER

pipeline{
	
	agent any
	stages{
		stage("init") {
			steps{
				script{
					model_test = load env.WORKSPACE + "/pipeline/module/pipeline-demo-module.groovy"
				}
			}
		}
		stage("write into yaml file") {
			steps{
				script{
					def amap = [name: 'Anthony',
                                age : 18,
                                city: 'Beijing',
                                isMale: true
								]
					yaml_file = env.WORKSPACE + "/testdata/new.yml"
					model_test.write_to_yaml(amap, yaml_file)
					println "the contents of yaml file are: "
					model_test.read_yaml_file(yaml_file)
				}
			}
		}
	}
}


我的Jenkins 成功构建测试job:http://65.49.216.200:8080/job/pipeline-project-demo/59/console

看这个日志打印就说明了写入到yaml文件成功,或者你去自己jenkins构建的workspace下找到这个./testdata/new.yaml文件,验证里面的内容。

好了到这里,关于Pipeline Utility Steps这个插件的常用方法学习,介绍到这里。这个插件的官方网页地址是https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/

我已经介绍了这个插件里面一半以上方法,如下图红圈方法是我们前面介绍过的。

关于pipeline上不同插件支持不同方法,你可以打开这个页面查看https://jenkins.io/doc/pipeline/steps/。

我也就是介绍一些基础常用的插件以及方法,其他可能是项目需要,到时候自己再跑过去看文档学习就可以。接下来,我要介绍在安装pipeline组件的时候,自动安装一个组件里面的方法,Pipeline:Basic Steps,这个方法你可以到插件管理,已安装界面看到。

也就是说,接下来学习的Basic Steps插件里面的方法,最好你都要掌握。只有basic steps里面方法不够用的时候,你才要考虑其他插件的方法。到这里,确实,我写文章顺序有点问题,应该先介绍basic steps插件下的方法。但是,前面插件学习应该也不难,有了这个基础,学习basic steps下的方法应该也是很容易。

猜你喜欢

转载自blog.csdn.net/u011541946/article/details/83928473