
在
CI/CD 是什么?
持续集成
(Continuous Integration, CI)
每次代码推送至版本控制系统(如 GitLab 仓库),系统自动进行构建和测试。这确保了代码变更能够快速集成到主分支,同时发现并修复集成错误。
持续交付
(Continuous Delivery, CD)
在持续集成的基础上,进一步自动化将已验证的代码变更交付到一个预生产或生产准备就绪的环境中。这个阶段可能包括更复杂的测试套件、性能测试等,确保软件随时可以部署到生产环境,但实际部署可能需要人工审批。
持续部署 (Continuous Deployment, CD)
在持续交付的基础上,将交付过程完全自动化,无需人工干预即可将通过所有测试的代码直接部署到生产环境。这意味着每次代码变更经过测试后都能快速安全地到达用户手中。

- 对提交的代码自动测试,失败的打回,通过测试的等待合并。
- 合并后自动部署到生产环境。
- 对于有合并权限的开发者,提交代码后自动测试,通过后自动部署生产环境。
实际应用
- 每次有提交自动触发测试流水线

- 测试通过的提交可以发起合并请求

- 测试通过,项目管理员合并,并自动触发测试和部署流水线

- 完成自动部署到生产环境


怎么搭建Gitlab CI/CD
- 下载 Gitlab runner
cd ~ curl -LJO https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64 chmod +x ~/gitlab-runner-linux-amd64 mv ~/gitlab-runner-linux-amd64 ~/gitlab-runner
- 配置Gitlab runner
~/gitlab-runner register #打开需要配置的项目的CI/CD页面,复制url和token进行填写

- 启动Gitlab runner
~/gitlab-runner run --working-directory=$HOME --config=~/.gitlab-runner/config.toml #可以设置--working-directory为希望测试脚本运行的位置
- 配置项目中的 .gitlab-ci.yml 流水线文件
stages: # List of stages for jobs, and their order of execution - test - deploy 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). tags: - v1.0 script: - echo "Linting code... This will take about 10 seconds." - bash test/test.sh # 写项目中的相对路径 - 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. tags: - v1.0 script: - echo "Deploying application..." - bash deploy/deploy.sh # 写项目中的相对路径 - echo "Application successfully deployed." only: - main
完成!可以提交代码测试了!
展望
规范脚本开发
每个模块的开发应书写相应的单元测试脚本。或是AI自动生成。
简化代码管理者的工作
管理者不必自行检查代码,自行部署代码。
降低开发对生产环境的不良影响
Rscript -e 'lintr::lint_dir(".")' # 检查当前目录下的所有 R 文件
最低限度下可以保证代码无语法问题。 确保部署后无重大Bug。