DEVOPS技术实践_23:判断文件下载成功作为执行条件

在实际生产中,我们经常会需要通过判断一个结果作为一个条件去执行另一个内容,比如判断一个文件是否存在,判官一个命令是否执行成功等等

现在我们选择其中一个场景进行实验,当某个目录下存在,则执行操作

1. 下载一个文件

[root@node1 ~]# cd /opt/

[root@node1 opt]# wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tgz

[root@node1 opt]# ll

-rw-r--r-- 1 root root 22802018 Oct 20  2018 Python-3.7.1.tgz

就判断这个文件是否存在

2 Jenkinsfile文件

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']], 
                doGenerateSubmoduleConfigurations: false, 
                extensions: [], 
                submoduleCfg: [], 
                userRemoteConfigs: [[credentialsId: 'e3e48ed7-dbce-4642-bb18-28e0c71ab962', 
                url: 'http://192.168.132.132/root/hello-world-greeting.git']]])
                }
            }
        }
        stage("Check file download") {
            steps {
                script {
                    out = sh(script: "ls /opt ", returnStdout: true).toString().trim()
                        println out
                        if(out.contains("Python-3.7.1.tgz")) {
                            println "file download successfully."
                        }else {
                            sh("exit 1")
                        }
                    }
                    
                }
            }
        }
    }

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

3 构建执行

DEVOPS技术实践_23:判断文件下载成功作为执行条件_第1张图片

控制台输出

Started by user darren ning
Obtained pipeline.jenkinsfile from git http://192.168.132.132/root/pipeline-parameter-test.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] echo
pipeline_parameter_project
[Pipeline] echo
127
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/pipeline_parameter_project
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url http://192.168.132.132/root/pipeline-parameter-test.git # timeout=10
Fetching upstream changes from http://192.168.132.132/root/pipeline-parameter-test.git
 > git --version # timeout=10
 > git fetch --tags --progress http://192.168.132.132/root/pipeline-parameter-test.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 62425dd2e7881670ddad0294e22aa9a2427eb835 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 62425dd2e7881670ddad0294e22aa9a2427eb835
Commit message: "Update pipeline.jenkinsfile"
 > git rev-list --no-walk 35d435ba12df03fb7b7c9b302a59511a19141c42 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (git checkout)
[Pipeline] script
[Pipeline] {
[Pipeline] checkout
using credential e3e48ed7-dbce-4642-bb18-28e0c71ab962
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url http://192.168.132.132/root/hello-world-greeting.git # timeout=10
Fetching upstream changes from http://192.168.132.132/root/hello-world-greeting.git
 > git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress http://192.168.132.132/root/hello-world-greeting.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 50eaa401672536f50add4ba0e86d290b8ffafb3c (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 50eaa401672536f50add4ba0e86d290b8ffafb3c
Commit message: "Update Jenkinsfile"
 > git rev-list --no-walk 50eaa401672536f50add4ba0e86d290b8ffafb3c # timeout=10
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Check file download)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ ls /opt
[Pipeline] echo
Python-3.7.1.tgz
[Pipeline] echo
file download successfully.
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

4 改进实验

1.添加try catch语句块

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

jenkinsfile文件

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']], 
                doGenerateSubmoduleConfigurations: false, 
                extensions: [], 
                submoduleCfg: [], 
                userRemoteConfigs: [[credentialsId: 'e3e48ed7-dbce-4642-bb18-28e0c71ab962', 
                url: 'http://192.168.132.132/root/hello-world-greeting.git']]])
                }
            }
        }
        stage("Check file download") {
            steps {
                script {
                    try{
                    out = sh(script: "ls /opt ", 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.")
                    }
                }
            }
        }
    }
}

构建

DEVOPS技术实践_23:判断文件下载成功作为执行条件_第2张图片

 控制台输出

