gitlab实现CI/CD自动化部署
gitlab支持通过配置CI/CD实现自动化部署我们的代码项目,主要核心就是配置gitlab-ci.yml文件以及在目标服务器上配置gitlab-runner
项目根目录中新增gitlab-ci.yml
这是我配置前端项目自动化部署的,整体大差不差,大家可自行更加需要,修改对应的script
yaml
default:
before_script:
- echo "start deploy"
- echo $CI_COMMIT_REF_NAME
# 阶段
stages:
- install
- buildDev
- buildTest
- buildProd
- deployDev
- deployTest
- deployProd
cache:
paths:
- node_modules/
# 安装依赖
install:
stage: install
# 此处的tags必须填入之前注册时自定的tag
tags:
- install
# 规定仅在dev、test、prod分支提交时才触发此阶段
only:
- dev
- test
- prod
# # 规定仅在package.json提交时才触发此阶段
# changes:
# - package.json
# 执行脚本
script:
- echo "start install"
- rm -rf ./node_modules
- npm install
# 打包dev分支
buildDev:
stage: buildDev
tags:
- buildDev
# 规定仅在dev分支提交时才触发此阶段
only:
- dev
script:
- echo "start dev build"
- rm -rf ./dist
- npm run build:dev
# 将此阶段产物传递至下一阶段
artifacts:
paths:
- dist/
# 打包test分支
buildTest:
stage: buildTest
tags:
- buildTest
# 规定仅在test分支提交时才触发此阶段
only:
- test
script:
- echo "start test build"
- rm -rf ./dist
- npm run build:test
# 将此阶段产物传递至下一阶段
artifacts:
paths:
- dist/
# 打包prod分支
buildProd:
stage: buildProd
tags:
- buildProd
# 规定仅在prod分支提交时才触发此阶段
only:
- prod
script:
- echo "start prod build"
- rm -rf ./dist
- npm run build:prod
# 将此阶段产物传递至下一阶段
artifacts:
paths:
- dist/
# 部署dev项目
deployDev:
stage: deployDev
tags:
- deployDev
only:
# 规定仅在dev分支提交时才触发此阶段
- dev
script:
- echo "start deploy Dev"
# 删除远端服务器的dist目录下的所有文件
- ssh -p 33953 root@17.215.219.70 'rm -rf /home/web/appointment_h5/dist'
# 把当前dist目录下的所有文件传输到远端服务器的dist目录
- scp -r -P 33953 ./dist root@17.215.219.70:/home/web/appointment_h5
- echo "deploy Dev successfully"
# 部署test项目
deployTest:
stage: deployTest
tags:
- deployTest
only:
# 规定仅在test分支提交时才触发此阶段
- test
script:
- echo "start deploy Test"
# 删除远端服务器的dist目录下的所有文件
- ssh -p 33953 root@17.215.219.70 'rm -rf /home/web/appointment_h5/dist'
# 把当前dist目录下的所有文件传输到远端服务器的dist目录
- scp -r -P 33953 ./dist root@17.215.219.70:/home/web/appointment_h5
- echo "deploy Test successfully"
# 部署prod项目
deployProd:
stage: deployProd
tags:
- deployProd
only:
# 规定仅在prod分支提交时才触发此阶段
- prod
script:
- echo "start deploy Prod"
- pwd
# 删除远端服务器的dist目录下的所有文件
- ssh -p 33953 root@17.215.219.70 'rm -rf /home/web/appointment_h5/dist'
# 把当前dist目录下的所有文件传输到远端服务器的dist目录
- scp -r -P 33953 ./dist root@17.215.219.70:/home/web/appointment_h5
- echo "deploy Prod successfully"
在gitlab上查看gitlab-runner配置方法
setting~CI/CD
找到runner
点击配置指南
选择要配置服务器对应的系统,并根据提示在服务器上执行对应代码
以linux为例
- 在目标服务器执行下列命令下载gitlab-runner
bash
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
- 分配权限执行runner
bash
sudo chmod +x /usr/local/bin/gitlab-runner
- 创建一个gitlab-runner用户
bash
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
- 安装runner
bash
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
- 启动runner
bash
sudo gitlab-runner start
- 注册runner
bash
sudo gitlab-runner register --url 你项目的仓库地址 --registration-token runner的token
例子:
sudo gitlab-runner register --url https://www.gitlab_abc.com/ --registration-token GR1348941mpAA965843DD8vpX
备注:这一步需要你输入项目描述、tags、以及部署的执行方式,大家根据情况自己填写,我选择的tags是buildDev, buildProd, buildTest, deployDev, deployProd, deployTest, install,执行方式是shell
- 执行完成之后,在仓库的CI/CD上就会出现一个runner
配置服务器环境
- nginx配置
- 创建打包后的代码存放地址
- 一些项目的启动环境,比如node之类的
推送代码,触发runner,实现自动构建和部署
向gitlab-ci.yml指定的分支推送代码,gitlab就会监听仓库指定分支的变化,从而触发runner,执行gitlab-ci.yml上的script脚本,这样就会把项目自动部署到服务器上
点击每一个节点进程,可以看到具体的执行日志输出
服务器上已经可以看到打包好并传输到指定目录的代码了
参考
https://www.cnblogs.com/05-hust/p/18081574
https://www.jianshu.com/p/8376ef8bafa6
https://blog.csdn.net/biubiubiuPlus/article/details/136644029
https://blog.csdn.net/weixin_39246554/article/details/130749673
https://www.jianshu.com/p/8376ef8bafa6
https://blog.csdn.net/yuan_jiaoyoung/article/details/131789769
https://blog.csdn.net/Mrxiao_bo/article/details/138863594
https://juejin.cn/post/6967570299336261646
https://www.jianshu.com/p/fd00a9cc87ef
https://blog.csdn.net/qq_30273575/article/details/127847751
https://www.jianshu.com/p/8d0a75dbd801
https://www.cnblogs.com/imgss/p/13746878.html
https://juejin.cn/post/6967570299336261646