Jenkins+Github实现自动触发构建
2019-05-16 本文已影响0人
张小Di
前提:已安装Jenkins+Git,已申请Github账号
若要实现自动构建,Jenkins 需获得远程代码仓库 Github 的读取权
1. Github中获取访问token值,需要一个对项目有写权限的账户
GitHub->setting->Developer setting->Personal access tokens->点击Generate new token
内容填写如下:

创建成功后,token如下:

2. Jenkins中Github配置中添加token凭证
系统管理->添加Github server->添加Secret text凭证->连接测试
同时,勾选为github指定另外一个hook url(配置Jenkins在Hook URL中监听Github的Post请求,然后进行自动构建)

Secret text凭证添加如下:
类型需选择Secret text,Secret处输入Github中上面生成的token值

注意:在操作过程,点击连接测试,可能会提示:Failed to validate the account,首先请确保添加凭证的类型是Secret text,其次secret值填写的是步骤1中生成的token值
3. Github中项目配置webhook
settings->webhooks->Add webhook


当结果如下:(前面所做的貌似都白做了??)

因为填写的webhook是本地链接,Github无法成功访问所致,所以需要将本地发布到线上,或者通过ngrok实现内网穿透(ngrok服务器搭建)
后续再看吧,需要学习的东西还有很多啊
一分钟实现内网穿透(ngrok服务器搭建)
小Di在此投机取巧,间接 实现 了自动化部署(实际上并没有实现 ),操作如下:
前提:Jenkins已经安装Github插件
在任务->设置Configure->Build Trigger中勾选【Github hook trigger for GITScm polling】和【poll SCM】,并设置定时轮询,当检测到有新的提交时会自动构建当前任务,如果没有新的提交则不会触发。
