Gitlab钩子提交合并代码触发Jenkins构建

Gitlab钩子提交合并代码触发Jenkins构建

一、配置安装

1.1 Gitlab安装

GitLab下载安装_GitLab最新中文免费版下载安装-极狐GitLab

1.2 Jenkins安装

《Jenkins安装总结》

1.3 安装问题总结

  1. Gitlab和Jenkins安装在同一台机器上时会有端口占用问题,Jenkins和Gitlab都会用到8080端口,需要对其中一个工具进行配置文件的端口更改

GitLab修改端口_gitlab 修改端口_热爱技术的小陈的博客-CSDN博客

二、触发流程配置及问题总结

2.1 配置流程

:::
2.1.1 Jenkins需要安装gitlab插件(Generic Webhook Trigger)
:::

image

:::
2.1.2 在Jenkins中勾选gitlab插件选项
:::

image

:::
2.1.3 进入需要监听的项目
:::

image

:::
2.1.4 点击"Settings"下的"Webhooks"
:::

image

:::
2.1.5 点击"Add new webhook"
:::

image

:::
2.1.6 配置URL及token,配置好之后点击"Sava changes"
:::

gitlab:

image

选择需要监控的分支:

image

Jenkins:

image

image

:::
2.1.7 点击Test的push操作,测试通过
:::

image

:::
2.1.8 解析Post传输过来的参数
:::

image

参数参考:

{
  "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

四、配置及使用过程中问题总结

  1. Gitlab配置webhook报错:Urlis blocked: Requests to the local netwo…解决

Gitlab配置webhook报错:Urlis blocked: Requests to the local netwo…解决 - 墨天轮 (modb.pro)

  1. 获取post参数内容

Generic Webhook Trigger插件 - jiaxzeng - 博客园 (cnblogs.com)

  1. jenkins报错Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password) 的处理

jenkins报错Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password) 的处理_51CTO博客_jenkins启动报错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值