jenkins pipline语句超时、重试;执行带参数;异常处理;邮件设置及发送预警

参考:
https://www.jianshu.com/p/f1167e8850cd
https://www.jenkins.io/zh/doc/pipeline/tour/running-multiple-steps/

1、pipline语句超时、重试

pipeline {
    agent any
    stages {
        stage('Deploy') {
            steps {
                retry(3) {
                    sh './flakey-deploy.sh'
                }

                timeout(time: 3, unit: 'MINUTES') {
                    sh './health-check.sh'
                }
            }
        }
    }
}

2、执行带参数

parameters

pipeline{
    agent any
    parameters {
        string(name: 'P1', defaultValue: 'it is p1', description: 'it is p1')
        booleanParam(name: 'P2', defaultValue: true, description: 'it is p2')
    }
    stages{
        stage("stage1"){
            steps{
                echo "$P1"
                echo "$P2"
            }
        }
    }
}

在这里插入图片描述

3、异常处理

script里try,catch

pipeline {
    agent any
    stages {
        stage('stage 1') {
            steps {
                script{
                    try {
                        sh 'exit 1'
                    }
                    catch (exc) {
                        echo 'Something failed'
                        
                    }
                }
            }
        }
    }
}

4、邮件设置及发送预警

设置:
a、这里写上发件邮件地址
在这里插入图片描述
b、
密码填qq设置里的授权码
用户名也写上发件邮件地址
端口465
在这里插入图片描述
在这里插入图片描述
可以先验证下
在这里插入图片描述

c、
通知这里邮箱填写与上一步邮件通知里完全一样
在这里插入图片描述
在这里插入图片描述
这些触发结果需要通知设置
在这里插入图片描述

pipline发送预警邮件

在post后填写:

*****如果发送成功,但是邮件没收到,可以改下重新生成qq smtp授权码试试

pipeline{
    agent any
    parameters {
        string(name: 'P1', defaultValue: 'it is p1', description: 'it is p1')
        booleanParam(name: 'P2', defaultValue: true, description: 'it is p2')
    }
    stages{
        stage("stage1"){
            steps{
                echo "$P1"
                echo "$P2"
            }
        }
    }
    post {
            success {
                emailext (
                    subject: "SUCCESSFUL: Job",
                    body: """<p>SUCCESSFUL: 12312</p>${env.JOB_NAME} ${env.BUILD_NUMBER},${env.BUILD_URL} """,
                    to: "[email protected],[email protected]",
                    from:"[email protected]"
                    
                   
                )
            }
            failure {
                emailext (
                    subject: "FAILED: Job ",
                    body: """<p>FAILED: Job213132</p>""",
                    to: "[email protected],[email protected]",
                    from:"[email protected]"
                    
                )
            }
        }
}




${env.JOB_NAME}
${env.BUILD_NUMBER}
${env.BUILD_URL}
在这里插入图片描述

Guess you like

Origin blog.csdn.net/weixin_42357472/article/details/120848450