jenkins流水线pipeline脚本实例

发送邮件

import hudson.model.*;
 
println env.JOB_NAME
println env.BUILD_NUMBER
 
pipeline{
    
    agent any
    stages{
        stage("send mail test") {
            steps{
                script {
                    mail to: '[email protected]',
                    subject: "Running Pipeline: ${currentBuild.fullDisplayName}",
                    body: "Something is wrong with ${env.BUILD_URL}"
                }
            }
        }
    }
}

发送邮件svn日志内容

pipeline {
    agent any
    stages {
        stage('拉代码') {
            steps {
                checkout("svn代码")
            }
        }
        stage('输出日志') {
            steps {
                script{
                    //调用方法得到日志并输出
                    def changeString = getChangeString()
                    echo "$changeString"
                }
            }
        }
        stage("发送邮件") {
            steps{
                script {
                    echo "$changeString"
                    mail to: '[email protected]',
                    subject: "${JOB_NAME}  (${BUILD_NUMBER})-提交SVN日志信息",
                    body: "SVN版本变更信息:\n$changeString\n构建日志: $BUILD_URL/console"
                }
            }
        }
    }
}

@NonCPS
def getChangeString() {
    MAX_MSG_LEN = 100
    def changeString = ""
    echo "Gathering SCM changes"
    def changeLogSets = currentBuild.changeSets
    for (int i = 0; i < changeLogSets.size(); i++) {
        def entries = changeLogSets[i].items
        for (int j = 0; j < entries.length; j++) {
            def entry = entries[j]
            truncated_msg = entry.msg.take(MAX_MSG_LEN)
            changeString += "${truncated_msg} -- ${entry.author}\n"
        }
    }

    if (!changeString) {
        changeString = "${currentBuild.fullDisplayName}:当前构建版本没有新的变更信息!"
    }
    return changeString
}

 

你可能感兴趣的:(jenkins流水线pipeline脚本实例)