Started by user darren ning
Obtained pipeline.jenkinsfile from git http://192.168.132.132/root/pipeline-parameter-test.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] echo
pipeline_parameter_project
[Pipeline] echo
129
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/pipeline_parameter_project
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url http://192.168.132.132/root/pipeline-parameter-test.git # timeout=10
Fetching upstream changes from http://192.168.132.132/root/pipeline-parameter-test.git
 > git --version # timeout=10
 > git fetch --tags --progress http://192.168.132.132/root/pipeline-parameter-test.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 902f050f4a8d3a83a625f583b87f394d0fbad31f (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 902f050f4a8d3a83a625f583b87f394d0fbad31f
Commit message: "Update pipeline.jenkinsfile"
 > git rev-list --no-walk 62425dd2e7881670ddad0294e22aa9a2427eb835 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (git checkout)
[Pipeline] script
[Pipeline] {
[Pipeline] checkout
using credential e3e48ed7-dbce-4642-bb18-28e0c71ab962
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url http://192.168.132.132/root/hello-world-greeting.git # timeout=10
Fetching upstream changes from http://192.168.132.132/root/hello-world-greeting.git
 > git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress http://192.168.132.132/root/hello-world-greeting.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 50eaa401672536f50add4ba0e86d290b8ffafb3c (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 50eaa401672536f50add4ba0e86d290b8ffafb3c
Commit message: "Update Jenkinsfile"
 > git rev-list --no-walk 50eaa401672536f50add4ba0e86d290b8ffafb3c # timeout=10
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Check file download)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ ls /opt
[Pipeline] echo
Python-3.7.1.tgz
[Pipeline] echo
file download successfully.
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

5 尝试失败构建

修改文件的名字

[root@node1 opt]# mv Python-3.7.1.tgz Python-3.7.1.tg

[root@node1 opt]# ll

-rw-r--r-- 1 root root 22802018 Oct 20  2018 Python-3.7.1.tg

再次构建

DEVOPS技术实践_23:判断文件下载成功作为执行条件_第3张图片

Started by user darren ning
Obtained pipeline.jenkinsfile from git http://192.168.132.132/root/pipeline-parameter-test.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] echo
pipeline_parameter_project
[Pipeline] echo
135
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/pipeline_parameter_project
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url http://192.168.132.132/root/pipeline-parameter-test.git # timeout=10
Fetching upstream changes from http://192.168.132.132/root/pipeline-parameter-test.git
 > git --version # timeout=10
 > git fetch --tags --progress http://192.168.132.132/root/pipeline-parameter-test.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 34338b984aa9c700e50fdbaf0b2a2a1cc7ba264b (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 34338b984aa9c700e50fdbaf0b2a2a1cc7ba264b
Commit message: "Update pipeline.jenkinsfile"
 > git rev-list --no-walk 64c00d7ecd3a420314e3950676835d9bb07b0e88 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (git checkout)
[Pipeline] script
[Pipeline] {
[Pipeline] checkout
using credential e3e48ed7-dbce-4642-bb18-28e0c71ab962
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url http://192.168.132.132/root/hello-world-greeting.git # timeout=10
Fetching upstream changes from http://192.168.132.132/root/hello-world-greeting.git
 > git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress http://192.168.132.132/root/hello-world-greeting.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 50eaa401672536f50add4ba0e86d290b8ffafb3c (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 50eaa401672536f50add4ba0e86d290b8ffafb3c
Commit message: "Update Jenkinsfile"
 > git rev-list --no-walk 50eaa401672536f50add4ba0e86d290b8ffafb3c # timeout=10
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Check file download)
[Pipeline] script
[Pipeline] {
[Pipeline] sh
+ ls /opt
[Pipeline] echo
Python-3.7.1.tg
[Pipeline] sh
+ exit 1
[Pipeline] echo
e
[Pipeline] error
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: fond error during check file download.
Finished: FAILURE

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

参考文献:https://blog.csdn.net/u011541946/article/details/84945882

你可能感兴趣的:(DEVOPS技术实践_23:判断文件下载成功作为执行条件)