目录

jenkins + docker + git 持续集成

git push 以后, jenkins 自动触发 代码打包,生成docker image , docker push 到 仓库,发布到环境里。

安装jenkins

这里不建议用 Docker 镜像,因为下面 Jenkins 自身会需要调用 Docker 来启动任务。

导入 jenkins 源

1
2
3
4
5
6

wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins.io/redhat-stable/jenkins.repo
rpm --import http://pkg.jenkins.io/redhat-stable/jenkins.io.key


yum -y install jenkins 

修改jenkins配置

1
2
3
4
5
6
7
vi /etc/sysconfig/jenkins

# 修改jenkins 目录
JENKINS_HOME="/opt/jenkins"

# 修改jenkins 端口
JENKINS_PORT="9999"

移动目录

1
2
# 将目录移动过来,否则程序报错
mv /var/lib/jenkins /opt/

启动服务

1
2
systemctl start jenkins
systemctl enable jenkins

访问WEB UI

1
http://myip:9999/ 

生成密钥

1
2
3
4
5
6
7
8
9
# 切换用户
su jenkins


# 生成key
ssh-keygen -t rsa -b 4096 -C "jenkins@git"

# 查看key信息
cat /home/jenkins/.ssh/id_rsa.pub

jenkins 后台配置

进入 jenkins –> Credentials –> Add Credentials https://jicki.cn/img/posts/jenkins/4.png

选择 系统管理 – > 管理插件

  1. 添加 Gradle Plugin 插件
  2. 添加 Git plugin 插件
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
常用插件

Build WIth Parameters   # 执行 构建 前手工输入参数

pipeline

Deploy Plugin   # build war 包以后部署

Email Extension Plugin  # 邮件发送

Multiple SCMs Plugin # 多项目构建工具

Git Parameter  # 构建的时候选择 git 分支

description setter plugin  # 配置 Build History 显示具体信息

user build vars plugin  # 显示 构建用户名 而非 id

下载慢可直接下载 hpi 文件,通过高级 导入插件安装

选择 系统管理 – > Global Tool Configuration

安装JDK

https://jicki.cn/img/posts/jenkins/1.png

安装 Gradle

https://jicki.cn/img/posts/jenkins/2.png

安装 Git

https://jicki.cn/img/posts/jenkins/3.png

创建项目 选择 自由风格 的项目

源码管理选择 Git

https://jicki.cn/img/posts/jenkins/5.png

构建 选择 Invoke Gradle script

https://jicki.cn/img/posts/jenkins/6.png

构建触发器

1
2
3
4
# 勾选 Poll SCM

# 每两分钟检查一次git代码是否有更新
H/2 * * * *

配置 邮件

首先必须安装 Email Extension Plugin 插件

系统管理 –> 系统设置 – > Jenkins Location

配置系统管理员邮件地址 — > xxx@163.com

配置 Extended E-mail Notification

SMTP Server =

点击高级

勾选 Use SMTP Authentication

输入 发送 用户 与 密码

填写 SMTP port

Default Content Type 选择 HTML (text/html)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Default Subject =  构建通知:$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!


Default Content = 

<b style="font-size:12px">(本邮件是程序自动下发的,请勿回复,<span style="color:red">请相关人员fix it,重新提交到git 构建</span>)<br></b><hr>

<b style="font-size: 12px;">项目名称:$PROJECT_NAME<br></b><hr>

<b style="font-size: 12px;">构建编号:$BUILD_NUMBER<br></b><hr>

<b style="font-size: 12px;">GIT版本号:${GIT_REVISION}<br></b><hr>

<b style="font-size: 12px;">构建状态:$BUILD_STATUS<br></b><hr>

<b style="font-size: 12px;">触发原因:${CAUSE}<br></b><hr>

<b style="font-size: 12px;">构建日志地址:<a href="${BUILD_URL}console">${BUILD_URL}console</a><br></b><hr>

<b style="font-size: 12px;">构建地址:<a href="$BUILD_URL">$BUILD_URL</a><br></b><hr>

<b style="font-size: 12px;">变更集:${JELLY_SCRIPT,template="html"}<br></b><hr>

https://jicki.cn/img/posts/jenkins/8.png

项目 – > 构建后操作 — > 添加 Editable Email Notification

拉到最下面 — > 点击 Advanced Settings…

https://jicki.cn/img/posts/jenkins/9.png

Recipient List 添加 收件邮箱 多个邮件以空格 隔开

https://jicki.cn/img/posts/jenkins/10.png