gitlab添加CI自动测试

1. CI运行规格配置文件.gitlab-ci.yml

yml 配置文件语法:
https://docs.gitlab.com/17.3/ee/ci/yaml/index.html

添加.gitlab-ci.yml文件并配置 :

## 定义几个阶段 
stages:          # List of stages for jobs, and their order of execution
  - build
  - test
  - deploy
  - 
 ## 示例job ,可以添加多个job 
build-generator-job:       # This job runs in the build stage, which runs first.
  stage: build  ## 运行的阶段 
  tags:
    - build-generator-job
  only:
    - develop
  script:
    - echo "Compiling the code..."
    - /bin/bash xxx.sh generator all  ## 示例内容,实际build需要干的事情,此处可以写命令,也可以写脚本等 
    - echo "Compile complete."

参考博客:https://blog.csdn.net/Mrxiao_bo/article/details/138863594

2. gitlab上添加runner & 绑定CI运行的服务器

进入项目的CICD配置页面,点击runners进入:


2.1 在服务器上安装runner

### Download and install binary

# Download the binary for your system
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

# Give it permission to execute
sudo chmod +x /usr/local/bin/gitlab-runner

# Create a GitLab Runner user
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

# Install and run as a service
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start

## Command to register runner

sudo gitlab-runner register --url http://192.168.1.xxx/ --registration-token GR1348941p2u73yR4HziHG8oNMjn7
## 此处的url是gitlab服务器地址 
## token是gitlab那边生成的,直接从runner处拷贝过来即可。

2.2 在服务器上注册runner

$ sudo gitlab-runner register --url http://192.168.1.xx/ --registration-token GR1348941p2u73yR4HziHG8oNMjn7
Runtime platform                                    arch=amd64 os=linux pid=1779809 revision=b92ee590 version=17.4.0
Running in system-mode.                            
                                                   
Enter the GitLab instance URL (for example, https://gitlab.com/):
# 可不填
[http://192.168.1.xx/]: 
Enter the registration token:
# 可不填
[GR1348941p2u73yR4HziHG8oNMjn7]: 
Enter a description for the runner:
# 可不填
[qli-Z390-UD]: 
Enter tags for the runner (comma-separated):
# 该runner可以执行的tag,这个和yml文件定义的job对应,也可不填,后续在gitlab runner界面下配置 
build-generator-job build-runtime-job build-app-job model-test-job
Enter optional maintenance note for the 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://docs.gitlab.com/ee/ci/runners/new_creation_workflow 
Registering runner... succeeded                     runner=GR1348941p2u73yR4
Enter an executor: custom, kubernetes, docker, docker-windows, docker+machine, docker-autoscaler, shell, ssh, parallels, virtualbox, instance:
# 配置执行环境,可以选docker/shell 等,跟进实际需求来定,如果需要依赖服务器环境等,可直接选shell 
docker
Enter the default Docker image (for example, ruby:2.7):
latest
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 "/etc/gitlab-runner/config.toml"

打开runner运行服务器下该文件 /home/gitlab-runner/.bash_logout ,屏蔽如下几行: (要不然会导致初始化得时候报错)

if [ "$SHLVL" = 1 ]; then
    [ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi

3. 原理解析

3.1 CI如何判断任务执行成功or失败 ?

  script:
    - echo "Compiling the code..."
    - /bin/bash test.sh all > test.log
    - echo "Compile complete."

如上脚本命令,判断成功失败的条件是每条命令的返回状态,如果返回非0,则判定为失败,如果返回0则是成功

3.2 artifacts下面配置的路径是如何确定,有何作用

model-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  tests... This will take about 10 mins."  > test.log
  artifacts:
 artifacts:
    name: "$CI_BUILD"
    paths:
      - test.log

artifacts用于指定在job 成功或失败 时应附加到作业的文件和目录的列表 。作业完成后,工件将被发送到GitLab,并可在GitLab UI中下载 .

artifacts:paths

路径是相对于项目目录($CI_PROJECT_DIR)的,不能直接在其外部链接。

相关推荐
JZC_xiaozhong2 小时前
低空经济中的数据孤岛难题,KPaaS如何破局?
大数据·运维·数据仓库·安全·ci/cd·数据分析·数据库管理员
Nejosi_念旧3 天前
开发常用工具
flutter·vue·gitlab·postman
yaoshengting4 天前
GitLab集成Jira
gitlab·jira
JZC_xiaozhong5 天前
支付宝“政府补贴”bug事件背后的权限管理启示
大数据·数据库·安全·ci/cd·金融·云计算·bug
mrhaoxiaojun6 天前
gitlab runner正常连接 提示 作业挂起中,等待进入队列 解决办法
gitlab
铜锣烧1号6 天前
【学习笔记】GitLab 使用技巧和说明和配置和使用方法
git·python·pycharm·gitlab
极小狐6 天前
如何在 Rocky Linux 上安装极狐GitLab?
gitlab·devsecops·devops·极狐gitlab·安全合规
_.Switch6 天前
高效构建与部署Python Web应用:FastAPI的测试与持续集成
前端·网络·数据库·python·ci/cd·fastapi
ihengshuai6 天前
手摸手实战前端项目CI CD
前端·ci/cd·云原生·kubernetes·devops