1. 程式人生 > >Jenkins高階篇之Pipeline方法篇-Pipeline Basic Steps-6-寫檔案writeFile和git SCM

Jenkins高階篇之Pipeline方法篇-Pipeline Basic Steps-6-寫檔案writeFile和git SCM

       這篇打算結束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程式碼的方式來實現。