.gitlabci.yml

本文最后更新于:1 分钟前

.gitlab-ci.yml初始模版

# This file is a template, and might need editing before it works on your project.
# This is a sample GitLab CI/CD configuration file that should run without any modifications.
# It demonstrates a basic 3 stage CI/CD pipeline. Instead of real tests or scripts,
# it uses echo commands to simulate the pipeline execution.
#
# A pipeline is composed of independent jobs that run scripts, grouped into stages.
# Stages run in sequential order, but jobs within stages run in parallel.
#
# For more information, see: https://docs.gitlab.com/ee/ci/yaml/index.html#stages
#
# You can copy and paste this template into a new `.gitlab-ci.yml` file.
# You should not add this template to an existing `.gitlab-ci.yml` file by using the `include:` keyword.
#
# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Getting-Started.gitlab-ci.yml

stages:          # List of stages for jobs, and their order of execution
  - build
  - test
  - deploy

build-job:       # This job runs in the build stage, which runs first.
  stage: build
  script:
    - echo "Compiling the code..."
    - echo "Compile complete."

unit-test-job:   # This job runs in the test stage.
  stage: test    # It only starts when the job in the build stage completes successfully.
  script:
    - echo "Running unit tests... This will take about 60 seconds."
    - sleep 60
    - echo "Code coverage is 90%"

lint-test-job:   # This job also runs in the test stage.
  stage: test    # It can run at the same time as unit-test-job (in parallel).
  script:
    - echo "Linting code... This will take about 10 seconds."
    - sleep 10
    - echo "No lint issues found."

deploy-job:      # This job runs in the deploy stage.
  stage: deploy  # It only runs when *both* jobs in the test stage complete successfully.
  environment: production
  script:
    - echo "Deploying application..."
    - echo "Application successfully deployed."

执行流程

stages定义 CI 执行流程的多个阶段,在上面的初始模版中共定义了三个阶段:

  1. build
  2. test
  3. depoly

stages中的顺序决定对应 job的执行顺序。如上面模版中,首先执行 build 阶段,所有属于 build 阶段的 job会并行,执行结束后,才会开始执行下一个stage test。

下面的 build-jobunit-test-joblint-test-jobdeploy-job 定义了一系列带有约束说明的job,每个job必须包含 scriptscript可以直接执行系统命令或脚本。job中的stage定义了该job属于哪个stages

该模版文件的输出结果是

# 首先执行 buile 阶段,下面的 job 中,build-job 属于 build 阶段
Compiling the code...
Compile complete.

# 执行 test 阶段,unit-test-job 和 lint-test-job 属于 test 阶段
Running unit tests... This will take about 60 seconds.
Code coverage is 90%

# 最后执行 deploy 阶段,deploy-job 属于 deploy 阶段
Deploying application...
Application successfully deployed.

gitlab-runner

job由 gitlab-runner 执行,每一个job的执行过程都是独立的,可以在不同的机器安装 gitlab-runner,并分配给不同的 project。

在 CentOS 中安装 gitlab-runner

# 添加 yum 源
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
# 更新 yum
yum update
# 安装 gitlab-runner
yum install gitlab-runner
# 查看版本
gitlab-runer -v

注册

image-20221106135851557

gitlab-runner register
# 填 gitlab 的 ip,可以是私有部署的 gitlab,也可以直接填写 https://gitlab.com
# 在 CI/CD 设置中找到 registration token,填写

然后可以在设置中看到已注册的可运行的 runner

image-20221106140015572

接下来 push 代码,就会自动触发 runner 执行任务

image-20221106140126128

image-20221106140138906

gitlab-runner 安装踩坑

  1. git 版本过低

CentOS 中带有的的 git 版本可能是 1.8.x,在这个版本中,CI 执行过程 gitlab-runner 可能会报错:

fatal: git fetch-pack: expected shallow list
fatal: The remote end hung up unexpectedly

更新 git 版本到最新就好了。

  1. 查看gitlab-runner 执行过程

更新 gitlab-runner 到最新版本,可在 GitLab 中看到 runner 在控制台中的执行过程。

若在添加 yum 源后没有执行 yum update,安装的 gitlab-runner 是低版本,在 GitLab 中只能看到 job执行的最终结果,如果执行出错,不方便排查是哪里出现的错误。

# 添加 yum 源
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
# 更新 yum,-y 参数默认为后面的命令执行 y
yum update -y
# 更新 gitlab-runner
yum install gitlab-runner
# 查看版本
gitlab-runner -v
# 我安装的 Version: 15.5.0 可以看到详细的执行过程

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!