9. GitLab CI/CD

9.1. 关于GitLab

9.2. 关于关于GitLab CI/CD

  • CI/CD是一种软件开发的方法,策略,CI 表示持续集成, CD表示持续部署,这种迭代有助于减少基于错误或失败的版本进行开发新代码的可能性。使用这种方法,从新代码开发到部署,可以减少人工干预甚至不用干预。

  • 直观体验

    image-20230903104440887

    CI_JOBS

9.3. 搭建CI/CD开发环境步骤

安装Gitlab服务器

GitLab Runner服务器

  • 一般来讲GitLab服务器和GitLab Runner服务器不是同一台服务器,避免文件的错误

  • 环境准备

    • 在服务器上安装必要软件,以MDK KEIL开发为例,需要安装KEIL, GIT, Python等软件

    • 由于GitLab Runner都是执行shell脚本,需要将开发环境的所有可执行文件,放到环境变量中

    • 配置好Git客户端,用户名,ssh公钥,确保Git可用

    • 下载并安装powershell 7 的msi安装包 64位32位,确保pwsh.exe所在路径在系统环境变量PATH中,gitlab-runner会用到pwsh.exe;在 Windows 上安装 PowerShell - PowerShell | Microsoft Learn

安装Gitlab Runner

以Windows平台为类,具体说一下如何安装

  • 创建存放GitLab-Runner相关文件的文件夹

    image-20230903111449254

  • 下载GitLab Runner可执行文件,该可执行文件是跨平台的

  • windows版本:Install GitLab Runner on Windows | GitLab

    image-20230903110325769

  • 下载后,将文件重命名,方便后续执行命令:gitlab-runner-windows-amd64.exe 修改为 gitlab-runner.exe

    image-20230903111719237

  • 以管理员方式运行cmd命令行窗口,并cd C:\GitLab-Runner

    image-20230903111843420

  • 运行GitLab runner

    image-20230903112010310

  • 注册GitLab Runner

    image-20230903110148926

  • image-20230903145832547

  • 安装了PowerShell-7.3.6-win-x64.msi后,确认是否已经将pwsh.exe放到了Path中,如果有,仍然出现下面的问题,可以尝试重启一下系统

    image-20230903173808252

    image-20230903175655768

    • 注释说明

      c:\GitLab-Runner>gitlab-runner.exe register
      Runtime platform                                    arch=amd64 os=windows pid=15596 revision=8ec04662 version=16.3.0
      Enter the GitLab instance URL (for example, https://gitlab.com/):
      #Gitlab网址
      Enter the registration token:
      #上面token可在下个图片所示位置找到,需要与项目匹配;
      Enter a description for the runner:
      [DESKTOP-7NOSC7V]: WX-CYC-Gitrunner	# description这个可以随意是可修改的,可以描述该runner是干什么用的
      Enter tags for the runner (comma-separated):
      # tags很重要!后面也可以修改但不建议后期修改,一旦修改会影响已经绑定该runner的CI运行,.gitlab-ci.yml会定义使用的tags即该名字的runner
      WX-CYC-Gitlabrunner
      Enter optional maintenance note for the runner:
      WX-CYC-Gitlabrunner,build,pwsh  # 输入这个 Runner 的可选维护说明,可以随意写
      WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecated in GitLab Runner 15.6 and will be replaced with support for authentication tokens. For more information, see https://gitlab.com/gitlab-org/gitlab/-/issues/380872
      Registering runner... succeeded                     runner=E8rHK3x7
      Enter an executor: custom, shell, virtualbox, docker+machine, instance, docker, docker-windows, parallels, ssh, docker-autoscaler, kubernetes:
      [pwsh]: shell  # 会在windows上自动映射到pwsh.exe,所以需要安装powershell 7
      Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
      
      Configuration (with the authentication token) was saved in "c:\\GitLab-Runner\\config.toml"
      

      打开Gitlab项目设置中的CI/CD,找到注册好的runner信息

  • image-20230903112845164

  • 至此,Runner已经注册完成,具备一个可部署的环境了

9.4. GitLab CI的语法介绍

参考文档

#流水线的stages的顺序可以自己定义,Runner会按照该顺序执行
#相同阶段的任务将会并发的执行,上一个阶段的任务完整的结束之后,下一个阶段的任务才会开始执行 
stages:
  - build
  - test
  - build-schedules
  - test-schedules

variables:
  GIT_SUBMODULE_STRATEGY: recursive # 拉取 Submodule 内容
  
# patch构建,只要有修改了*.c,*.h,west.yml,.gitlab-ci.yml,*.py相关提交就会触发;
build:
  stage: build
  tags:
    - WX-Gitlabrunner
  script:
    - echo 'stage build' # 显示当前阶段
    - echo 'west update'
    - west update
    - cd tools/CI/	# bat命令:切到工程的tools/CI目录
    - echo "$PWD"	# bat命令:显示所在当前路径
    - echo $CI_COMMIT_SHA	# $CI_COMMIT_SHA 代表本次CI执行拉取代码的哈希值
    #- python CI_build.py	# 执行python脚本,编译top.bin bt.bin,也可通过使用keil相关cmd命令编译
    #- python pack.py $CI_COMMIT_SHA	# 执行python脚本,保存编译后的bin及相关map文件
  allow_failure: false # 是否允许本阶段失败,false:不允许,true:允许
  only: # only:在满足某些条件时执行,
    refs:
      - WS9652 # 仅当分支名是 WS9652 时生效
    changes: #修改了*.c,*.h,west.yml,.gitlab-ci.yml,*.py文件时触发
      - "*.c"
      - "*.h"
      - "west.yml"
      - ".gitlab-ci.yml"
      - "*.py"
  except: # except:在满足某些条件时不执行;only和except冲突时,except优先级更高
    changes:
      - "*.md" # *.md 文件修改不触发

# 构建后测试
test:
  stage: test
  tags:
    - WX-Gitlabrunner
  script:
    - echo 'stage test'
    - cd tools/CI
    - echo "$PWD"
    # - pip install paramiko
    #- python auto_write_test.py $CI_COMMIT_SHA # 执行python脚本,包括自动烧录release/debug版本flash,及烧录后开机测试
  when: manual
  allow_failure: false
  only:
    refs:
      - WS9652
    changes:
      - "*.c"
      - "*.h"
      - "west.yml"
      - ".gitlab-ci.yml"
      - "*.py"
  except:
    changes:
      - "*.md"
 
# 计划构建,仅通过计划触发
build-schedules:
  stage: build-schedules
  tags:
    - WX-Gitlabrunner
  script:
    - echo 'stage build-schedules'
    - echo 'west update'
    - west update
    - cd tools/CI/
    - echo "$PWD"
    - echo $CI_COMMIT_SHA
    - python CI_build.py
    - python pack.py $CI_COMMIT_SHA
  allow_failure: false
  only:
    - schedules # 表示仅允许计划触发

# 计划测试,仅通过计划触发
test-schedules:
  stage: test-schedules
  tags:
    - WX-Gitlabrunner
  script:
    - echo 'stage test-schedules'
    - cd tools/CI
    - echo "$PWD"
    # - pip install paramiko
    - python auto_write_test.py $CI_COMMIT_SHA
  when: manual
  allow_failure: false
  only:
    - schedules