jenkins+gogs自动打包事件流程如下:
1)用户通过git提交或者更新代码至gogs仓库;
2)gogs仓库中配置了连携jenkins的web钩子,触发jenkins中的job;
3)jenkins中job执行(拉取gogs仓库中的更新后代码包,执行打包操作);
1.要件准备
1)jenkins环境中安装git
安装git
# yum install -y git
# whereis git
git: /usr/bin/git /usr/share/man/man1/git.1.gz
配置git目录:Dashboard--Manage Jenkins--Global Tool Configuration--Git--Path to Git executable:/usr/bin/git--Save
2)jenkins环境中追加Gogs插件
Dashboard--Manage Jenkins--Plugin Manager
确认Gogs插件已安装,如果没有安装的添加安装。
3)jenkins中添加Credentials
Dashboard--Manage Jenkins--Credentials--System--Global credentials (unrestricted)中选择Add Credentials.
输入登录gogs的用户和密码创建一个credentials
2.jenkins中创建一个Job
Dashboard--New Item--输入Item name--选择Freestyle project--点击OK
配置Job:Configure--General--Source Code Management--Git--输入Repository URL--选择登录gogs的用户Credentials--选择Branch----Svae;
--Build Triggers--选择Trigger builds remotely (e.g., from scripts),输入Authentication Token--选择Build when a change is pushed to Gogs
--Build Steps--Execute shell--输入需要后续执行的命令--Save
--Buld Now检测Job执行情况
3.gogs配置web钩子
选择我的仓库--仓库设置--管理Web钩子--添加Web钩子--选择Gogs--填写推送地址、选择数据格式、密钥文本可免、设置希望触发Web钩子的事件、选择激活--点击添加Web钩子
测试推送
4.测试push事件
1)配置Credentials
配置Credentials
用户登录Gogs--用户设置--SSH 密钥 --增加密钥
--进入git客户端目录「 ~/.ssh」--确认目录下是否已有证书
$ cd ~/.ssh
Administrator@DESKTOP-6NEL3HC MINGW64 ~/.ssh
$ ll
total 13
-rw-r--r-- 1 Administrator 197121 2602 Nov 29 17:30 id_rsa
-rw-r--r-- 1 Administrator 197121 570 Nov 29 17:30 id_rsa.pub
-rw-r--r-- 1 Administrator 197121 926 Jan 15 17:37 known_hosts
如果不存在,则创建
$ ssh-keygen -t rsa -C "luorunfan@qq.com"
查看公钥信息
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDB2bpQa1VDF2VjjhLQFf9HlySrmI2gGaj2Q0P/Ce6nLmtu8EvWOkhVnNsvEgzwEgxOf9oWQI+3QQeYFOvmpuYiDuGj9PVdGCUKn5R/3u/LzibG2aos2qZjhmgadZB1qBOsHjYlsP5/yYkCT9b/SjEStojbdknO9CI6M7HszZoZQHE4c9uh1n6MkYEsxM6xJDpvjJsMRSrvGzHepYVYsJ7h7i2sQagm4K+ELObgUianK7ipGJNG3UXM+jVPX+tXmQ0l7GY5VNIEY6NXVo0dd4HwMnU8uFOjyVOkEniBgvvVTNduurjMknp80PIkH8qtv1ImYs/6R5IsFRnrXL2zVcUalgF9JHVbDZunld5SuXxJzFM1TNdyekj5pD7C9O+LXJTujpo3anDHSQgfDxCXrp2kVYOtobFAaPSz0K2bKirPbdC1l+vSUFYmYknH2BrC66W/q0WQhsz+y+98jiYw+esVeaO+2mWMgIgPz97liZxcPtOxS5DBUCjYvthKJhbyoJE= luorunfan@qq.com
2) Push代码
$ git clone http://172.26.37.127:3000/luorf/CMDB.git
$ cd CMDB
$ vi README.md
$ git add *
$ git commit -m 'jenkins job'
$ git push origin master
查看gogs的Web钩子最近推送记录,确认推送成功;
查看jenkins job执行情况,确认推送成功;
Started by remote host 172.26.37.127
Running as SYSTEM
[EnvInject] - Loading node environment variables.
Building in workspace /var/lib/jenkins/workspace/onejob
The recommended git tool is: NONE
using credential d8bf0db3-88d4-418e-8ace-d75c834c18a2
> /usr/bin/git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/onejob/.git # timeout=10
Fetching changes from the remote Git repository
> /usr/bin/git config remote.origin.url http://172.26.37.127:3000/luorf/CMDB.git # timeout=10
Fetching upstream changes from http://172.26.37.127:3000/luorf/CMDB.git
> /usr/bin/git --version # timeout=10
> git --version # 'git version 2.31.1'
using GIT_ASKPASS to set credentials gogs login
> /usr/bin/git fetch --tags --force --progress -- http://172.26.37.127:3000/luorf/CMDB.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> /usr/bin/git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision 6dd3e5cff20c70f6e6143458e90bfcb6ef0fd40a (refs/remotes/origin/master)
> /usr/bin/git config core.sparsecheckout # timeout=10
> /usr/bin/git checkout -f 6dd3e5cff20c70f6e6143458e90bfcb6ef0fd40a # timeout=10
Commit message: "cmdb firsh"
> /usr/bin/git rev-list --no-walk 6dd3e5cff20c70f6e6143458e90bfcb6ef0fd40a # timeout=10
[onejob] $ /bin/sh -xe /tmp/jenkins11963501685487794640.sh
++ date +%Y%m%d%H%M
+ tar zcvf cmdb.202301291846.tar.gz ./cmdb.202301171407.tar.gz ./db.sqlite3 ./local_cmdb ./manage.py ./README.md ./static
./cmdb.202301171407.tar.gz
./db.sqlite3
...
./static/import_export/action_formats.js
++ date +%Y%m%d%H%M
+ mv ./cmdb.202301291846.tar.gz ../
Finished: SUCCESS
问题解决
1)Gog管理Web钩子提示:推送 URL 被解析到默认禁用的本地网络地址。
解决办法:
修改gogs安全设置
# vi /home/git/gogs/custom/conf/app.ini
在[security]中添加如下内容
LOCAL_NETWORK_ALLOWLIST = 172.26.37.127,172.26.37.128
##### 添加允许本地地址,hostname或者ip address
重启gogs
# systemctl restart gogs
提醒:从gogs 0.12.10开始支持「LOCAL_NETWORK_ALLOWLIST = *」。