【jenkins】gitlab配置MR jenkins流水线门禁

1. 新建流水线项目

 

 

 

2. 配置jenkins流水线项目触发方式为Opened Merge Request Events

 

 

 

 

 

   配置目标分支和生成token

3. gitlab添加jenkins项目的webhook

 

 

强制拦截配置

 

 

 4. 配置流水线

pipeline {
    agent {
        node {
            // 节点名称
            label 'node-1'
            customWorkspace "${env.JOB_NAME}/${env.BUILD_NUMBER}"
        }
    }
    options {
        gitLabConnection('gitlab')
    }
    stages {
        stage('PreEnv') {
            steps {
                script {
                    RECEIVER = RECEIVER
                }
                sh 'java -version'
            }
        }
        stage('Checkout') {
            steps {
                checkout changelog: true, poll: true, scm: [
                  $class: 'GitSCM',
                  branches: [[name: "origin/${env.gitlabSourceBranch}"]],
                  doGenerateSubmoduleConfigurations: false,
                  extensions: [[
                    $class: 'PreBuildMerge',
                    options: [
                      fastForwardMode: 'FF',
                      mergeRemote: 'origin',
                      mergeStrategy: 'default',
                      mergeTarget: "${env.gitlabTargetBranch}"
                    ]
                  ]],
                  extensions: [[
                    $class: 'UserIdentity', 
                    email: "${env.gitlabUserEmail}", 
                    name: "${env.gitlabUserName}"
                  ]],
                  extensions: [[
                    $class: 'RelativeTargetDirectory', 
                    relativeTargetDir: 'ningbo-port-cloud-platform'
                  ]],
                  submoduleCfg: [],
                  userRemoteConfigs: [[
                    url: "${env.gitlabSourceRepoURL}"
                  ]]
                ]
            }
        }
        stage('Build') { 
            steps {
                // TODO UnitTest
                echo 'start Build ......'
            }
        }
    }
    post {
        success {
            send_email('Succeed', "$RECEIVER")
            updateGitlabCommitStatus(name: 'build', state: 'success')
        }
        failure {
            send_email('Failed', "$RECEIVER")
            updateGitlabCommitStatus(name: 'build', state: 'failed')
            addGitLabMRComment comment: "构建日志地址:${env.BUILD_URL}console"
        }
        unstable {
            send_email('unstable', "$RECEIVER")
            updateGitlabCommitStatus(name: 'build', state: 'success')
            addGitLabMRComment comment: "构建日志地址:${env.BUILD_URL}console"
        }
        aborted {
            send_email('Aborted', "$RECEIVER")
            updateGitlabCommitStatus(name: 'build', state: 'failed')
        }
    }
}

def send_email(String result, String receiver) {
    emailext (
        subject: "Build: ' + $result",
        body: '$DEFAULT_CONTENT',
        to: "$receiver"
    )
}

5. 测试, 通过自己的分支向master分支提交MR

 

 

参考链接:https://blog.csdn.net/nklinsirui/article/details/100521145

 

posted @ 2022-02-14 18:06  代码诠释的世界  阅读(1694)  评论(0编辑  收藏  举报