前端项目gitlab自动化部署CICD实战(windows版)

       本篇文章是关于gitlab自动化部署实战的,简单涉及到原理,如果想要了解更多特性及原理性的东西,请参照官网(Get started with GitLab CI/CD);另外,由于某些特殊因素,本篇CICD的实战是在windows环境下进行的。

      大致的CICD流程是可以这样认为的(假设流程已经跑通):当您提交或者合并分支到目标分支时,就会触发目标服务器的gitlab-runner(或者说runner服务一直在监听着gitlab目标分支的变化),runner就会根据配置,把该分支下的工程下载到目标服务器,然后根据gitlab-ci.yml配置执行相关的job,完成CICD。

一、下载gitlab-runner.exe

1、下载gitlab-runner.exe,windows 64位官网下载,该下载地址可能需要科学上网。

2、在目标服务上去创建一个文件夹,该文件夹要有读写的权限,将gitlab-runner.exe复制到该目录下。

注:文件夹的名称一定要英文,且要避免非法字符,如空格等。(血的教训)

       还必须要有git管理工具,它会依赖这个工具执行CICD的某些操作。

二、注册gitlab-runner

1、找到gitlab某个工程下CICD的相关信息,如下图(下方图的url和token是我们注册gitlab-runner要用到的信息)

2、管理员身份运行powershell,执行./gitlab-runner.exe register,然后一步步填写注册信息

PS E:\zero_dev\cicd> ./gitlab-runner.exe register
Runtime platform                                    arch=amd64 os=windows pid=32796 revision=7f7a4bb0 version=13.11.0
Enter the GitLab instance URL (for example, https://gitlab.com/):
https://xxx.xxx.com/    # 这是你gitlab的地址,即上一步中的url
Enter the registration token:
Z16Ng6BGpWtf98TU4M7d    # 这是你gitlab某个工程中的token,即上一步中的token
Enter a description for the runner:
[zhaoshuai9lc10]: nocode-205-example   # 这个是描述,尽量描述清楚些,否则后面你会不清楚这个runner是搞啥的
Enter tags for the runner (comma-separated):
nocode-205              # 这个是标签,可以指定运行哪个或哪些标签的runner
Registering runner... succeeded                     runner=Z16Ng6BG
Enter an executor: virtualbox, docker-ssh+machine, custom, docker-windows, parallels, shell, ssh, docker, docker-ssh, docker+machine, kubernetes:
shell     # 选择执行器,这里我们选择shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
PS E:\zero_dev\cicd>

这样就完成了runner与gitlab工程之间的信息互注册。runner知道了自己需要操作哪个gitlab项目,gitlab工程也知道了该runner的存在。

接着刷新一下runners settings界面,就会看到新注册的runner了。

也会在本地gitlab-runner.exe目录下生成一个config.toml,这里面记录着咱们的注册信息。

3、将gitlab-runner.exe注册成服务,并启动该服务。(分别执行./gitlab-runner.exe install 和  ./gitlab-runner.exe start)

然后就会发在进程中多了一个gitlab-runner.exe

注:

1、执行器选择shell时,windows默认的执行器是powershell,所以在执行job的script的命令时,只有符合powershell的命令会被执行,否则将会报错。

如:linux命令中rm -rf 是可以被执行的,但是powershell中-rf是不被识别的。

2、./gitlab-runner.exe install 可以指定用户。

三、 配置yml文件。

点击gitlab左侧CI/CD下方的编辑器,然后选择编写`流水线编辑器`,加yml配置复制进去

我的yml文件的配置如下,更多(gitlab-ci.yml的配置参照官网):

# # 代表注释
# 这里面有两台服务器的配置,205和108
# stages  定义一个工作场景阶段,install_build、deploy_test、deploy_production都是要执行的job
stages:
  - install_build
# - deploy_test
  - deploy_production
# 全局变量,可以在job中使用
variables:
  targetPath_dec_108: C:\xxxx\web\apps\dev\nocode\web\form-designer
  targetPath_inspur_205: C:\yyyy\web\apps\dev\nocode\web\form-designer

# 是否启用缓存, {}表示不启用缓存
cache: {}

# job,安装依赖并执行编译
install_build_205:
  # stages中的一个job
  stage: install_build
  # 只有在dev和master分支才触发CICD
  only:
    - dev
    - master
  # 运行指定tag的gitlab-runner
  tags:
    - nocode-205
  # 在执行命令前执行的操作
  before_script:
    - chcp.com 65001
  # 命令操作
  script:
    - npm config set registry https://registry.npm.taobao.org/
    - npm install
    - npm run build
  # 在执行命令后执行的操作
  after_script:
    - rm -r node_modules/*
  # 制品,即build之后的生成物
  artifacts:
    paths:
      - dist/*

# 安装依赖并执行编译
install_build_108:
  stage: install_build
  only:
    - dev
    - master
  tags:
    - nocode-108
  before_script:
    - chcp.com 65001
  script:
    - npm config set registry https://registry.npm.taobao.org/
    - npm install
    - npm run build
  after_script:
    - rm -r node_modules/*
  artifacts:
    paths:
      - dist/*

# 部署测试服务器
# deploy_test:
#   stage: deploy_test
#   only:
#     - dev
#   script:
#     - pm2 delete app || true
#     - pm2 start app.js --name app

# 部署生产服务器
deploy_production_205:
  stage: deploy_production
  # 依赖哪个job,上一个job执行完,才执行这个
  needs: ["install_build_205"]
  only:
    - dev
    - master
  tags:
    - nocode-205
  before_script:
    - chcp.com 65001
  script:
    - echo $CI_PROJECT_DIR
    - echo $targetPath_inspur_205
    - echo $CI_PROJECT_DIR\dist\*---------to---------$targetPath_inspur_205
    - rm -r $targetPath_inspur_205\*
    - cp -r $CI_PROJECT_DIR\dist\* $targetPath_inspur_205
    
# 部署生产服务器
deploy_production_108:
  stage: deploy_production
  needs: ["install_build_108"]
  only:
    - dev
    - master
  tags:
    - nocode-108
  before_script:
    - chcp.com 65001
  script:
    - echo $CI_PROJECT_DIR
    - echo $targetPath_dec_108
    - echo $CI_PROJECT_DIR\dist\*---------to---------$targetPath_dec_108
    - rm -r $targetPath_dec_108\*
    - cp -r $CI_PROJECT_DIR\dist\* $targetPath_dec_108

注:

这个配置的意思是,我在两天服务器上分别执行编译和部署;我的gitlab-ci.yml不一定是最优配置,理解也有限,各位可以参照官网自行组织所需配置。

四、查看运行效果

这样就可以看到有两条流程在跑。

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值