gitlab利用webhook实现push代码后jenkins自动构建之Linux服务器

1.概述

利用Jenkins可以实现构建,但每次都需要人为的去执行一次“立即构建”,这样显然不方便。利用gitlab的webhook,当每次push后会触发jenkins,自动构建至目标服务器上。
gitlab服务器:10.11.1.29
Jenkins服务器:10.11.1.5
目标服务器:10.11.1.30

2.实现步骤

2.1.配置连接Gitlab的SSH Key

需要通过jenkins用户去生成SSH Key,不然在创建Job时,会产生如下错误:

Failed to connect to repository : Command "git ls-remote -h git@10.11.1.29:aorise/test.git HEAD" 
returned status code 128: stdout: stderr: Host key verification failed. 
fatal: Could not read from remote repository. 
Please make sure you have the correct access rights and the repository exists.

因为jenkins连接Gitlab是通过jenkins这个账户的
通过jenkins账户生成SSH Key
切换账户至jenkins

su jenkins

发现没用
改用

sudo su -s /bin/bash jenkins
bash-4.2$ 

生成SSH Key

bash-4.2$ ssh-keygen -t rsa -f /var/lib/jenkins/.ssh/id_rsa
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /var/lib/jenkins/.ssh/id_rsa.
Your public key has been saved in /var/lib/jenkins/.ssh/id_rsa.pub.
The key fingerprint is:
67:4d:e6:73:96:53:c1:de:79:e8:6c:cc:00:93:c5:de jenkins@i-4720eba4
The key's randomart image is:
+--[ RSA 2048]----+
| +. .. |
| + . ..|
| +o...+|
| =o E=o|
| S o +*= .|
| o +*. |
| . |
| |
| |
+-----------------+

将id_rsa.pub里字符串拷贝只Gitlab的SSH Keys里
测试是否成功

ssh -T git@10.11.1.29

见到如下字样,则成功
Welcome to GitLab, aorise!(aorise是Gitlab上的用户)

2.2.安装插件

需要安装的插件:Gitlab Hook Plugin和Build Authorization Token
安装好重启jenkins,或者不重启也行
如果没有安装Build Authorization Token,则在Test hook时会报403错误

2.3.随机生成Token

openssl rand -hex 12
30910262eff664f2acbcb962

2.4.创建Job

在jenkins首页点击
==新建
==输入item name # 比如auto-test
==选中“构建一个自由风格的软件项目”
==源码管理,选中Git,在Repository URL处输入git@10.11.1.29:aorise/test.git
==Add Credential,点击“Add”->选中“jenkins” ,在弹出的界面上,Kind栏选中“SSH Username with private key”,Username栏输入名称,比如jenkins-test,Private Key处选中“Enter directly”,在Key输入框中,输入/var/lib/jenkins/.ssh/id_rsa里的字符创,点击“Add”添加即可
==构建触发器,勾上“触发远程构建(例如,使用脚本)”,在身份验证令牌栏输入随机生成的Token:30910262eff664f2acbcb962,
==构建触发器,勾上“GitHub hook trigger for GITScm polling”
==构建,选中“Execute shell”,在Commend栏输入如下脚本:

#!/bin/bash
SOURCE_DIR=/var/lib/jenkins/workspace/${JOB_NAME}/
DEST_DIR=/mnt/test/
REMOTE_IP=10.11.1.30
sudo /usr/bin/rsync -e "ssh -p 22" -avpgolr --exclude=.git $SOURCE_DIR root@$REMOTE_IP:DEST_DIR

==保存

2.5.配置钩子
登录Gitlab,配置钩子,不同版本的Gitlab,钩子形式可能不一样,有的是Webhooks,有的是System Hooks,我本机上的就是后者
==进入Gitlab
==进入test
==点击“System Hooks”,在弹出的界面中,URL栏输入http://10.11.1.5:8080/buildByToken/build?job=auto-test&token=
30910262eff664f2acbcb962
URL格式为http://jenkins服务器地址:8080/buildByToken/build?job=job名称&token=token
勾选“Push events”
==保存
==点击“Test hook”,如果返回“Hook executed successfully:HTTP 201”,则配置成功

2.6.push代码

在git库中添加文件,然后push,看看目标服务器(10.11.1.30)/mnt/test/下是否多了刚才添加的文件,如果看到了,则配置成功,如果以后需要发布代码,只需push即可,jenkins会构建到目标服务器上的

Started by remote host 10.11.1.29
Building in workspace /var/lib/jenkins/workspace/auto-test
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url git@10.11.1.29:aorise/test.git # timeout=10
Fetching upstream changes from git@10.11.1.29:aorise/test.git
 > git --version # timeout=10
using GIT_SSH to set credentials 
 > git fetch --tags --progress git@10.11.1.29:aorise/test.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 3ba8194838ce7fd0133459a72021072fca97f504 (refs/remotes/origin/master)
Commit message: "tttt"
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 3ba8194838ce7fd0133459a72021072fca97f504
 > git rev-list 3ba8194838ce7fd0133459a72021072fca97f504 # timeout=10
[auto-test] $ /bin/bash /tmp/jenkins4533238182324155783.sh
Warning: Permanently added '10.11.1.30' (ECDSA) to the list of known hosts.
building file list ... done
sent 116 bytes received 12 bytes 256.00 bytes/sec
total size is 36 speedup is 0.28
Finished: SUCCESS

参考地址:
http://www.cnblogs.com/shansongxian/p/6605623.html
http://www.cnblogs.com/reblue520/p/7146638.html
https://wiki.jenkins.io/display/JENKINS/Build+Token+Root+Plugin
git钩子:
https://git-scm.com/book/zh/v2/%E8%87%AA%E5%AE%9A%E4%B9%89-Git-Git-%E9%92%A9%E5%AD%90#_git_hooks

    原文作者:四冶
    原文地址: https://www.jianshu.com/p/4eac43872b40
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