【GIT】基于.gitlab-ci.yml发布前端工程

基于.gitlab-ci.yml发布前端工程

Date : 2021.06.11
Author: jwensh

关键词: gitlab ci/cd

1.需求

  1. 使用 gitlab 来管理工程代码,并使用 gitlab-ci 来发布工程代码到测试或开发环境
  2. 触发条件可以根据 pushcommitbranchtag 来触发发布操作
  3. 01 完成上诉操作

2.实践

1.为工程添加 .gitlab-ci.yml 文件

注意:.gitlab-ci.yml是一个YAML文件,所以你必须特别注意缩进。始终使用空格,而不是制表符。

  • 这里需要注意的点就是,你想怎么构建你的 pipeline ? 里面都有哪些步骤和操作
  • 对于我记录的这个:我希望执行
    • 1.下载 master 的代码,安装依赖
    • 2.进行打包,生成可部署的文件
    • 3.将代码部署到指定地方
    • 4.执行成功后,删除打包完的文件
  • 上述执行其实可以使用 shell 脚本来实现
  • 想要了解具体的 .gitlab-ci.yml 内容细节,可以查看 官方文档,或自己使用的 gitlab help(http://your.site.com/help/ci/yaml/README.md) 帮助文档

特别提醒:gitlab 内置变量会因为不同版本有不同变化,请查看具体的 gitlab help 文档

stages:
  - install
  - build
  - deploy
  - cleanup

install:
  stage: install
  script:
    - echo $CI_BUILD_REF
    - git log -1 --format=%s
    - echo $CI_PROJECT_DIR
    - cnpm install
  only:
    - /^v[0-9]/
  environment:
    name: test-192
  tags:
    - onetest

package:
  stage: build
  script:
    - cnpm run build:prod
  only:
    - /^v[0-9]/
  tags:
    - onetest
  environment:
    name: test-192

deploy:
  stage: deploy
  script:
    - sh ./build/deploy-192.sh $CI_PROJECT_DIR // 这一块是可以完成四部的操作
  only:
    - master
    - /^v[0-9]/
  tags:
    - onetest
  environment:
    name: test-192

cleanup:
  stage: cleanup
  script:
    - rm -rf ./dist
  only:
    - /^v[0-9]/
  tags:
    - onetest
  when: on_success
  environment:
    name: test-192
  • 通俗的解读下我对 gitlab-ci.yml 文档的理解,其作用就是定义一场活动,这次活动的流程由 stages 控制, 然后有各个环节的主持人(即:installpackagedeploycleanup 四位) , 为了让这个活动圆满完成,出现很多人报名参加贡献节目(jobs),那活动就规定节目的内容形式,什么时候上和上哪些人(onlyexceptwhen)?表演什么(script)?跟那个主持人对接(stage)?在哪个会场表演(tags 他是下面创建的 runner 中的一个标识)?

2.为工程添加一个 gitlab runner

  • 什么是 gitlab runner ?可以看下 官方文档 深入理解下其原理, 如果用过 jenkins 就不难理解他的作用

  • 看下图

    951fa1d6d1eaf475a68ff51383a0f420.png

  • 要为您的系统下载适当的软件包:

  1. gitlab-runner 找到最新的文件名和选项 。
  2. 选择一个版本并下载二进制文件,如下载任何其他标记版本的文档中所述,用于最新的 GitLab Runner 版本。
  • 例如,对于 Debian 或 Ubuntu:

    # Replace ${arch} with any of the supported architectures, e.g. amd64, arm, arm64
    # A full list of architectures can be found here https://gitlab-runner-downloads.s3.amazonaws.com/latest/index.html
    curl -LJO "https://gitlab-runner-downloads.s3.amazonaws.com/latest/deb/gitlab-runner_${arch}.deb"
    
  • 例如,对于 CentOS 或 Red Hat Enterprise Linux:

    # Replace ${arch} with any of the supported architectures, e.g. amd64, arm, arm64
    # A full list of architectures can be found here https://gitlab-runner-downloads.s3.amazonaws.com/latest/index.html
    curl -LJO "https://gitlab-runner-downloads.s3.amazonaws.com/latest/rpm/gitlab-runner_${arch}.rpm"
    
  • 可以使用 lsb_release -a 来查看当前机器的信息

  • 安装

  1. 为您的系统安装软件包,如下所示。

    例如,对于 Debian 或 Ubuntu:

    dpkg -i gitlab-runner_<arch>.deb
    

    例如,对于 CentOS 或 Red Hat Enterprise Linux:

    rpm -i gitlab-runner_<arch>.rpm
    
  2. 注册跑步者

    在 Linux 下注册跑步者:
    
    运行以下命令:
    
    sudo gitlab-runner register
    
    输入您的 GitLab 实例 URL(也称为gitlab-ci coordinator URL)。
    输入您获得的令牌以注册跑步者。
    输入跑步者的描述。您可以稍后在 GitLab 用户界面中更改此值。
    输入与 runner 关联的标签,用逗号分隔。您可以稍后在 GitLab 用户界面中更改此值。
    提供runner executor。对于大多数用例,请输入 docker。
    如果您docker作为执行人输入,系统会要求您提供用于未在.gitlab-ci.yml.
    
    

    需要注意的是 runner executor 会决定了执行整个 ci 脚本的环境,区别参考官网文档, 因为会存在版本差异,请你谨慎选择,我安装的时候选择了 docker,最后修改了的配置文件为 shell

    [@gd_63 tools]# curl -LJO "https://gitlab-runner-downloads.s3.amazonaws.com/latest/rpm/gitlab-runner_amd64.rpm"
    total 138356
    -rw-r--r-- 1 root root 141657052 Jun  8 14:18 gitlab-runner_amd64.rpm
    (base) [@gd_63_192 tools]# sudo gitlab-runner register
    Runtime platform                                    arch=amd64 os=linux pid=4036 revision=7a6612da version=13.12.0
    Running in system-mode.
    
    Enter the GitLab instance URL (for example, https://gitlab.com/):
    https://git.***.com/
    Enter the registration token:
    novx3wu2atuU3NxRAi**
    Enter a description for the runner:
    [gd_63_192]: TM_onetest_frontend
    Enter tags for the runner (comma-separated):
    onetest
    Registering runner... succeeded                     runner=novx3wu2
    Enter an executor: virtualbox, kubernetes, custom, docker, shell, ssh, docker+machine, docker-ssh+machine, docker-ssh, parallels:
    docker
    Enter the default Docker image (for example, ruby:2.6):
    
    Enter the default Docker image (for example, ruby:2.6):
    ruby:2.6
    Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
    
    默认安装 gitlab-runner
    
    目录: /home/gitlab-runner
    配置文件: /etc/gitlab-runner/config.toml 
    配置参考: https://docs.gitlab.com/runner/executors/README.html
    
    可以使用 gitlab-runner 命令来操作这个 agent
    
  • 执行完上面,可以在 gitlab 项目工程下 settings/ci cd 页面看到对应的 runner

3.为 runner 所在机器上配置打包和部署相关依赖 (略)

  • 安装 node.js 、cnpm (注意版本)

    npm install cnpm -g --registry=https://registry.npm.taobao.org
    
  • 安装 nginx (配置nginx.conf)

4.测试

  • 本地代码通过 branchtag 提交,符合正则条件的,都会触发 ci

3.遇到的问题

  • 不知道脚本预设公共变量都有哪些:https://docs.gitlab.com/ee/ci/variables/predefined_variables.html

  • 内置环境变量都有哪些: https://docs.gitlab.com/ee/ci/variables/README.html#syntax-of-environment-variables-in-job-scripts

  • gitlab-ci出现fatal: reference is not a tree问题的原因可能有:

    • 当前执行的分支或tag被删除
    • 当前执行的分支和tag有同名的情况
    • yml文件中 only 和 expert 配置冲突了
  • git push 后发现不能build成功,是否的nodejs版本问题.需要从新下载安装安装

    Running with gitlab-runner 13.12.0 (7a6612da)
      on TM 8080f1e4
    Preparing the "shell" executor
    Using Shell executor...
    Preparing environment
    Running on gd_63...
    Getting source from Git repository
    Fetching changes...
    rm: cannot remove ‘/home/gitlab-runner/builds/8080f1e4/0/test_mm/TM/.git/hooks/post-checkout’: Permission denied
    Cleaning up file based variables
    ERROR: Job failed: exit status 1
    
  • 没有目录权限:是因为 runner 使用的是 gitlab-runner 用户,如果整个 ci 设计到操作其他权限的文件目录就会报错,所以需要 chmod 下对应的文件或目录

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值