Jenkins-Gitlab 前端项目自动化部署

1. 机器准备

|---------|------------|
| 开发 | 10.0.0.204 |
| gitlab | 10.0.0.201 |
| jenkins | 10.0.0.200 |
| web | 10.0.0.202 |

2. 开发主机配置

创建密钥对,将公钥添加到gitlab账号的ssh密钥中

root@dev \~\]#**ssh-keygen** \[root@dev \~\]#**cat /root/.ssh/id_rsa.pub** ![](https://i-blog.csdnimg.cn/direct/49ed5889918647a0b0d1cde5856c169c.png) 复制远程仓库的内容到本地 \[root@dev \~\]#**git clone git@gitlab.lly.org:devops/wheel.git** dns 指向201主机 \[root@dev \~\]#**vim /etc/netplan/01-netcfg.yaml** ...... nameservers: addresses: \[10.0.0.201

3. gitlab 主机配置

配置dns解析

root@gitlab \~\]#**apt update \&\& apt install -y bind9 bind9-utils bind9-host bind9-dnsutils** \[root@gitlab \~\]#**vim /etc/bind/named.conf.default-zones** ...... zone "lly.org" IN { type master; file "/etc/bind/lly.org.zone"; }; \[root@gitlab \~\]#**vim /etc/bind/lly.org.zone** $TTL 1D @ IN SOA master admin ( 1 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS master master A 10.0.0.201 gitlab A 10.0.0.201 \[root@gitlab \~\]#**chgrp bind /etc/bind/lly.org.zone** \[root@gitlab \~\]#**systemctl enable named** \[root@gitlab \~\]#**systemctl restart named**

4. jenkins 主机配置

dns 指向201主机

root@jenkins \~\]#**vim /etc/netplan/01-netcfg.yaml** ...... nameservers: addresses: \[10.0.0.201

安装 jenkins 和 jdk

root@jenkins \~\]#**apt update \&\& apt -y install openjdk-17-jdk** \[root@jenkins \~\]#**ls** jenkins_2.479.1_all.deb \[root@jenkins \~\]#**dpkg -i jenkins_2.479.1_all.deb** \[root@jenkins \~\]#**systemctl status jenkins.service** 创建脚本将前端文件并赋予权限 \[root@jenkins \~\]#**mkdir -p /data/jenkins/scripts** \[root@jenkins \~\]#**vim /data/jenkins/scripts/test.sh** HOST_LIST=" 10.0.0.202" for host in $HOST_LIST;do scp -r \* root@$host:/var/www/html/ done \[root@jenkins \~\]#**chmod +x /data/jenkins/scripts/test.sh** 在 jenkins 工具上创建全局凭据(凭据中添加的私钥和gitlab上添加公钥成对即可) \[root@jenkins \~\]#**su jenkins** jenkins@jenkins:\~/.ssh$ **cat /root/.ssh/id_rsa** 将查询到的私钥添加到下面private key 中 ![](https://i-blog.csdnimg.cn/direct/10b875e837854c2e801da9cb723e1e40.png) jenkins@jenkins:\~/.ssh$ **cat id_rsa.pub** 将查询到的公钥添加到对应gitlab账户的ssh密钥中 ![](https://i-blog.csdnimg.cn/direct/ce91b7e46b28432c9f0a4f0d20881175.png) 在jenkins工具上创建任务 ![](https://i-blog.csdnimg.cn/direct/dedeb0fe72b248589b982d9c4720455b.png) ![](https://i-blog.csdnimg.cn/direct/434268e4bc2f4678b4cb8f11bfee130f.png) ![](https://i-blog.csdnimg.cn/direct/96e5e31881134324980b1caa039fc066.png)

5. web 服务器配置

dns 指向201主机

root@web \~\]#**vim /etc/netplan/01-netcfg.yaml** ...... nameservers: addresses: \[10.0.0.201

安装 nignx 服务

root@web \~\]#**apt update \&\& apt install nginx -y**

6. 验证是否实现自动化部署

在开发主机上修改代码并上传到远程仓库

root@dev wheel\]#**vim index.html** \[root@dev wheel\]#**git commit -am "change"** \[root@dev wheel\]#**git push --all** 随后用 web 服务器 ip 在浏览器中验证是否显示修改后的页面

相关推荐
十年磨一剑~36 分钟前
Linux程序接收到sigpipe信号崩溃处理
linux
geshifei42 分钟前
Sched ext回调3——select_cpu(linux 6.15.7)
linux·ebpf
yunfuuwqi1 小时前
OpenClaw✅真·喂饭级教程:2026年OpenClaw(原Moltbot)一键部署+接入飞书最佳实践
运维·服务器·网络·人工智能·飞书·京东云
代码游侠1 小时前
C语言核心概念复习——网络协议与TCP/IP
linux·运维·服务器·网络·算法
你真是饿了1 小时前
6.库制作与原理
linux·服务器
Zach_yuan2 小时前
深入浅出 JSONCpp
linux·服务器·网络·c++
马猴烧酒.3 小时前
【面试八股|JAVA多线程】JAVA多线程常考面试题详解
java·服务器·数据库
Serene_Dream3 小时前
git 合并冲突的分支
git
北京迅为3 小时前
《【北京迅为】itop-3568开发板NPU使用手册》- 第 7章 使用RKNN-Toolkit-lite2
linux·人工智能·嵌入式·npu
我是一只puppy3 小时前
使用AI进行代码审查
javascript·人工智能·git·安全·源代码管理