Jenkins 实现 GitLab 特定分支提交时自动化构建

一、GitLab 配置

  1. 给 GitLab 配置 Jenkins 的部署秘钥(公钥,用 root 登录 GitLab)
    Jenkins 实现 GitLab 特定分支提交时自动化构建_第1张图片

    Jenkins 实现 GitLab 特定分支提交时自动化构建_第2张图片

      注意:如果配置了 Jenkins-slave ,项目在哪个 slave 上进行构建 就需要把哪个 slave 的公钥配置到 GitLab 的部署秘钥中

  2. 新建的项目中启用对应的部署秘钥
    Jenkins 实现 GitLab 特定分支提交时自动化构建_第3张图片
    Jenkins 实现 GitLab 特定分支提交时自动化构建_第4张图片
    秘钥未启用是显示的 enable,启用后显示的是 disable
  3. 配置 GitLab 项目 push 事件的 web Hook(它会把事件信息发送通过URL发送给Jenkins)
    Jenkins 实现 GitLab 特定分支提交时自动化构建_第5张图片
# GitLab 发送给 Jenkins 的内容
{
    "object_kind": "push",
        "event_name": "push",
        "before": "07e1b053d4f699a13e1abfdddbd7c53e7df1436a",
        "after": "42b8a70696eb3357963b6f076aed49cced0d20de",
        "ref": "refs/heads/release",
        "checkout_sha": "42b8a70696eb3357963b6f076aed49cced0d20de",
        "message": null,
        "user_id": 18,
        "user_name": "xxx",
        "user_username": "liuchengsheng",
        "user_email": "[email protected]",
        "user_avatar": "https://www.gravatar.com/avatar/d640f62f6441a0d232dab26113fc1e32?s=80\u0026d=identicon",
        "project_id": 19,
        "project": {
            "id": 19,
            "name": "test",
            "description": "测试",
            "web_url": "http://code.xxxxxxx.cc/liuchengsheng/test",
            "avatar_url": null,
            "git_ssh_url": "[email protected]:liuchengsheng/test.git",
            "git_http_url": "http://code.xxxxxxx.cc/liuchengsheng/test.git",
            "namespace": "liuchengsheng",
            "visibility_level": 0,
            "path_with_namespace": "liuchengsheng/test",
            "default_branch": "master",
            "ci_config_path": null,
            "homepage": "http://code.xxxxxxx.cc/liuchengsheng/test",
            "url": "[email protected]:liuchengsheng/test.git",
            "ssh_url": "[email protected]:liuchengsheng/test.git",
            "http_url": "http://code.xxxxxxx.cc/liuchengsheng/test.git"
        },
        "commits": [
        {
            "id": "42b8a70696eb3357963b6f076aed49cced0d20de",
            "message": "six\n",
            "timestamp": "2018-06-04T11:12:10+08:00",
            "url": "http://code.xxxxxxx.cc/liuchengsheng/test/commit/42b8a70696eb3357963b6f076aed49cced0d20de",
            "author": {
                "name": "imlcs",
                "email": "[email protected]"
            },
            "added": [

                ],
            "modified": [
                "README.MD"
                ],
            "removed": [

                ]
        }
    ],
        "total_commits_count": 1,
        "repository": {
            "name": "test",
            "url": "[email protected]:liuchengsheng/test.git",
            "description": "测试",
            "homepage": "http://code.xxxxxxx.cc/liuchengsheng/test",
            "git_http_url": "http://code.xxxxxxx.cc/liuchengsheng/test.git",
            "git_ssh_url": "[email protected]:liuchengsheng/test.git",
            "visibility_level": 0
        }
}

二、Jenkins 配置

  1. 配置获取 Git 仓库的私钥(就是刚才在 GitLab 中公钥所对应的私钥)
    Jenkins 实现 GitLab 特定分支提交时自动化构建_第6张图片
  2. 安装 Generic Webhook Trigger Plugin 插件
    Jenkins 实现 GitLab 特定分支提交时自动化构建_第7张图片
  3. 新建项目(新版本 jenkins URL 配置中 token 一定要写)
    Jenkins 实现 GitLab 特定分支提交时自动化构建_第8张图片
  4. 自动构建测试,GitLab 用户在指定分支上提交代码触发 Jenkins 构建

你可能感兴趣的:(Jenkins 实现 GitLab 特定分支提交时自动化构建)