API接口文档
https://docs.gitlab.cn/jh/api/commits.html
配置pipline流水线
生成http请求代码:
使用HttpRequest插件生成
- sharelibs内容
//这是share libs里的
package devopsdef httpReq(reqType, reqUrl, reqBody, accessToken){def gitServer = "https://jihulab.com/api/v4"result = httpRequest consoleLogResponseBody: true, httpMode: 'POST',requestBody: reqBody,url: "${gitServer}/${reqUrl}",wrapAsMultipart: false,customHeaders: [[maskValue: false, name: 'contentType', value: 'APPLICATION_JSON'],[maskValue: true, name: 'Authorization', value: "Bearer ${accessToken}"]]return result}def changeCommitStatus(projectId, status, commitSha, accessToken){commitApi = "projects/${projectId}/statuses/${commitSha}?state=${status}"respones = httpReq("POST", commitApi, '', accessToken)println respones}
- jenkinsfile内容
// 这是jenkinsfile里边的
@Library('jenkinslibs') _
def tools = new devops.tools()
def sonar = new devops.sonarQube()
def gitlab = new devops.gitlab()if ("${runOpts}" == "GitlabPush"){branch = branch - "refs/heads/"currentBuild.description = "Trigger by ${url} ${branch}"def status = "running"env.accessToken = env.accessTokengitlab.changeCommitStatus(projectId, status, commitSha, env.accessToken)
}pipeline {agent {label {label 'vm-node'retries 10}}environment {CodePath = "${WORKSPACE}/code"}stages {stage('download code') { steps {checkout(scmGit(branches: [[name: '*/master']], extensions: [cleanAfterCheckout(deleteUntrackedNestedRepositories: true), [$class: 'RelativeTargetDirectory',relativeTargetDir: '$WORKSPACE/code/${GIT_REPOSITORY_NAME}']], userRemoteConfigs: [[credentialsId: 'f58233b3-bdb3-4fd4-82d6-4cea03d75971',url: 'https://codeup.aliyun.com/64257bf308203590adcc93bf/QMUI-Android.git']]))}}stage('QA') { steps {dir("${CodePath}"){script{tools.printVar("sonar scanner! ${PATH}", 'red')//sonar.Scanner("${projectName}", "${projectName}", "${WORKSPACE}/code/gradle/wrapper")}}}}stage('Deploy') { steps {script{tools.printVar("ni hao shara library!", 'red')}}}}post {always {script{println("running")}}aborted {script{println("aborted")gitlab.changeCommitStatus(projectId, "aborted", commitSha, env.accessToken)}}success {script{println("success")gitlab.changeCommitStatus(projectId, "success", commitSha, env.accessToken)}}failure {script{println("failure")gitlab.changeCommitStatus(projectId, "failure", commitSha, env.accessToken)}}}}
jenkins中的配置
测试和验证
结论:可以再优化以下,作为merge请求的门禁。缺点是不好根据状态找到jenkins记录
文章中提到的accessToken获取方法见:https://blog.csdn.net/weixin_44014460/article/details/139195724