在Debian系統上使用GitLab進行持續集成(CI/CD)涉及幾個關鍵步驟,包括安裝GitLab、配置GitLab Runner以及設置CI/CD流水線。以下是詳細的步驟指南:
確保您的Debian系統已經更新到最新版本:
sudo apt update
sudo apt upgrade
安裝必要的軟件包:
sudo apt install curl openssh-server ca-certificates postfix
使用以下命令下載GitLab鏡像文件并安裝:
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
sudo apt install gitlab-ce
在安裝過程中,系統可能會提示您配置外部URL和電子郵件設置,請根據您的環境提供適當的值。
啟動GitLab服務:
sudo systemctl start gitlab
設置GitLab開機自啟:
sudo systemctl enable gitlab
重新配置GitLab以應用更改:
sudo gitlab-ctl reconfigure
使用Docker拉取并安裝GitLab Runner:
docker pull gitlab/gitlab-runner:latest
為了讓所有項目都能用這個Runner進行部署,需要修改配置文件,打上鉤子。
配置Docker代理(如果需要):
usermod -aG docker gitlab-runner
編輯~/.docker/config.json
文件,配置代理:
{
"proxies": {
"default": {
"httpProxy": "http://proxy_ip:8080",
"httpsProxy": "http://proxy_ip:8080",
"noProxy": "localhost,127.0.0.1"
}
}
}
.gitlab-ci.yml
文件,用于定義CI/CD流水線的配置。在.gitlab-ci.yml
文件中定義流水線階段(stages)和任務(jobs),例如構建、測試、部署等。
使用不同的執行器(executors)來運行任務,如Shell、Docker等。
配置觸發條件,如代碼提交、定時觸發等。
配置環境變量,用于存儲敏感信息或配置參數。
配置緩存以提高流水線的執行效率。
配置依賴關系,使得某個任務依賴于其他任務的執行結果。