Jenkins-自动化

定时构建

使用Cron表达式指定执行时间。

bash 复制代码
# 格式
# ┌──分(0 - 59)
# │ ┌──时(0 - 23)
# │ │ ┌──日(1 - 31)
# │ │ │ ┌─月(1 - 12)
# │ │ │ │ ┌─星期(0 - 6,表示从周日到周六)
# │ │ │ │ │
# *  *  *  * 
# 每日 01:00
0 1 * * * 
# 这个时候Jenkins 会提示我们使用下面的语法H 1 * * *
# 可能有大量的job 被设定在每日01:00执行
#  H - jenkins可以自动安排执行顺序,减轻压力
* * * * * # 每分钟执行 这个时候我们要忽略下面的提示我们想要的就是每分钟执行
# Do you really mean "every minute" when you say "* * * * *"? Perhaps you meant "H * * * *" to poll once per hour

外部触发

首先安装插件:

2 我们通过发送

安装插件以后我们获取

bash 复制代码
# 获取crumb
curl -u "jenkins:jenkins" 'http://ip/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)'
# Jenkins-Crumb:dcaad0c0555a9369b2874d4a6d8870b9f55d34510e9b2d537ef4cbd905ff7825

# 保存crumb
crumb=$(curl -u "jenkins:1234" -s 'http://10.0.0.215:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
#触发Jobs
curl -u "jenkins:1234" -H "$crumb" -X POST http://ip/job/maven-job/build?delay=0sec

Git server hooks

首先进入仓库:

Gitlab默认使用了hashed storage,可以在 Main menu > Admin > Overview > Projects下

选中项目,找到 Gitaly relative path 然后区对应的路,创建钩子文件。

Git server hooks可以定义 pre-receive, post-receive, update 等多种 Git server-side hooks

  • applypatch-msg
  • pre-applypatch
  • post-applypatch
  • pre-commit
  • prepare-commit-msg
  • commit-msg
  • post-commit
  • pre-rebase
  • post-checkout
  • post-merge
  • pre-receive
  • update
  • post-receive
  • post-update
  • pre-auto-gc
  • post-rewrite
  • pre-push
  • 如果只创建单个的server hook,创建一个名称与hook类型相符的文件。例如,对于一个预接收的server hook,文件名应该是pre-receive,没有扩展名。
  • 如果要创建多个server hook,为hook创建一个与hook类型相匹配的目录。例如,对于一个预接收的server hook,目录名称应该是pre-receive.d
bash 复制代码
# 新建custom_hooks目录
mkdir custom_hooks
cd  custom_hooks
vi post-receive

post-receive中写入,刚刚crumbIssuer的方式

bash 复制代码
#! /bin/bash
crumb=$(curl -u "jenkins:1234" -s 'http://10.0.0.215:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')

curl -u "jenkins:1234" -H "$crumb" -X POST http://10.0.0.215:8080/job/maven-job/build?delay=0sec

保存,赋予执行权限

bash 复制代码
chmod u+x post-receive
相关推荐
m0_7417688536 分钟前
使用docker的小例子
运维·docker·容器
学习3人组1 小时前
CentOS 中配置 OpenJDK以及多版本管理
linux·运维·centos
厨 神2 小时前
vmware中的ubuntu系统扩容分区
linux·运维·ubuntu
Karoku0662 小时前
【网站架构部署与优化】web服务与http协议
linux·运维·服务器·数据库·http·架构
geek_Chen012 小时前
虚拟机共享文件夹开启后mnt/hgfs/下无sharefiles? --已解决
linux·运维·服务器
(⊙o⊙)~哦2 小时前
linux 解压缩
linux·运维·服务器
寻爱的希斯克利夫3 小时前
tomcat 配置jenkins_home 目录
servlet·tomcat·jenkins
最新小梦3 小时前
Docker日志管理
运维·docker·容器
鸡鸭扣4 小时前
虚拟机:3、(待更)WSL2安装Ubuntu系统+实现GPU直通
linux·运维·ubuntu
友友马5 小时前
『 Linux 』HTTP(一)
linux·运维·服务器·网络·c++·tcp/ip·http