gitlab实现CI/CD自动化部署

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 [email protected] 'rm -rf /home/web/appointment_h5/dist'
    # 把当前dist目录下的所有文件传输到远端服务器的dist目录
    - scp -r -P 33953 ./dist [email protected]:/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 [email protected] 'rm -rf /home/web/appointment_h5/dist'
    # 把当前dist目录下的所有文件传输到远端服务器的dist目录
    - scp -r -P 33953 ./dist [email protected]:/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 [email protected] 'rm -rf /home/web/appointment_h5/dist'
    # 把当前dist目录下的所有文件传输到远端服务器的dist目录
    - scp -r -P 33953 ./dist [email protected]:/home/web/appointment_h5
    - echo "deploy Prod successfully"

在gitlab上查看gitlab-runner配置方法

setting~CI/CD

找到runner

点击配置指南

选择要配置服务器对应的系统,并根据提示在服务器上执行对应代码

以linux为例

  1. 在目标服务器执行下列命令下载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
  1. 分配权限执行runner
bash 复制代码
sudo chmod +x /usr/local/bin/gitlab-runner
  1. 创建一个gitlab-runner用户
bash 复制代码
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
  1. 安装runner
bash 复制代码
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
  1. 启动runner
bash 复制代码
sudo gitlab-runner start
  1. 注册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

  1. 执行完成之后,在仓库的CI/CD上就会出现一个runner

配置服务器环境

  1. nginx配置
  2. 创建打包后的代码存放地址
  3. 一些项目的启动环境,比如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

https://blog.csdn.net/qq_40699305/article/details/82753506

https://juejin.cn/post/7037022688493338661

相关推荐
新加坡内哥谈技术1 小时前
Meta计划借助AI实现广告创作全自动化
运维·人工智能·自动化
集成显卡10 小时前
PlayWright | 初识微软出品的 WEB 应用自动化测试框架
前端·chrome·测试工具·microsoft·自动化·edge浏览器
b***251111 小时前
动力电池点焊机:驱动电池焊接高效与可靠的核心力量|比斯特自动化
人工智能·科技·自动化
沉到海底去吧Go11 小时前
【行驶证识别成表格】批量OCR行驶证识别与Excel自动化处理系统,行驶证扫描件和照片图片识别后保存为Excel表格,基于QT和华为ocr识别的实现教程
自动化·ocr·excel·行驶证识别·行驶证识别表格·批量行驶证读取表格
Iamccc13_13 小时前
智能仓储的未来:自动化、AI与数据分析如何重塑物流中心
人工智能·数据分析·自动化
星释15 小时前
如何自动部署GitLab项目
gitlab
keson要进步15 小时前
CICD实战(一) -----Jenkins的下载与安装
运维·ci/cd·centos·自动化·jenkins
keson要进步15 小时前
CICD实战(二)-----gitlab的安装与配置
linux·运维·gitlab
猫头虎20 小时前
[特殊字符]解决 “IDEA 登录失败。不支持早于 14.0 的 GitLab 版本” 问题的几种方法
java·ide·网络协议·http·https·gitlab·intellij-idea
tianyuanwo20 小时前
Ansible自动化运维全解析:从设计哲学到实战演进
运维·自动化·ansible