CI/CD之Gitlab集成Jenkins多分支pipeline实现质量检测和自动发布
本次实施主要实现:
- 代码提交gitlab,自动触发Jenkins构建
- gitlab发起Merge Request, 需要Jenkins检查通过才可以merge,实现代码review和质量管控
- gitlab开发分支merge后自动发布到test环境
- gitlab master分支merge后自动发布到prod环境
Jenkins Config
- 安装插件Gitlab, 使用教程: https://github.com/jenkinsci/gitlab-plugin#pipeline-jobs
- 安装插件Pipeline Utility Steps, 用来读取文件
- 安装插件Warnings Next Generation , 使用教程:https://github.com/jenkinsci/warnings-ng-plugin/blob/master/doc/Documentation.md#quality-gate-configuration
配置gitlab connection
系统设置-gitlab
配置API token, 需要登陆gitlab,给一个developer角色的账号,在系统设置中找到access token, 获取token。 然后在Jenkins中配置Gitlab API Toekn的凭证。
Jenkins多分支Job
新建多分支流水线任务。
配置分支源,输入gitlab地址,创建一个username password token, 填入gitlab的账号和密码。其他默认读取根目录下的jenkinsfile文件。
https://github.com/Ryan-Miao/code-quality-verify-demo/blob/master/Jenkinsfile
接下来重点就是Jenkinsfile里的配置。
主要有:
获取gitlab connection, 填写我们之前配置gitlab connection
properties([gitLabConnection('gitlab-bigdata')])
拉取代码
checkout scm
告诉gitlab job状态
updateGitlabCommitStatus name: 'build', state: 'pending'
不同分支走不同的构建方式
if (env.BRANCH_NAME == 'master' || env.BRANCH_NAME == 'dev' ) {
stage("Build Docker Image"){
echo "build docker image"
echo "Only dev/master branch can build docker image"
}
if(env.BRANCH_NAME == 'dev'){
stage("Deploy to test"){
echo "branch dev to deploy to environment test"
}
stage("Integration test"){
echo "test环境集成测试"
}
}
if(env.BRANCH_NAME == 'master'){
stage("Deploy to prod"){
echo "branch master to deploy to environment prod"
}
stage("Health check"){
echo "prod检查"
}
}
}
点击立即构建即可。
触发方式可以选择手动触发,定时触发(比如每分钟), gitlab trigger.
Gitlab trigger jenkins
对于多分支jenkins任务,trigger配置很简单。直接在gitlab项目配置中,找到integration,直接配置jenkins项目地址即可,选中push events和merge request events.
http://JENKINS_URL/project/PROJECT_NAME
When you configure the plugin to trigger your Jenkins job, by following the instructions below depending on job type, it will listen on a dedicated URL for JSON POSTs from GitLab's webhooks. That URL always takes the form http://JENKINS_URL/project/PROJECT_NAME, or http://JENKINS_URL/project/FOLDER/PROJECT_NAME if the project is inside a folder in Jenkins. You should not be using http://JENKINS_URL/job/PROJECT_NAME/build or http://JENKINS_URL/job/gitlab-plugin/buildWithParameters, as this will bypass the plugin completely.
Gitlab Merge Request
gitlab在项目设置中,找到Merge Request
Only allow merge requests to be merged if the pipeline succeeds
Pipelines need to be configured to enable this feature.
Only allow merge requests to be merged if all discussions are resolved
当我们发起一个M-R
当pipeline构建成功之后:
我们Jenkinsfile里设置不同分支的构建策略,这样就实现了不同环境的发布和质量校验。需要注意的是,当代码合并到master的时候,我们的功能就会执行发布策略了。而实际上,我们应该发布到canary金丝雀环境,即预生产环境,等确保没有任何问题之后再手动发布到prod。这里简化处理发布流程,直接发布。
参考
关注我的公众号

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2017-06-17 banner自动生成工具,ascii文字展示
2015-06-17 jQuery中使用ajax,$.post