在Gitlab的CI执行中,我们可以完全不需要Jenkins持续集成的工具就可以在Gitlab中完成CICD的体系化建设。在本文章中,主要介绍结合Gitlab工具以及Jenkins的持续集成工具,在本地的代码push提交后,能够自动触发CI中的项目自动执行,这其实就会有很多的想象空间,比如在开发同学push提交代码后,开发的单元测试代码先执行,接下来是冒烟测试执行,然后是API自动化测试持续执行,完全的可以打造可持续的流水线的方式来验证每次提交代码的质量检查和验证,在过程中如果存在执行失败,可以对接的如钉钉这些进行报警。下来主要详细的介绍这部分的配置和案例实战。
在CI中安装插件Gitlab hooks,具体的插件名称为:Gitlab Hook Plugin,GitLab Plugin,Git plugin,安装插件成功后,在CI的项目中,在代码管理中,把分支部分取消,也就是任意分支提交都是能够进行自动触发的,如下所示:
在Jenkins中选择要触发的项目,如saas的项目,然后点击配置,在构建触发器中选择Build when a change is pushed to GitLab,如下所示:
然后点击高级,到Secret token中点击Generate,就会自动生成Secret token的信息,如下所示:
特别注意:需要记住的是GitLab Web Hook的URL地址信息和Secure token的信息,切记。
在Gitlab的系统中,在Gitlab的saas工程中,点击Settings中的Itergrations,如下所示:
然后填写Web Hook的URL地址和Secure token的信息,如下所示:
主要填写正确的Web Hook地址和Secure token信息后,其他的都是默认的,最后点击Add webhook添加按钮,添加成功就会显示这些信息:
然后点击test可以进行测试,test下来框我们选择Push events,如下所示:
点击Push events后,就会自动触发远程的Jenkins项目执行,点击后,如下所示:
下来就会自动触发执行,输出的信息具体为:
代码语言:javascript复制Started by GitLab push by wuya
Running as SYSTEM
Building in workspace /root/.jenkins/workspace/saas
The recommended git tool is: NONE
No credentials specified
> git rev-parse --resolve-git-dir /root/.jenkins/workspace/saas/.git # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url http://47.***.***.233/wuya/saas.git # timeout=10
Fetching upstream changes from http://47.***.****.233/wuya/saas.git
> git --version # timeout=10
> git --version # 'git version 1.8.3.1'
> git fetch --tags --progress http://47.***.***.233/wuya/saas.git refs/heads/*:refs/remotes/origin/* # timeout=10
skipping resolution of commit remotes/origin/master, since it originates from another repository
Seen branch in repository origin/master
Seen 1 remote branch
> git show-ref --tags -d # timeout=10
Checking out Revision fe214b29e04da559410f7626998360659f91ad3b (origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f fe214b29e04da559410f7626998360659f91ad3b # timeout=10
Commit message: "Merge branch 'master' of http://47.95.142.233/wuya/saas"
> git rev-list --no-walk fe214b29e04da559410f7626998360659f91ad3b # timeout=10
[saas] $ /bin/sh -xe /root/devops/apache-tomcat/temp/jenkins6227912556825410283.sh
python3 -m pytest test_login.py
============================= test session starts ==============================
platform linux -- Python 3.7.1, pytest-5.4.3, py-1.10.0, pluggy-0.13.1
rootdir: /root/.jenkins/workspace/saas
plugins: allure-pytest-2.9.43, html-2.0.1, forked-1.1.3, xdist-1.31.0, metadata-1.8.0
collected 5 items
test_login.py ..... [100%]
============================== 5 passed in 0.02s ===============================
Finished: SUCCESS
然后我们再来看Gitlab的展示,我们点击edit,就会出现我们测试的信息,如下所示:
点击View details,就会显示详细的执行信息,其实就是通过API调用的执行信息,具体信息如下所示:
代码语言:javascript复制Request URL: POST http://47.***.***.233:8080/jenkins/project/saas 200
Trigger: Push Hook
Elapsed time: 0.02 sec
Request time: 4 minutes ago
Request headers:
Content-Type: application/json
X-Gitlab-Event: Push Hook
X-Gitlab-Token: a1d0328bae15c19a3628b5099c4d0f75
Request body:
{
"object_kind": "push",
"event_name": "push",
"before": "a5bf6239ac02b5784a5ccbb35d02100f3b5a1144",
"after": "fe214b29e04da559410f7626998360659f91ad3b",
"ref": "refs/heads/master",
"checkout_sha": "fe214b29e04da559410f7626998360659f91ad3b",
"message": null,
"user_id": 25,
"user_name": "wuya",
"user_username": "wuya",
"user_email": "",
"user_avatar": "https://www.gravatar.com/avatar/858ce7b9321d1b66f49c242b0dda1a87?s=80&d=identicon",
"project_id": 14,
"project": {
"id": 14,
"name": "saas",
"description": "",
"web_url": "http://47.***.***.233/wuya/saas",
"avatar_url": null,
"git_ssh_url": "git@47.***.***.233:wuya/saas.git",
"git_http_url": "http://47.***.***.233/wuya/saas.git",
"namespace": "wuya",
"visibility_level": 20,
"path_with_namespace": "wuya/saas",
"default_branch": "master",
"ci_config_path": null,
"homepage": "http://47.***.***.233/wuya/saas",
"url": "git@47.***.***.233:wuya/saas.git",
"ssh_url": "git@47.***.***.233:wuya/saas.git",
"http_url": "http://47.***.***.233/wuya/saas.git"
},
"commits": [
{
"id": "fe214b29e04da559410f7626998360659f91ad3b",
"message": "Merge branch 'master' of http://47.***.***.233/wuya/saasn",
"timestamp": "2021-06-26T02:09:25Z",
"url": "http://47.***.***.233/wuya/saas/commit/fe214b29e04da559410f7626998360659f91ad3b",
"author": {
"name": "wuya",
"email": "2839168630@qq.com"
},
"added": [
".gitlab-ci.yml"
],
"modified": [
],
"removed": [
]
},
{
"id": "d15f40bd3419b9f47d5983381e55e312fd69e24c",
"message": "增加了新的3个测试点n",
"timestamp": "2021-06-26T02:08:57Z",
"url": "http://47.***.***.233/wuya/saas/commit/d15f40bd3419b9f47d5983381e55e312fd69e24c",
"author": {
"name": "wuya",
"email": "2839168630@qq.com"
},
"added": [
],
"modified": [
"test_login.py"
],
"removed": [
]
},
{
"id": "a5bf6239ac02b5784a5ccbb35d02100f3b5a1144",
"message": "Update .gitlab-ci.yml",
"timestamp": "2021-06-20T09:56:42Z",
"url": "http://47.***.***.233/wuya/saas/commit/a5bf6239ac02b5784a5ccbb35d02100f3b5a1144",
"author": {
"name": "wuya",
"email": "2839168630@qq.com"
},
"added": [
],
"modified": [
".gitlab-ci.yml"
],
"removed": [
]
}
],
"total_commits_count": 3,
"push_options": {
},
"repository": {
"name": "saas",
"url": "git@47.***.***.233:wuya/saas.git",
"description": "",
"homepage": "http://47.***.***.233/wuya/saas",
"git_http_url": "http://47.***.***.233/wuya/saas.git",
"git_ssh_url": "git@47.***.***.233:wuya/saas.git",
"visibility_level": 20
}
}
Response headers:
X-Content-Type-Options: nosniff
Content-Length: 0
Date: Sat, 26 Jun 2021 02:33:07 GMT
Connection: close
Response body:
下面我们到saas下编辑代码后,进行push,就会自动触发执行,如下所示: