Jenkinsfile defined in objects and functions, access to git author, a notification is sent nails

Since the beginning of the use of Jenkinsfile after Jenkins configured as rampant as CI developers to customize the script is too convenient.

For example, recently developed a project to involve people, to submit a lot of conflict, and some people did not compile their own submitted, resulting in the back of people are blocked, so we need to tell him: Submit failed.

First, Jenkinsfile should be how to use it, see: https://www.cnblogs.com/woshimrf/p/gitlab-with-jenkins.html

When you want to define Jenkinsfile nail notice, first of all get git Submitted by:

最外层:

class MyChange {
   String author;
   String msg;
}
@NonCPS
def getChanges()
{
    def changeLogSets = currentBuild.changeSets
    for (int i = 0; i < changeLogSets.size(); i++)
    {
        def entries = changeLogSets[0].items
        for (int j = 0; j < entries.length; j++)
        {
            def entry = entries[0]
            def change = new MyChange()
            change.author = entry.author
            change.msg = entry.msg
            return change
        }
    }

}

然后在node节点里定义变量

ding_group_access_token = "c20d35654da77a99d8869e04xxxxxxxd1a81aasssa2"
ding_jenkinsUrl = "http://jenkins.ryan.com/view/我的view名称/"

最后在失败的时候通知:


stage('Compile And UnitTest') {
    echo "2.Compile the code"
    try {
        sh "mvn clean install"
    } catch (Exception ex) {
        updateGitlabCommitStatus name: 'build', state: 'failed'
        def change = getChanges()
        dingTalk accessToken: "${ding_group_access_token}", imageUrl: '', jenkinsUrl: "${ding_jenkinsUrl}", message: "@所有人 构建失败@$change.author $change.msg" , notifyPeople: "$change.author"
        throw ex;
    } finally {

    }

    updateGitlabCommitStatus name: 'build', state: 'success'
}

The final Jenkinsfile might look like this

文件位置
my-project
- .deploy
   - Jenkinsfile
- src

Create a pipeline job in jenkins years, and specify Jenkinsfile

Jenkinsfile



class MyChange {
   String author;
   String msg;
}
@NonCPS
def getChanges()
{
    def changeLogSets = currentBuild.changeSets
    for (int i = 0; i < changeLogSets.size(); i++)
    {
        def entries = changeLogSets[0].items
        for (int j = 0; j < entries.length; j++)
        {
            def entry = entries[0]
            def change = new MyChange()
            change.author = entry.author
            change.msg = entry.msg
            return change
        }
    }

}

node {
    properties([gitLabConnection('gitlab-bigdata')])

    stage('Prepare') {
        echo "1.Prepare Stage"
        checkout scm
        updateGitlabCommitStatus name: 'build', state: 'pending'
        mvn_module = '多模块项目mvn子模块'
        pom = readMavenPom file: "${mvn_module}/pom.xml"
        k8s_label = "项目在k8s集群中的名称"
        docker_host = "自己的docker私服"
        ding_group_access_token = "c20d35654da77a99d8869e041xxxac7d6699xxxxxx"
        ding_jenkinsUrl = "http://jenkins.ryan.com/view/job所在的view/"
        //要部署的k8s集群, 默认是杭州(config-hangzhou), 可选上海(config-shanghai)
        k8s_cluster_node = "config-hangzhou"
        //部署环境
        profile=""
        if(env.BRANCH_NAME == 'dev') {
            profile = "dev"
            k8s_cluster_node = "config-dev"
        }
        if(env.BRANCH_NAME == 'test') {
            profile = "test"
            k8s_cluster_node = "config-shanghai"
        }
        if(env.BRANCH_NAME == 'master') {
            profile = "prod"
            k8s_cluster_node = "config-hangzhou"
        }

        img_name = "${pom.groupId}-${pom.artifactId}"
        docker_img_name = "${docker_host}/${img_name}"
        echo "group: ${pom.groupId}, artifactId: ${pom.artifactId}, version: ${pom.version}"
        echo "docker-img-name: ${docker_img_name}"
        script {
            build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
            build_tag = "${env.BRANCH_NAME}-${build_tag}"

            currentBuild.displayName = BUILD_NUMBER + "_" +build_tag
        }
    }

    stage('Compile And UnitTest') {
        echo "2.Compile the code"
        try {
            sh "mvn clean install"
        } catch (Exception ex) {
            updateGitlabCommitStatus name: 'build', state: 'failed'
            def change = getChanges()
            dingTalk accessToken: "${ding_group_access_token}", imageUrl: '', jenkinsUrl: "${ding_jenkinsUrl}", message: "@所有人 构建失败@$change.author $change.msg" , notifyPeople: "$change.author"
            throw ex;
        } finally {

        }

        updateGitlabCommitStatus name: 'build', state: 'success'
    }



    if (env.BRANCH_NAME == 'dev' ||env.BRANCH_NAME == 'test' || env.BRANCH_NAME == 'master') {
        stage('Build Docker Image') {
            echo "4.Build Docker Image Stage"
            sh "docker build -t ${docker_img_name}:${build_tag} " +
                    " --build-arg JAR_FILE=target/${mvn_module}.jar " +
                    " --build-arg profile=${profile} " +
                    " -f ${mvn_module}/.deploy/Dockerfile ./${mvn_module}"
        }

        stage('Push Docker Image') {
            echo "5.Push Docker Image Stage"
            //sh "mvn deploy -Dmaven.test.skip=true"
            sh "docker tag ${docker_img_name}:${build_tag} ${docker_img_name}:latest"
            sh "docker tag ${docker_img_name}:${build_tag} ${docker_img_name}:${pom.version}"
            withCredentials([usernamePassword(credentialsId: 'docker-register-sz-shuwei', passwordVariable: 'dockerPassword', usernameVariable: 'dockerUser')]) {
                sh "docker login -u ${dockerUser} -p ${dockerPassword} ${docker_host}"
                sh "docker push ${docker_img_name}:latest"
                sh "docker push ${docker_img_name}:${pom.version}"
                sh "docker push ${docker_img_name}:${build_tag}"
            }
        }


        stage("Deploy to k8s - ${profile}") {
            echo "6. Deploy Stage"

            updateGitlabCommitStatus name: 'deploy', state: 'pending'
            sh "sed -i 's/<IMG_NAME>/${img_name}/g;s/<IMG_TAG>/${build_tag}/g;s/<k8s-label>/${k8s_label}/g' ${WORKSPACE}/${mvn_module}/.deploy/${profile}-k8s.yaml"
            sh "kubectl --kubeconfig /home/jenkins/.kube/${k8s_cluster_node} " +
                    "apply -f ${WORKSPACE}/${mvn_module}/.deploy/${profile}-k8s.yaml --record"
            sh "sleep 5"
            echo "创建的实例:"
            sh "kubectl --kubeconfig /home/jenkins/.kube/${k8s_cluster_node}  get po -o wide | grep ${k8s_label}"
            echo "您的应用svc: "
            sh "kubectl --kubeconfig /home/jenkins/.kube/${k8s_cluster_node}  get svc | grep ${k8s_label}"

            updateGitlabCommitStatus name: 'deploy', state: 'success'
        }
    }



}




Guess you like

Origin www.cnblogs.com/woshimrf/p/jenkinsfile-customize-notify-with-git-commit-author.html