Jenkins高级篇之Pipeline方法篇-Pipeline Utility Steps-3-方法readProperties

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

这篇继续来学习文件相关操作的方法,前面学习了JSON格式文件的读和写,这么介绍一个读取properties文件,这个方法特别是适合Java项目,一个Java项目很多配置文件可能是采用properties类型来写入键值对数据。

1.方法readProperties

就是从一个xxx.properties文件去读取内容出来,得到是一个map对象。下面我现在/testdata文件夹下创建一个test.properties文件,里面写入这些内容。

Name = Anthony
Aget = 18
City = Beijing
Agend = male

module 里面解析读取properties的方法如下。

def read_properties(properties_file) {
	 def props = readProperties interpolate: true, file: properties_file
	 props.each {
		println ( it.key + " = " + it.value )
	 }
}

pipeline 读取properties的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 properties") {
			steps{
				script{
					properties_file = env.WORKSPACE + "/testdata/test.properties"
					model_test.read_properties(properties_file)
					println "================================"
				}
			}
		}
	}
}


我的jenkins 测试job地址:http://65.49.216.200:8080/job/pipeline-project-demo/47/console

你通过日志可以看到具体测试结果,代码可以看replay下脚本,或者github项目。

2.方法touch

这个touch就是linux下的touch的作用,创建一个文件(如果不存在),并产生时间戳。

下面来举例,运行一个stage来测试下。

stage("touch file") {
			steps{
				script{
					touch_file = env.WORKSPACE + "/testdata/"+ env.BUILD_NUMBER +".log"
					touch touch_file
				}
			}
		}

把这个加到原来的pipeline stage文件,运行,然后去自己jenkins 节点机器下找到刚刚touch的文件

[root@Anthony ~]# cd /var/lib/jenkins/workspace/pipeline-project-demo/testdata/
[root@Anthony testdata]# ls
123.txt  abc.log  new_json.json   test.properties
48.log   a.log    test_json.json
[root@Anthony testdata]# cat 48.log
[root@Anthony testdata]# ls -l 48.log
-rw-r--r-- 1 jenkins jenkins 0 Nov  8 10:08 48.log
[root@Anthony testdata]#

我的测试jenkins job:http://65.49.216.200:8080/job/pipeline-project-demo/48/

这里需要提醒的哈,touch的文件在jenkins 节点机器不是在代码库,也不是在你eclipse项目里。

猜你喜欢

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