在CentOS上部署GitLab CI/CD需要以下几个步骤:
1. 安装GitLab
首先,你需要在CentOS上安装GitLab。你可以选择使用官方的GitLab仓库来安装。
添加GitLab仓库
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
安装GitLab CE(社区版)
sudo yum install gitlab-ce
启动并启用GitLab服务
sudo systemctl start gitlab sudo systemctl enable gitlab
验证安装
打开浏览器,访问 http://
,你应该能看到GitLab的登录页面。
2. 配置GitLab Runner
GitLab Runner是执行CI/CD任务的代理。
注册Runner
在GitLab项目页面,点击 Settings
-> CI / CD
,然后选择 Runners
。点击 New runner
,按照提示进行操作。
你可以选择以下几种注册方式:
- Shell Executor:适用于简单的任务。
- Docker Executor:适用于需要Docker环境的任务。
- Kubernetes Executor:适用于Kubernetes环境。
这里以Shell Executor为例:
sudo gitlab-runner register
按照提示输入GitLab实例的URL和注册令牌,选择Shell Executor,并配置Runner的名称和标签。
验证Runner注册
在GitLab项目页面,点击 Settings
-> CI / CD
,然后选择 Runners
,你应该能看到新注册的Runner。
3. 配置CI/CD Pipeline
在GitLab项目中创建一个 .gitlab-ci.yml
文件,定义你的CI/CD流程。
示例 .gitlab-ci.yml
stages: - build - test - deploy build_job: stage: build script: - echo "Building the project..." - # 你的构建脚本 test_job: stage: test script: - echo "Running tests..." - # 你的测试脚本 deploy_job: stage: deploy script: - echo "Deploying the project..." - # 你的部署脚本 only: - master
4. 运行CI/CD Pipeline
提交 .gitlab-ci.yml
文件到GitLab仓库,GitLab Runner会自动检测到新的文件并开始执行CI/CD流程。
你也可以手动触发Pipeline:
- 在GitLab项目页面,点击
CI / CD
->Pipelines
。 - 点击
Run pipeline
,选择要运行的分支。
5. 监控和调试
如果Pipeline运行失败,你可以在GitLab项目页面查看详细的日志信息,以便进行调试。
通过以上步骤,你就可以在CentOS上成功部署GitLab CI/CD,并开始自动化你的构建、测试和部署流程。