美文网首页
jenkins pipeline exception

jenkins pipeline exception

作者: AppleLin_cdea | 来源:发表于2021-10-13 14:57 被阅读0次

    2021年10月13日14:52:38 星期三
    jenkins pipeline exception 例子

    示例代码

    import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
    
    class CDemo  implements Serializable
    {
        def script
        def m_is_cov
        def CDemo(sci)
        {
            this.script = sci
        }
        
        def test()
        {
            script.sh """#!/bin/bash -xe
                ls aaabbb
            """
        }
        
        def run(is_cov=false)
        {
            try {
                test()
            }catch(e){
                script.echo "test e: "+e
            }
        }
    }
    
    node('general')
    {
        stage('cov') {
            obj = new CDemo(this)
            obj.run(true)
        }
    }
    

    运行结果

    Console Output
    14:49:50 Started by user huangyalin
    14:49:50 Running in Durability level: MAX_SURVIVABILITY
    14:49:50 [Pipeline] Start of Pipeline
    14:49:50 [Pipeline] node
    14:49:50 Running on general in /home/jenkins/workspace/demo
    14:49:50 [Pipeline] {
    14:49:50 [Pipeline] stage
    14:49:50 [Pipeline] { (cov)
    14:49:50 [Pipeline] sh
    14:49:51 + ls aaabbb
    14:49:51 ls: cannot access 'aaabbb': No such file or directory
    14:49:51 [Pipeline] echo
    14:49:51 test e: hudson.AbortException: script returned exit code 2
    14:49:51 [Pipeline] }
    14:49:51 [Pipeline] // stage
    14:49:51 [Pipeline] }
    14:49:51 [Pipeline] // node
    14:49:51 [Pipeline] End of Pipeline
    14:49:52 Finished: SUCCESS
    

    总结

    如果函数 test() 内部出现 exception, 调用它的地方可以捕获 test() 里面产生的 exception.
    通过 try catch 可以捕获 excption 来自己处理,如果不用 try catch,那么 exception 就会让程序结束运行。

    相关文章

      网友评论

          本文标题:jenkins pipeline exception

          本文链接:https://www.haomeiwen.com/subject/omzsoltx.html