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

释放双眼,带上耳机,听听看~!

本篇开始,举例几个实际的项目小练习。这几个小练习,你通过自动化脚本可以实现,只不过,现在需要转换成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

效果如下


1
2
3
4
1[root@Anthony test]# ls
2Python-3.7.1.tgz
3[root@Anthony test]#
4

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1import hudson.model.*;
2
3println env.JOB_NAME
4println env.BUILD_NUMBER
5
6pipeline{
7  
8   agent any
9   stages{
10     
11      stage("git checkout") {
12          steps{
13              script {
14                  checkout([$class: 'GitSCM', branches: [[name: '*/master']],
15                      userRemoteConfigs: [[credentialsId: '6f4fa66c-eb02-46dc-a4b3-3a232be5ef6e',
16                          url: 'https://github.com/Anthonyliu86/HelloWorld.git']]])
17              }
18          }
19      }
20      stage("Check file download") {
21          steps {
22              script {
23                  out = sh(script: "[ -f /tmp/test/Python-3.7.1.tgz ]  && echo 'true' || echo 'false' ", returnStdout: true)
24                  println out
25                  if(out == "true") {
26                      println "file download successfully."
27                  }
28              }
29          }
30      }
31  }
32}
33
34

测试并观察日志输出


1
2
3
4
5
6
7
8
9
10
11
12
1[Pipeline] { (Check file download)
2[Pipeline] script
3[Pipeline] {
4[Pipeline] sh
5[pipeline_basic_steps] Running shell script
6+ '[' -f /tmp/test/Python-3.7.1.tgz ']'
7+ echo true
8[Pipeline] echo
9true
10
11[Pipeline] }
12

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

优化代码

1.添加try catch语句块

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

优化后的代码如下


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
1import hudson.model.*;
2
3println env.JOB_NAME
4println env.BUILD_NUMBER
5
6pipeline{
7  
8   agent any
9   stages{
10     
11      stage("git checkout") {
12          steps{
13              script {
14                  checkout([$class: 'GitSCM', branches: [[name: '*/master']],
15                      userRemoteConfigs: [[credentialsId: '6f4fa66c-eb02-46dc-a4b3-3a232be5ef6e',
16                          url: 'https://github.com/Anthonyliu86/HelloWorld.git']]])
17              }
18          }
19      }
20      stage("Check file download") {
21          steps {
22              script {
23                 
24                  try{
25                      out = sh(script: "[ -f /tmp/test1/Python-3.7.1.tgz ]  && echo 'true' || echo 'false' ", returnStdout: true)
26                      println out
27                      if(out == "true") {
28                          println "file download successfully."
29                      }else {
30                          sh("exit 1")
31                      }
32                  } catch(Exception e) {
33                      println e
34                      error("fond error during check file download.")
35                  }
36              }
37          }
38      }
39  }
40}
41
42

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

执行结果日志:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1[Pipeline] { (Check file download)
2[Pipeline] script
3[Pipeline] {
4[Pipeline] sh
5[pipeline_basic_steps] Running shell script
6+ '[' -f /tmp/test1/Python-3.7.1.tgz ']'
7+ echo false
8[Pipeline] echo
9false
10
11[Pipeline] sh
12[pipeline_basic_steps] Running shell script
13+ exit 1
14[Pipeline] echo
15hudson.AbortException: script returned exit code 1
16[Pipeline] error
17[Pipeline] }
18[Pipeline] // script
19[Pipeline] }
20[Pipeline] // stage
21[Pipeline] }
22[Pipeline] // withEnv
23[Pipeline] }
24[Pipeline] // node
25sh: line 1: 14088 Terminated              sleep 3
26sh: line 1: 14098 Terminated              sleep 3
27[Pipeline] End of Pipeline
28ERROR: fond error during check file download.
29Finished: FAILURE
30

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
1import hudson.model.*;
2
3println env.JOB_NAME
4println env.BUILD_NUMBER
5
6pipeline{
7  
8   agent any
9   stages{
10     
11      stage("git checkout") {
12          steps{
13              script {
14                  checkout([$class: 'GitSCM', branches: [[name: '*/master']],
15                      userRemoteConfigs: [[credentialsId: '6f4fa66c-eb02-46dc-a4b3-3a232be5ef6e',
16                          url: 'https://github.com/Anthonyliu86/HelloWorld.git']]])
17              }
18          }
19      }
20      stage("Check file download") {
21          steps {
22              script {
23                 
24                  try{
25                      out = sh(script: "ls /tmp/test ", returnStdout: true).toString().trim()
26                      println out
27                      if(out.contains("Python-3.7.1.tgz")) {
28                          println "file download successfully."
29                      }else {
30                          sh("exit 1")
31                      }
32                  } catch(Exception e) {
33                      println e
34                      error("fond error during check file download.")
35                  }
36              }
37          }
38      }
39  }
40}
41
42

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

 

给TA打赏
共{{data.count}}人
人已打赏
安全经验

职场中的那些话那些事

2021-9-24 20:41:29

安全经验

AWStats日志分析系统

2021-11-28 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索