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: '1399811201@qq.com',
                    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: 'qq@qq.com',
                    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
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM