Git+Jenkins实战(一):从零搭建自动化发布与回滚系统(附完整代码)

一、为什么需要Jenkins?

在现代软件开发中,手动部署项目不仅效率低下,而且容易出错。随着团队规模扩大、版本迭代加快,自动化集成与部署成为刚需。

Jenkins 作为最流行的开源自动化服务器,支持:

  • 持续集成(CI)

  • 持续交付(CD)

  • 自动化测试

  • 多环境部署

  • 版本回滚

本文将通过一个完整的静态网站项目,带你从0到1掌握 Jenkins + GitLab + Ansible 的自动化发布流程。


二、环境准备(完整设备清单)

主机名 IP 地址 软件 角色
Jenkins 192.168.10.104 Jenkins 构建服务器
GitLab 192.168.10.105 GitLab 代码仓库
web01 192.168.10.101 Apache 网站服务器
dev 192.168.10.102 Git 开发者主机

所有主机均使用 CentOS 7/8 或 Rocky Linux。


三、GitLab 项目创建与代码提交(详细步骤)

1. 创建项目 league

登录 GitLab → 新建项目 → 选择空白项目 → 项目名称为 league

2. 开发者克隆项目并提交代码(完整命令)

bash

复制代码
# 设置主机名
hostnamectl set-hostname dev
bash

# 安装 Git
yum install -y git

# 克隆项目
git clone http://192.168.10.105/root/league.git
cd league

# 配置用户信息
git config --global user.name "Zhang San"
git config --global user.email "zhangsan@league.com"
git config --global core.editor vim

# 初始化本地仓库
git init

# 添加代码文件(假设已有代码)
git add .

# 提交到本地仓库
git commit -m "Initial commit"

# 设置主分支名称
git branch -M main

# 推送到远程仓库
git push -u origin main
# 输入 GitLab 用户名和密码

✅ 小知识:git branch -M main 是将默认分支从 master 改为 main,这是 Git 社区近年推荐的做法。


四、Jenkins 配置:发布静态网站

1. 安装 Publish Over SSH 插件

进入 Manage JenkinsManage Plugins → 搜索并安装 Publish Over SSH

2. 配置远程 Web 服务器

路径:Manage JenkinsSystemPublish over SSH

  • Name: web01

  • Hostname: 192.168.10.101

  • Username: root

  • Remote Directory: /var/www/html

  • 勾选 Use password authentication → 输入密码

点击 Test Configuration 验证连接成功。

3. 新建自由风格任务

  • 任务名:league-deploy

  • 类型:Freestyle project

源码管理:
  • 选择 Git

  • Repository URL: http://192.168.10.105/root/league.git

  • Branches to build: */main

构建步骤:
  • 增加构建步骤 → Send files or execute commands over SSH

  • Name: web01

  • Source files: **/*

  • Remote directory: /

📌 注意:**/* 表示同步所有文件,Remote directory 是相对于之前配置的 /var/www/html

4. 构建并验证

点击 Build Now,查看控制台输出,成功后访问:

text

复制代码
http://192.168.10.101

看到网站内容即表示部署成功。


五、参数化构建:实现版本切换与回滚

1. 修改代码并打标签

bash

复制代码
# 修改代码
vim index.html

# 提交
git add .
git commit -m "update index.html"

# 打标签
git tag v1.0
git push origin v1.0

2. Jenkins 配置参数化构建

  • 勾选 This project is parameterized

  • 添加参数 → Git Parameter

    • Name: Tag

    • Default value: origin/main

3. 修改源码管理中的分支

  • Branches to build: $Tag

4. 构建时选择版本

点击 Build with Parameters,选择 v1.0main,即可发布不同版本。

✅ 扩展知识点:

  • Git Parameter 插件会自动拉取 Git 仓库中的所有标签和分支。

  • 回滚只需选择旧标签重新构建即可。


六、自动触发构建:GitLab Webhook

1. Jenkins 配置触发器

  • 勾选 Build when a change is pushed to GitLab

  • 复制 Webhook URL:http://192.168.10.104:8080/project/league-deploy

  • 生成并复制 Token

2. GitLab 配置 Webhook

  • 进入项目 → Settings → Webhooks

  • URL 填写 Jenkins 地址

  • Secret Token 填写刚才生成的 Token

  • 勾选 Push events

3. 测试自动触发

修改代码并推送:

bash

复制代码
git add .
git commit -m "test webhook"
git push

Jenkins 会自动触发构建,无需手动点击。

✅ 知识点补充:

Webhook 本质是 HTTP 回调,GitLab 在代码推送时会主动请求 Jenkins 的接口,触发构建。


七、进阶实战:Jenkins + Ansible 部署 PHP 项目(WordPress)

1. 安装 LAMP 环境(web01)

bash

复制代码
dnf install -y httpd mysql mysql-server php php-mysqlnd
systemctl start httpd mysql
mysqladmin -u root password 'pwd123'

创建数据库:

sql

复制代码
CREATE DATABASE wordpress;
GRANT ALL ON *.* TO 'root'@'localhost' IDENTIFIED BY 'pwd123';
FLUSH PRIVILEGES;

2. 开发者提交 WordPress 代码

bash

复制代码
git clone http://192.168.10.105/root/wordpress.git
tar -zxvf wordpress-6.8.2.tar.gz
cd wordpress
git add .
git commit -m "add wordpress"
git push

3. Jenkins 配置 Ansible 插件

  • 安装 Ansible Plugin

  • 构建步骤中添加 Invoke Ansible Ad-Hoc Command

配置参数:

  • Host pattern: webservers

  • Inventory: /etc/ansible/hosts

  • Module: synchronize

  • Module arguments:

    text

    复制代码
    src=${WORKSPACE} dest=/var/www/html/ rsync_opts=--exclude=.git

💡 知识点:

  • ${WORKSPACE} 是 Jenkins 内置变量,表示当前任务的工作目录。

  • synchronize 模块本质是调用 rsync,适合增量同步代码。

4. 重启服务与权限设置

再添加两个构建步骤(Execute shell):

bash

复制代码
ansible webservers -m command -a "systemctl restart httpd"
ansible webservers -m command -a "chown -R apache:apache /var/www/html/"

5. 访问 WordPress 安装页面

浏览器访问:

text

复制代码
http://192.168.10.101/wp-admin/setup-config.php

按照提示完成安装。


八、总结与扩展知识点

模块 知识点
版本控制 Git 标签、分支管理、main/master 分支演变
Jenkins 核心 参数化构建、Webhook、插件扩展
部署方式 SSH 推送、Ansible 同步、rsync 增量
回滚机制 通过标签重新构建实现一键回滚
自动触发 GitLab Webhook 实现 Push 即构建

常见问题与建议

  • Webhook 触发失败:检查 GitLab 外发请求设置(允许发往本地网络)

  • 权限不足:确保 Jenkins 主机的 SSH 用户有目标服务器的写入权限

  • 代码未更新:检查是否清除了浏览器缓存,或 Apache 是否重启

相关推荐
言6664 小时前
要忽略前端依赖包node_modules的文件在目录下 git暂存区消失
git
胡小禾5 小时前
Git Worktree
git
程序员小羊!5 小时前
18 GIt
git
怣疯knight5 小时前
Git 本地分支关联远程分支 常用命令汇总
git
ANNENBERG5 小时前
git分支开发管理
git
坤坤藤椒牛肉面5 小时前
GIT的使用
git
w3296362716 小时前
使用 OpenCode 在 Windows 上加速安装 Playwright 的完整指南
windows·git
无人生还别怕6 小时前
搭建jenkins服务并接入openldap认证
运维·jenkins
Java 码思客21 小时前
【ElasticSearch从入门到架构师】第3章:ES 核心基础概念(架构师必备底层认知)
大数据·elasticsearch·jenkins