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
相关推荐
从零开始学习人工智能几秒前
深入解析 OPC UA:工业自动化与物联网的关键技术
运维·物联网·自动化
从后端到QT32 分钟前
SRS流媒体服务器(8)源码分析之rtc/rtmp互相转码详解
运维·服务器·实时音视频
CodeWithMe34 分钟前
Nginx入门进阶:从零到高手的实战指南
运维·nginx
鹏大师运维37 分钟前
在银河麒麟V10 SP1上手动安装与配置高版本Docker的完整指南
linux·运维·docker·容器·麒麟·统信uos·中科方德
QMCY_jason40 分钟前
linux 内存占用排查 vm.nr_hugepages
linux·运维·服务器
半方白1 小时前
通过 ks.cfg 文件实现 openEuler 系统自动部署
运维·自动化运维
moppol1 小时前
容器化 vs 虚拟机:什么时候该用 Docker?什么时候必须用 VM?
运维·docker·容器
ZPC82101 小时前
ubuntu 6.8.0 安装xenomai3.3
linux·运维·ubuntu
真智AI2 小时前
AI智能体时代来临:数据分析的变革与自动化之路
人工智能·数据分析·自动化
一梦浮华2 小时前
自学嵌入式 day30 IPC:进程间通信
linux·运维·服务器