大前端时代应用收藏-工具篇技术专题-中间件

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

2018-06-04  本文已影响1212人  w也不知道

一、GitLab 配置

  1. 给 GitLab 配置 Jenkins 的部署秘钥(公钥,用 root 登录 GitLab)

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

  2. 新建的项目中启用对应的部署秘钥 秘钥未启用是显示的 enable,启用后显示的是 disable
  3. 配置 GitLab 项目 push 事件的 web Hook(它会把事件信息发送通过URL发送给Jenkins)
# 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": "liuchengsheng@xxxxxxx.com",
        "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": "git@code.xxxxxxx.cc: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": "git@code.xxxxxxx.cc:liuchengsheng/test.git",
            "ssh_url": "git@code.xxxxxxx.cc: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": "15224800531@163.com"
            },
            "added": [

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

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

二、Jenkins 配置

  1. 配置获取 Git 仓库的私钥(就是刚才在 GitLab 中公钥所对应的私钥)
  2. 安装 Generic Webhook Trigger Plugin 插件
  3. 新建项目
  4. 自动构建测试,GitLab 用户在指定分支上提交代码触发 Jenkins 构建
上一篇下一篇

猜你喜欢

热点阅读