自建git仓库 搭配jenkins插件 Generic Webh
2020-07-21 本文已影响0人
张建勇9511
1.git仓库搭建参考
https://www.runoob.com/git/git-server.html
2.生成ssh key
windows(开发使用)
打开git bash
输入
ssh-keygen -t rsa -C "你的邮箱地址"
生成秘钥,默认在用户目录下的.ssh文件夹内

linux(jenkins使用)
在命令行内输入,默认在当前用户目录下的.ssh文件夹内
ssh-keygen -t rsa -C "你的邮箱地址"

3.配置公钥
id_rsa.pub文件用文本编辑器打开,复制出来
服务器切换到git用户,vi打开
vi ~/.ssh/authorized_keys
将复制出来的公钥,粘贴进去,一行一个

4.配置jenkins凭据
添加一个凭据

username :生成key时配置的邮箱
Passphrase:密码
Private Key:私钥,在.ssh目录下的id_rsa文件内,复制出来
5.jenkins项目配置
5.1 jenkins 下载插件 Generic Webhook Trigger Plugin

5.2 源码管理

配置仓库地址
验证需要刚刚添加的凭证
5.3 触发构建,配置webhook
构建触发器下的 Generic Webhook Trigger 打钩

token自己设置一个
5.4 配置git hook
在服务器上创建的仓库目录下有个hooks目录,里面有很多.sample文件都是案例文件
我们的目的是push代码触发构建,需要使用到的钩子是 post-receive,在hooks目录下新建一个名字为 post-receive的脚本文件

脚本内容
#!/bin/bash
#########
# git hooks
# by zhangjianyong 2020年7月21日
#########
HOOKS_LOG="/mnt/data/git/gitrepo/bimuyu-api.git/hooks/log/hooks.log"
echo "代码提交触发:" `date "+%y-%m-%d %H:%M:%S"` >> $HOOKS_LOG
while read oldrev newrev ref
do
echo "当前触发分支:"$ref >> $HOOKS_LOG
curl -i -X POST -H "Content-Type: application/json" -d "{\"ref\":\"$ref\"}" http://xxx.xxx.xxx/generic-webhook-trigger/invoke?token=xxxx>> $HOOKS_LOG
done
保存脚本,添加可执行权限
chmod +x post-receive
提交代码后,就会触发该脚本,通知jenkins触发构建