1.前端项目自动化部署需要的环境依赖
Node 安装项目依赖、打包都需要
Nginx web 项目部署必须「正向代理、方向代理、负载均衡等等」、 GitLab 也会用到
Nginx(默认自动安装)
Git 自动化部署,需要拉取代码
GitLab 没啥好说
GitLab-Runner 配合 GitLab CI/CD 使用的应用程序
2.安装 Node
下载和解压
wget https://nodejs.org/dist/v12.9.0/node-v12.9.0-linux-x64.tar.xz
tar xf node-v12.9.0-linux-x64.tar.xz
cp -rf /root/node-v12.9.0-linux-x64 /usr/local/node
vim /etc/profile
export PATH=$PATH:/usr/local/node/bin
source /etc/profile
[root@jumpserver node]
v12.9.0
3.安装 Git
Gitlab 自动化部署需要拉取代码,需要用到 Git 。尽量安装Git 2.x.x 版本,不然新版的 GitLab 自动化部署无法拉取代码。
本人安装的是 Git 2.24.4 版本,其他版本请自行尝试
使用 IUS 仓库(yum源),执行完下面的命令,应该就能安装了git
yum -y install \
https://repo.ius.io/ius-release-el7.rpm \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@jumpserver yum.repos.d]
[root@jumpserver yum.repos.d]
git version 1.8.3.1
4.源码编译nginx
[root@jumpserver data]
[root@jumpserver data]
[root@jumpserver data]
[root@jumpserver data]
[root@jumpserver nginx-1.18.0]
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module \
> --with-http_v2_module \
> --with-http_realip_module \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --with-pcre \
> --with-stream \
> --with-stream_ssl_module \
> --with-stream_realip_module
[root@jumpserver nginx-1.18.0]
[root@jumpserver nginx-1.18.0]
[root@jumpserver nginx]
[root@jumpserver nginx]
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@jumpserver system]
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@jumpserver system]
success
[root@jumpserver system]
5.安装GIt Lab
yum -y install https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-14.3.0-ce.0.el7.x86_64.rpm
vim /etc/gitlab/gitlab.rb
external_url 'http://192.168.32.132:1874'
firewall-cmd --permanent --zone=public --add-port=1874/tcp
firewall-cmd --reload
[root@jumpserver ~]
sudo gitlab-ctl start
sudo gitlab-ctl stop
sudo gitlab-ctl restart
[root@jumpserver ~]
Password: fBy1bpzI/sQ8J5bks1KS41hsW0eD9f1P/PX7HdHc6Z4=
进入 GitLab 控制台
gitlab-rails console -e production
执行命令: user = User.where(id: 1).first,此 user 则表示 root 用户
执行命令:user.password = 'admin@sjy’修改密码, user.password_confirmation = ‘admin@sjy’ 确认密码
执行命令: user.save
执行命令: exit
5.部署CI/CD
安装 gitlab-runner
wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
chmod +x /usr/local/bin/gitlab-runner
useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
gitlab-runner start
新建 runner
gitlab-runner register
http://192.168.32.132:1874
tZwc77sFFHRbX6ck9q9r
测试webpack-vue项目部署
webpack-vue-cicd
shell
[root@jumpserver /]
Runtime platform arch=amd64 os=linux pid=12332 revision=febb2a09 version=15.0.0
Running in system-mode.
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://192.168.32.132:1874
Enter the registration token:
tZwc77sFFHRbX6ck9q9r
Enter a description for the runner:
[jumpserver]: 测试webpack-vue项目部署
Enter tags for the runner (comma-separated):
webpack-vue-cicd
Enter optional maintenance note for the runner:
shell
Registering runner... succeeded runner=tZwc77sF
Enter an executor: custom, docker, ssh, virtualbox, docker-ssh+machine, kubernetes, docker-ssh, parallels, shell, docker+machine:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
注册完成后,就可以在http://localhost:1874/admin/runners 页面查看创建的runner
6.nginx 配置项目访问地址
mkdir -pv /www/wwwroot/webpack_vue_cicd
chown gitlab-runner /www/wwwroot/webpack_vue_cicd/
firewall-cmd --permanent --zone=public --add-port=3001/tcp
firewall-cmd --reload
vim /usr/local/nginx/conf/nginx.conf
server {
listen 3001;
server_name localhost;
location / {
root /www/wwwroot/webpack_vue_cicd;
index index.html index.htm;
}
}
[root@jumpserver /]
7.push 项目到gitlab 新建项目下
git remote update origin --prune 刷新分支 如果有旧分支删除 创建新的分支 就刷新到最新的分支在提交代码到那个新的分支上面
git branch 查看本地分支
git branch -a 查看本地以及远程所有分支
git branch -r 查看远程分支
git checkout -b compile remotes/origin/compile 切换到你想要提交的分支
git add .
git commit -m '' 提交代码
git status 查看状态
git pull origin compile 拉取远程指定分支
git log -3 --graph compile 查看本地提交commit的状态
操作步骤:
1.将gitlab上新建项目先拉取到本地
2.将文件夹内容复制进此目录下
3.执行相关git命令
8. 编写 .gitlab-ci.yml 文件
.gitlab-ci.yml 文件是存放在项目的根目录下的,要提交到gitlab上面,runner 会根据 .gitlab-ci.yml 编写的规则自动部署项目
新建 .gitlab-ci.yml,添加下面的内容
stages:
- build
- deploy
cache:
paths:
- node_modules/
build:
stage: build
tags:
- webpack-vue-cicd
script:
- cd ${CI_PROJECT_DIR}
- echo "当前目录位置:"
- pwd
- echo "安装npm步骤"
- npm install
- npm run build
only:
- main
artifacts:
paths:
- dist/
deploy:
stage: deploy
tags:
- webpack-vue-cicd
script:
- rm -rf /www/wwwroot/webpack_vue_cicd/*
- cp -rf ${CI_PROJECT_DIR}/dist/* /www/wwwroot/webpack_vue_cicd/
9.验收效果
每次出发流水线的条件:
1.当有新的代码提交到gitlab分支上时,就会自动触发流水线
2.自己通过gitlab上的请求合并选项手动点击流水线
3.浏览器输入192.168.32.132:3001 访问项目
参考链接
|