Jenkins高级篇之Pipeline方法篇-Pipeline Basic Steps-6-写文件writeFile和git SCM

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

       这篇打算结束basic pipeline这个插件的方法学习,前面和本篇介绍方法,基本把常用的basic pipeline里面的方法都介绍了一遍。如果以后遇到不会的,可以去这个官方网站去查询新的方法介绍,其实官网也是比较坑的,介绍了这个方法的作用,但是没有给出一个具体的代码的示例。就拿https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/ 这篇插件的官网来说,真的没有一个具体代码示例。我都是边看官网文章具体方法介绍,如果不会这个方法代码,就去google查询,然后在具体jenkins job上不断测试和调整代码,最后把测试通过的代码放在这个系列文章中。

1.writeFile

在前面文章,我介绍了readFile,前面介绍了如何读,那么这里就介绍如何写。readFile和writeFile都是简单的文本文件读写。当你拿到一个文件,不知道选择什么方式来读写,那么这个readFile和writeFile就是最原始的方法。但是,如果是json类型文件,那么我们可以用前面介绍插件的readJson来读取,读取得到是一个json 的对象,就可以直接调用json的方法。

我会在/testdata目录下,写入一个write.txt的文件,先通过writeFile写入,然后通过readFile读取出来。

pipeline代码

import hudson.model.*;

println env.JOB_NAME
println env.BUILD_NUMBER

pipeline{
	
	agent any
	stages{
		stage("writeFile demo") {
			steps{
				script {
					write_file_path = "${env.WORKSPACE}/testdata/write.txt"
					file_contents = "Hello Anthony!! 这是一个测试例子"
					//write into write.txt
					writeFile file: write_file_path, text: file_contents, encoding: "UTF-8"
					// read file and print it out
					fileContents = readFile file: write_file_path, encoding: "UTF-8"
					println fileContents
				}
			}
		}
	}
}

测试结果

[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (writeFile demo)
[Pipeline] script
[Pipeline] {
[Pipeline] writeFile
[Pipeline] readFile
[Pipeline] echo
Hello Anthony!! ����һ����������
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

上面看到了乱码,即使我代码中使用了encoding=UTF-8进行读写操作,但是由于github系统不支持保存中文代码,所以这里打印出来内容就是乱码。

其实关于basic pipeline这个插件,有很多操作是写到step(...) 这个代码块里的,在step里可以调用其他构建或者构建后操作,例如构建后操作一般有发送邮件提醒功能。官网也有很多方法和参数关于step的描述,我可以负责任的说,除非你对Jenkins太熟悉,否则你完全可以跳过官网关于step的这个很长篇幅的学习,反正我工作是是很少用到官网介绍关于在step里能实现的一些功能。

2.git plugin插件介绍

下面我们来学习别的常用的插件以及方法,前面我们运行stage,如果你观察了jenkins的job 的stage view,一定会看到第一个stage的名称叫Declarative: Checkout SCM,这个是pipeline自动给加上的。有时候,我们在一个项目自动化中,可能还需要checkout其他git项目的代码。这种有两个git项目地址的自动化项目有很多,下面我就来介绍一下。

环境条件:

1.我分别在github上有两个项目

2.我在jenkins配置了访问github的用户名和密码

这重点提下这个访问github的密码在jenkins上的存储。

我的Jenkins环境上就配置了一个github的访问用户,记住红圈这个id,待会下面代码需要用到。

3.我写两个stage,第二个stage会显示如何在pipeline中下载我第二个github项目

下面是我的pipeline代码

import hudson.model.*;

println env.JOB_NAME
println env.BUILD_NUMBER

pipeline{
	
	agent any
	stages{
		stage("Hello Demo") {
			steps{
				script {
					println "Hello Demo!"
				}
			}
		}
		
		stage("git checkout") {
			steps{
				script {
					checkout([$class: 'GitSCM', branches: [[name: '*/master']],
						userRemoteConfigs: [[credentialsId: '6f4fa66c-eb02-46dc-a4b3-3a232be5ef6e', 
							url: 'https://github.com/Anthonyliu86/HelloWorld.git']]])
				}
			}
		}
	}
}

我的测试运行日志

[Pipeline] echo
pipeline_basic_steps
[Pipeline] echo
80
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/pipeline_basic_steps
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/Anthonyliu86/Pipeline-learn-demo-anthony.git # timeout=10
Fetching upstream changes from https://github.com/Anthonyliu86/Pipeline-learn-demo-anthony.git
 > git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress https://github.com/Anthonyliu86/Pipeline-learn-demo-anthony.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 4abe7ec1dba5ffd25f258347b8f9594377eefb3f (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 4abe7ec1dba5ffd25f258347b8f9594377eefb3f
Commit message: "add git check demo"
 > git rev-list --no-walk 9689ab8bad547bdfc21d9b8a742b8df3a4dc3376 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello Demo)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Hello Demo!
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (git checkout)
[Pipeline] script
[Pipeline] {
[Pipeline] checkout
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/Anthonyliu86/HelloWorld.git # timeout=10
Fetching upstream changes from https://github.com/Anthonyliu86/HelloWorld.git
 > git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress https://github.com/Anthonyliu86/HelloWorld.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 6038053d56db63710721b0847398e2a02ca8d7f1 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 6038053d56db63710721b0847398e2a02ca8d7f1
Commit message: "Update test1"
First time build. Skipping changelog.
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

这种git 下载代码的方式,在任何基于git的版本控制服务器都可以,常见的github,gittree,gitlab等。一般在pipeline代码中,很常见在不同stage中使用不同git仓库地址的代码,所以git SCM操作,可以写在不同stage中。这个到指定git服务器上拉取代码是很常见的pipeline代码操作,其他关于什么github集成插件里面方法的几乎可以不去了解。

到这里,暂时结束学习不同pipeline 插件如何在pipeline 代码里使用,如果你想查询你知道的插件,如何写pipeline去实现,去https://jenkins.io/doc/pipeline/steps/

这个地址查询插件名称就好。例如,我就看到钉钉提醒,之前在我博客介绍简单二次开发这个插件。接下来文章,我可能会借着selenium UI自动化具体项目来结合pipeline,演示如何做集成测试,所有的Jenkins UI操作都通过用pipeline代码的方式来实现。

猜你喜欢

转载自blog.csdn.net/u011541946/article/details/84678803
SCM