Gitlab钩子提交合并代码触发Jenkins构建
一、配置安装
1.1 Gitlab安装
GitLab下载安装_GitLab最新中文免费版下载安装-极狐GitLab
1.2 Jenkins安装
1.3 安装问题总结
- Gitlab和Jenkins安装在同一台机器上时会有端口占用问题,Jenkins和Gitlab都会用到8080端口,需要对其中一个工具进行配置文件的端口更改
GitLab修改端口_gitlab 修改端口_热爱技术的小陈的博客-CSDN博客
二、触发流程配置及问题总结
2.1 配置流程
:::
2.1.1 Jenkins需要安装gitlab插件(Generic Webhook Trigger)
:::
:::
2.1.2 在Jenkins中勾选gitlab插件选项
:::
:::
2.1.3 进入需要监听的项目
:::
:::
2.1.4 点击"Settings"下的"Webhooks"
:::
:::
2.1.5 点击"Add new webhook"
:::
:::
2.1.6 配置URL及token,配置好之后点击"Sava changes"
:::
gitlab:
选择需要监控的分支:
Jenkins:
:::
2.1.7 点击Test的push操作,测试通过
:::
:::
2.1.8 解析Post传输过来的参数
:::
参数参考:
{
"object_kind": "push",
"event_name": "push",
"before": "bcd97b359136404cd754a9f784b86d7ba3fd780c",
"after": "ae5870697653a1e803559a5aeae7675a22de9bd0",
"ref": "refs/heads/main",
"ref_protected": true,
"checkout_sha": "ae5870697653a1e803559a5aeae7675a22de9bd0",
"message": null,
"user_id": 1,
"user_name": "Administrator",
"user_username": "root",
"user_email": "",
"user_avatar": "https://192.168.199.143:8880/uploads/-/system/user/avatar/1/avatar.png",
"project_id": 1,
"project": {
"id": 1,
"name": "test",
"description": null,
"web_url": "https://192.168.199.143:8880/root/test",
"avatar_url": null,
"git_ssh_url": "git@192.168.199.143:root/test.git",
"git_http_url": "https://192.168.199.143:8880/root/test.git",
"namespace": "Administrator",
"visibility_level": 0,
"path_with_namespace": "root/test",
"default_branch": "main",
"ci_config_path": null,
"homepage": "https://192.168.199.143:8880/root/test",
"url": "git@192.168.199.143:root/test.git",
"ssh_url": "git@192.168.199.143:root/test.git",
"http_url": "https://192.168.199.143:8880/root/test.git"
},
"commits": [
{
"id": "ae5870697653a1e803559a5aeae7675a22de9bd0",
"message": "Update file test",
"title": "Update file test",
"timestamp": "2023-09-18T06:41:30+00:00",
"url": "https://192.168.199.143:8880/root/test/-/commit/ae5870697653a1e803559a5aeae7675a22de9bd0",
"author": {
"name": "Administrator",
"email": "[REDACTED]"
},
"added": [
],
"modified": [
"test"
],
"removed": [
]
},
{
"id": "ff6ce3c3628d22befc964a0ec7b8c74ae3339406",
"message": "test",
"title": "test",
"timestamp": "2023-09-18T06:40:03+00:00",
"url": "https://192.168.199.143:8880/root/test/-/commit/ff6ce3c3628d22befc964a0ec7b8c74ae3339406",
"author": {
"name": "Administrator",
"email": "[REDACTED]"
},
"added": [
],
"modified": [
"test"
],
"removed": [
]
},
{
"id": "bcd97b359136404cd754a9f784b86d7ba3fd780c",
"message": "test",
"title": "test",
"timestamp": "2023-09-18T06:37:59+00:00",
"url": "https://192.168.199.143:8880/root/test/-/commit/bcd97b359136404cd754a9f784b86d7ba3fd780c",
"author": {
"name": "Administrator",
"email": "[REDACTED]"
},
"added": [
],
"modified": [
"test"
],
"removed": [
]
}
],
"total_commits_count": 3,
"push_options": {
},
"repository": {
"name": "test",
"url": "git@192.168.199.143:root/test.git",
"description": null,
"homepage": "https://192.168.199.143:8880/root/test",
"git_http_url": "https://192.168.199.143:8880/root/test.git",
"git_ssh_url": "git@192.168.199.143:root/test.git",
"visibility_level": 0
}
}
三、脚本相关–待完善
echo $CHECKOUT_SHA
rm -rf Source
mkdir Source
cd Source
git clone $SSH_URL -b test3
dir_name=$(echo $(basename $SSH_URL) | awk -F ".git" '{print $1}')
echo $dir_name
cd $dir_name
git cherry-pick $CHECKOUT_SHA
git push origin HEAD:refs/heads/test3
description="push_success"
timeout 10 curl -s -X POST "${BUILD_URL}submitDescription?description=${description}" --user root:***********************************************|| true
四、配置及使用过程中问题总结
- Gitlab配置webhook报错:Urlis blocked: Requests to the local netwo…解决
Gitlab配置webhook报错:Urlis blocked: Requests to the local netwo…解决 - 墨天轮 (modb.pro)
- 获取post参数内容
Generic Webhook Trigger插件 - jiaxzeng - 博客园 (cnblogs.com)
- jenkins报错Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password) 的处理
jenkins报错Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password) 的处理_51CTO博客_jenkins启动报错