在 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 了。