配置 Jenkins 工程
打开 Jenkins 工程的配置页面,找到 构建触发器 的部分,勾选 Build when a change is pushed to GitLab 那一行:
需要记下 Build when a change is pushed to GitLab 那一行中, GitLab CI Service URL: 后面的 URL,后面在配置 GitLab 工程时需要用到。
还要点开右下角的 高级 :
随后点击右下角的 Generate 按钮,生成 Secret token ,保存这里生成的 Secret token,它同样将用于后面 GitLab 的配置。随后点击左下角的 保存 按钮,保存前面所做的配置。
配置 GitLab 工程
创建一个新的或选择一个已有的 GitLab 工程。然后选择 Settings -> Integrations ,在 URL 一栏中输入前面保存的 GitLab CI Service URL ,在 Secret Token 一栏中输入前面保存的 Secret token ,然后选择需要 trigger Jenkins 工程执行构建的事件:
点击绿色的 Add webhook 按钮,完成 webhook 的创建。
创建好了 webhook 之后,点击 Test 下拉框中的 Push events ,如下图:
可以手动产生事件,触发 Jenkins 工程。点击 Edit ,在 webhook 的编辑页面,拉到页面底部,还可以看到,该 webhook 最近的调用情况,如下图:
点开特定调用的 View details ,还可以看到这次调用的详细情况,如下图:
由此不难理解,GitLab trigger Jenkins 工程,主要是通过向 Jenkins 服务器发送一个 POST 请求实现的。
验证测试
修改我们的 GitLab 测试工程中的文件,并 push 到 GitLab 服务器上:
test_for_gerrit git:(master) echo "Hello, world" >> read2.md test_for_gerrit git:(master) git status On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: read2.md no changes added to commit (use "git add" and/or "git commit -a") test_for_gerrit git:(master) git add . test_for_gerrit git:(master) git commit -m "Add test data." test_for_gerrit git:(master) git push warning: push.default is unset; its implicit value has changed in Git 2.0 from 'matching' to 'simple'. To squelch this message and maintain the traditional behavior, use: git config --global push.default matching To squelch this message and adopt the new behavior now, use: git config --global push.default simple When push.default is set to 'matching', git will push local branches to the remote branches that already exist with the same name. Since Git 2.0, Git defaults to the more conservative 'simple' behavior, which only pushes the current branch to the corresponding remote branch that 'git pull' uses to update the current branch. See 'git help config' and search for 'push.default' for further information. (the 'simple' mode was introduced in Git 1.7.11. Use the similar mode 'current' instead of 'simple' if you sometimes use older versions of Git) Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 312 bytes | 0 bytes/s, done. Total 3 (delta 1), reused 0 (delta 0) To ssh://git@g.hz.netease.com:22222/cloudgame/test_for_gerrit.git 23660c1..e86152a master -> master test_for_gerrit git:(master) |
随后迅速地就能在 Jenkins 中,测试工程主页面的左下方,看到由 GitLab push 所 trigger 起来的构建任务:
在由 GitLab push 所 trigger 起来的构建任务的下方,会显示构建任务是由谁 push 的代码所 trigger 起来的。打开特定构建任务的 控制台输出 可以看到构建的详细过程:
上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。