在 CentOS 上搭建 GitLab CI 需要以下几个步骤:
首先,确保你的 CentOS 系统是最新的,并且安装了一些必要的依赖包。
sudo yum update -y
sudo yum install -y curl policycoreutils-python openssh-server
你可以从 GitLab 的官方网站下载最新的安装脚本,或者使用包管理器安装。这里我们使用包管理器安装。
# 添加 GitLab 仓库
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
# 安装 GitLab CE
sudo yum install -y gitlab-ce
编辑 GitLab 的配置文件 /etc/gitlab/gitlab.rb
,进行一些基本的配置。
sudo nano /etc/gitlab/gitlab.rb
在文件中找到以下行并进行修改:
external_url 'http://your-gitlab-domain.com'
将 http://your-gitlab-domain.com
替换为你的 GitLab 实例的域名或 IP 地址。
保存并关闭文件后,运行以下命令重新配置并启动 GitLab。
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
GitLab CI/CD 的配置是通过 .gitlab-ci.yml
文件来完成的。你需要在你的项目根目录下创建这个文件,并定义你的 CI/CD 流程。
例如,一个简单的 .gitlab-ci.yml
文件可能如下所示:
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Building the project..."
- mvn package
test_job:
stage: test
script:
- echo "Running tests..."
- mvn test
deploy_job:
stage: deploy
script:
- echo "Deploying the project..."
- scp target/my-app.jar user@your-server:/path/to/deploy
.gitlab-ci.yml
文件将 .gitlab-ci.yml
文件提交到你的 GitLab 仓库中。
git add .gitlab-ci.yml
git commit -m "Add GitLab CI/CD configuration"
git push origin master
一旦 .gitlab-ci.yml
文件被推送到仓库,GitLab CI/CD 将自动开始运行你的 CI/CD 流程。
你可以在 GitLab 的 Web 界面中监控 CI/CD 流程的进度和状态。导航到你的项目页面,然后点击 CI/CD
> Pipelines
。
通过以上步骤,你就可以在 CentOS 上成功搭建 GitLab CI/CD 了。