Jenkins高级篇之Pipeline实践篇-1-如何判断文件下载成功举例

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

本篇开始,举例几个实际的项目小练习。这几个小练习,你通过自动化脚本可以实现,只不过,现在需要转换成pipeline的方式去实现。小练习,就不写module方法,所有的代码都在stage{...}里写。第一个小练习的题目是:在linux的/tmp/test目录下,判断python 3的文件文件是否下载成功。

需求分析:

在一个目录下判断一个文件是否成功下载?这里我们只考虑linux环境哈,windows的就不考虑。在/tmp/test 如何判断一个文件是否下载成功。一般来说,这个文件是否在这个路径/tmp/test是否显示,这是一种思路。更严谨的应该是,在前面基础之上,判断这个文件的大小,甚至md5值是否和服务器上提供的信息一致。这里我们模仿就简单,直接判断这个文件存在路径里就可以。

找对应的方法:

在jenkins中有fileExists(file路径),是有这么一个方法可以判断文件是否存在,但是这个方法只能判断当然Jenkins的workspace下的文件,显然本篇的/tmp/test/xxxx文件不在workspace,这个方法不适合。那么剩下就是采用linux shell命令来搞定这个事情,当然你可以用python或者Java同样可以解决问题。由于是linx环境,shell命令可能更适合。利用[ -f /tmp/test/xxx],执行这个命令,如果存在就会打印true,不存在就打印false.

为了模拟在/tmp/test/路径下有文件,我们提前创建/tmp/test这个文件夹,然后利用命令下载一个文件到这个路径:wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tgz

效果如下

[root@Anthony test]# ls
Python-3.7.1.tgz
[root@Anthony test]#

思路有了,我们就可以写代码并测试了。

import hudson.model.*;

println env.JOB_NAME
println env.BUILD_NUMBER

pipeline{
	
	agent any
	stages{
		
		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']]])
				}
			}
		}
		stage("Check file download") {
			steps {
				script {
					out = sh(script: "[ -f /tmp/test/Python-3.7.1.tgz ]  && echo 'true' || echo 'false' ", returnStdout: true)
					println out
					if(out == "true") {
						println "file download successfully."
					}
				}
			}
		}
	}
}

测试并观察日志输出

[Pipeline] { (Check file download)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
[pipeline_basic_steps] Running shell script
+ '[' -f /tmp/test/Python-3.7.1.tgz ']'
+ echo true
[Pipeline] echo
true

[Pipeline] }

这个shell命令 前半部分是判断文件是否存在,参数是-f,如果参数是-d就是判断文件夹是否存在。如果存在就输出true,否则输出false. 这里你可以更改一个文件名称,再次测试(replay),就可以得到输出false的效果。

优化代码

1.添加try catch语句块

2.如果发生错误,就把jenkinsjob标黄

优化后的代码如下

import hudson.model.*;

println env.JOB_NAME
println env.BUILD_NUMBER

pipeline{
	
	agent any
	stages{
		
		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']]])
				}
			}
		}
		stage("Check file download") {
			steps {
				script {
					
					try{
					    out = sh(script: "[ -f /tmp/test1/Python-3.7.1.tgz ]  && echo 'true' || echo 'false' ", returnStdout: true)
					    println out
					    if(out == "true") {
						    println "file download successfully."
					    }else {
							sh("exit 1")
						}
					} catch(Exception e) {
						println e
						error("fond error during check file download.")
					}
				}
			}
		}
	}
}

上面加了try catch之后,如果不等于true,那么就执行exit 1代码,Linux就退出,这个是一个异常,被catch之后,打印了异常e,并执行了error()方法,error方法在jenkins中是中断运行并标记当前运行job为failed的功能。

执行结果日志:

[Pipeline] { (Check file download)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
[pipeline_basic_steps] Running shell script
+ '[' -f /tmp/test1/Python-3.7.1.tgz ']'
+ echo false
[Pipeline] echo
false

[Pipeline] sh
[pipeline_basic_steps] Running shell script
+ exit 1
[Pipeline] echo
hudson.AbortException: script returned exit code 1
[Pipeline] error
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
sh: line 1: 14088 Terminated              sleep 3
sh: line 1: 14098 Terminated              sleep 3
[Pipeline] End of Pipeline
ERROR: fond error during check file download.
Finished: FAILURE

这个小练习,我们复习了sh() error()和学习了一个shell脚本,用来判断文件是否存在。到这里,其实我还是可以优化这个代码,如果不采用上面-f来判断文件是否存在,可以采用字符串操作的思维去解题。代码如下

import hudson.model.*;

println env.JOB_NAME
println env.BUILD_NUMBER

pipeline{
	
	agent any
	stages{
		
		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']]])
				}
			}
		}
		stage("Check file download") {
			steps {
				script {
					
					try{
					    out = sh(script: "ls /tmp/test ", returnStdout: true).toString().trim()
					    println out
					    if(out.contains("Python-3.7.1.tgz")) {
						    println "file download successfully."
					    }else {
							sh("exit 1")
						}
					} catch(Exception e) {
						println e
						error("fond error during check file download.")
					}
				}
			}
		}
	}
}

这个代码思路就是,把linux执行打印结果存在一个字符串中,通过字符串包含的方法去判断文件是否存在。这个思路有时候很好用,字符串操作在编程过程中经常被使用到。

猜你喜欢

转载自blog.csdn.net/u011541946/article/details/84945882
今日推荐