周期性构建
概念:周期性构建这是—-种基于 cron 类型的构建机制.按照预定义的时间周期性启动作务
- 对于期望能够基于代码变更进行触的CI场景来说,周期性构建并非其最佳选项,但对于有些类型的住务,它却也能够通过精心编排的周期性构建来避免资源冲突;
- 周期性构建分为定时构建和轮询构建
- 定时构建: 按时间周期性的触发构建
- 轮询SCM(Source Code Management): 指的是定期到代码仓库检查代码是否有变更,存在代码变更时就运行pipeline;为了能够从CI中得到更多的收益,轮询操作越频繁越好;显然,这会给SCM带去无谓的压力,所以构建的触发由SCM负责通知Jenkins最为理想;但在外部的SCM无法通知到局域网中的Jenkins时,可以采轮询SCM方式倒也不失为一种选择
案例:实现定时构建
- 新建一个任务
- 设置构建周期
- 创建后每分钟构建一次
案例:实现SCM 构建
- 选择轮询SCM构建
- 观察Git 轮询日志可以看到当有变化时才会构建,否则不会执行构建
Git未发生变化时
Git未发生变化时,Git 轮询日志捕捉到变化即刻开始自动构建
[root@gitlab data]#cd http_demo_go/
[root@gitlab http_demo_go]#git branch
* master
[root@gitlab http_demo_go]#tree .
.
├── go.mod
├── hello.html
└── main.go
0 directories, 3 files
[root@gitlab http_demo_go]#vim main.go
[root@gitlab http_demo_go]#git commit -am 'v1.0'
[master 4d0fa92] v1.0
Committer: root <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:
git config --global --edit
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
1 file changed, 1 insertion(+), 1 deletion(-)
[root@gitlab http_demo_go]#git tag v1.0
[root@gitlab http_demo_go]#git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 280 bytes | 280.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
To gitlab.mooreyxia.org:myprj/http_demo_go.git
99131f2..4d0fa92 master -> master
[root@gitlab http_demo_go]#git log
commit 4d0fa926dbba5f94923fbf98eeb6793553d465d8 (HEAD -> master, tag: v1.0, origin/master, origin/HEAD)
Author: root <[email protected]>
Date: Sun Feb 19 08:20:41 2023 +0000
v1.0
commit 99131f252ad74aee49aaffbe4fae7e5beea5fee8
Author: 南山 <[email protected]>
Date: Sun Jul 24 11:52:46 2022 +0800
v1.0
构建 Webhook 触发器
概念:构建触发器(webhook),也称为钩子,实际上是一个HTTP回调,其用于在开发人员向gitlab提交代码后能够触发jenkins自动执行代码构建操作
常见应用场景:只有在开发人员向develop分支提交代码的时候会自动触发代码构建和部署至测试环境,而向主分支提交的代码不会自动构建,需要运维人员手动部署代码到生产环境。
可以使用下面两种方式实现 Webhook 触发构建
- 触发远程构建: 此方式无需安装插件
- Build when a change is pushed to GitLab. GitLab webhook URL: 需要安装插件
案例:触发远程构建
设置触发远程构建
#设置令牌
JENKINS_URL/job/Http-Demo-Go-WebHook-01/build?token=TOKEN_NAME
JENKINS_URL:指当前jenkins地址 http://jenkins.mooreyxia.org:8080/
token:设置一个随机值,建议使用 openssl rand -base64 或者 openssl rand -hex 进制生成
[root@jenkins ~]#openssl rand -hex 10
c93e2fa79c33ff489b26
http://jenkins.mooreyxia.org:8080/job/Http-Demo-Go-WebHook-01/build?token=c93e2fa79c33ff489b26
保存构建后验证触发远程构建
#如果未登录用户则被拒绝
[root@web01 ~]#curl http://jenkins.mooreyxia.org:8080/job/Http-Demo-Go-WebHook-01/build?token=c93e2fa79c33ff489b26
<html><head><meta http-equiv='refresh' cnotallow='1;url=/login?from=%2Fjob%2FHttp-Demo-Go-WebHook-01%2Fbuild%3Ftoken%3Dc93e2fa79c33ff489b26'/><script>window.location.replace('/login?from=%2Fjob%2FHttp-Demo-Go-WebHook-01%2Fbuild%3Ftoken%3Dc93e2fa79c33ff489b26');</script></head><body style='background-color:white; color:white;'>
Authentication required
<!--
-->
</body></html> [root@web01 ~]#
#使用账户触发远程调用 -> 使用授权账户,不建议用管理账户
[root@web01 ~]#curl http://admin:[email protected]:8080/job/Http-Demo-Go-WebHook-01/build?token=c93e2fa79c33ff489b26
注意:Jenkins 配置生成用户的 API Token,使用Token调用Webhook更安全
创建用户并生成用户Token
用户登陆后自己修改用户配置---添加API Token
注意: 此值是一次性的,所以必须立即复制Token
验证使用用户Token触发远程构建
[root@web01 ~]#curl http://moore:[email protected]:8080/job/Http-Demo-Go-WebHook-01/build?token=c93e2fa79c33ff489b26
接下来我们设置gitlab项目发生变化时自动向Jenkins发送Webhook请求
设置Webhook功能前需要Gitlab打开外发请求
创建Webhook
测试
提交代码,确认自动触发构建
[root@gitlab http_demo_go]#vim main.go
[root@gitlab http_demo_go]#git commit -am 'v2.0'
[master c039b03] v2.0
Committer: root <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:
git config --global --edit
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
1 file changed, 1 insertion(+), 1 deletion(-)
[root@gitlab http_demo_go]#git tag v2.0
[root@gitlab http_demo_go]#git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 278 bytes | 139.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
To gitlab.mooreyxia.org:myprj/http_demo_go.git
4d0fa92..c039b03 master -> master
- 另外一种利用插件实现Webhook功能的方式
Jenkins安装gitlab插件
设置webhook构建模式
生成Token
#得到Webhook地址和令牌
http://jenkins.mooreyxia.org:8080/project/Http-Demo-Go-WebHook-02
f1937609a26ecc720f7d45bc02f861ad
之后保存设置即可。
接下来将上面得到的Webhook地址和令牌绑定到Gitlab对应项目的webhook就可以了
提交代码,确认自动触发构建
[root@gitlab http_demo_go]#vim main.go
[root@gitlab http_demo_go]#git commit -am 'v4.0'
[master defa0a8] v4.0
Committer: root <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:
git config --global --edit
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
1 file changed, 1 insertion(+), 1 deletion(-)
[root@gitlab http_demo_go]#git tag v4.0
[root@gitlab http_demo_go]#git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 277 bytes | 277.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
To gitlab.mooreyxia.org:myprj/http_demo_go.git
2fe751f..defa0a8 master -> master
最后简单提一下可以设置任务上下游构建关系,即A->B->C类似的任务构建流程
我是moore,大家一起加油!!!
标签:git,http,CICD,gitlab,构建,Jenkins,go,64,root From: https://blog.51cto.com/mooreyxia/6066816