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
相关推荐
Tim风声(网络工程师)10 小时前
排查内网互联网访问流程
运维·服务器·网络
坚持就完事了11 小时前
Linux中如何添加环境变量
linux·运维·服务器
AC赳赳老秦11 小时前
政企内网落地:OpenClaw 离线环境深度适配方案,无外网场景下本地化模型对接与全功能使用
java·大数据·运维·python·自动化·deepseek·openclaw
l1t11 小时前
mingw和Linux中的gcc和llvm编译器编译的pocketpy执行同一个python脚本的不同效果
linux·运维·python
ITyunwei098712 小时前
团队管理与人才发展:如何打造一支“召之即来,来之能战”的铁军?
大数据·运维·人工智能
木心术112 小时前
如何使用AI agent基于产品技术手册和标准协议完成FPGA寄存器的自动化配置、代码修改和编译的完整方案
人工智能·fpga开发·自动化
白緢12 小时前
一、Linux 基础入门
linux·运维·服务器
Deepoch12 小时前
面向工业现场自主运维:Deepoc 具身模型开发板的端侧智能升级路径
运维·人工智能·科技·巡检机器人·deepoc
菜菜艾13 小时前
自动化环境补丁更新系统
linux·运维·bash·运维开发
江上清风山间明月13 小时前
Nginx基于域名区分的多网站部署
运维·nginx·部署·php·多个网站