GITlab CI/CD流程配置
什么是CI/CD流程
CI的好处:
- 快速的发现错误
- 保证分支不偏离主干
执行过程中的任务流程.定义在.gitlab-ci.yml中, 运行的pipeline如下:
CI流程配置:
0. 服务器配置:
此步骤是如果gitlab服务器是自己搭建的,则需要去配置好本地IP
/opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml
修改host地址到gitlab对应的服务器地址。
1. gitlab-runner配置:
1.1 gitlab-runner安装:
参考 https://docs.gitlab.com/runner/install/
以linux x86安装为例子:
# 下载gitlab-runner文件
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
# 添加执行权限
sudo chmod +x /usr/local/bin/gitlab-runner
# 使用docker[可选]
curl -sSL https://get.docker.com/ | sh
# 创建一个gitlab-ci user[可选]
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
# 安装和开启service
sudo gitlab-runner install --user=gitlab-runner[这里可以更改用户] --working-directory=/home/gitlab-runner
sudo gitlab-runner start
1.2 gitlab-runner注册:
注册runner
sudo gitlab-runner register
# 根据需要填写描述,token等,如下图:
输入:gitlab-ci 对应的URL ,认证token ,名称,备注,执行器(docker较好),以及默认镜像。
注意url和token可以从项目的 settings → CI/CD → runners 中查询到:
配置完成后runner 显示绿色就代表和runner保持了链接。
注意:需要编辑runner 的选项,保持在job 没有tag时候也要运行。否在为标记的jobs在pipeline在运行时会提示找不到执行器。(没配置过得人可能会遇到坑)
1.3 修改runner的配置文件:
#查看gitlab-runner list配置
> gitlab-runner list
Listing configured runners ConfigFile=/home/bitmain/.gitlab-runner/config.toml
查看如下配置:
[[runners]]
name = "chendu_ubuntu_docker"
url = "http://10.32.228.64/"
token = "6bcb2ffc9f2efab2ddc91b3ecff7de"
executor = "docker"
[runners.custom_build_dir]
[runners.docker]
tls_verify = false
image = "ubuntu:v4"
privileged = false
disable_entrypoint_overwrite = false
oom_kill_disable = false
devices = ["/dev/bm1682-dev0:/dev/bm1682-dev0", "/dev/bmdev-ctl:/dev/bmdev-ctl"]
disable_cache = false
volumes = ["/cache", "/home/bm-driver/release_ubuntu:/workspace/release_ubuntu:rw", "/home/.sophon:/root/.sophon:rw", "/home/bitmain/bm-driver/release_centos:/workspace/release_centos:rw", "/home/bm-driver/release_debian:/workspace/release_debian:rw"]
pull_policy = "never"
shm_size = 0
[runners.cache]
[runners.cache.s3]
[runners.cache.gcs]
devices: 挂载设备
volumes: 挂在目录
pull_policy: 如果image不存在是否拉取,never表示任何时候都不拉取.
2. CI流程使用:
2.1.编写.gitlab-ci.yml,放在项目根目录:
.gitlab-ci.yml的详细语法记录在:https://docs.gitlab.com/ee/ci/yaml/ 中 ,其可以定义一个项目提交后
从build 到 test 到delpoly 中每一步可以进行的工作。以其中一个流程来具体描述
build_ubuntu:
image:
name: "ubuntu:v5"
stage: test
script:
- cd /workspace/release_ubuntu/scripts
- bash ./install_lib.sh nntc
- cd /workspace/release_ubuntu/scripts
- source envsetup_pcie.sh
- cd /builds/ai-algorithm/sophon-inference
- mkdir -p build
- cd build
- cmake ..
- make
- cd /builds/ai-algorithm/sophon-inference/python
- source setup_dev_python_path.sh
- cd /builds/ai-algorithm/sophon-inference
- python3 tests/algokit/test_yolov3.py
- python3 tests/algokit/test_ssh.py
tags:
- chendu_ubuntu_docker
build_ubuntu: pipeline的名称
image: 运行在哪个镜像中
stage: 在哪个阶段运行,不同阶段是串行的,一个阶段内是并行的.
script: 具体运行的bash命令
tags: 在gitlab-runner建立时候对每个runner标记的tag,用于指定哪个此pipeline运行在哪个runner中.如下图蓝色部分的c++_runner_tag就是tag
2.2 触发CI\CD
在每次commit项目代码时则均会触发代码检查,可以通过CI\CD菜单查看运行情况
点击job可以查看具体内容:
3. 其他配置
如果CI失败可以触发不能merge,设置:
Settings -> Merge request
开启 Only allow merge requests to be merged if the pipeline succeeds