在Linux環境下搭建GitLab CI/CD涉及幾個主要步驟,包括安裝GitLab、配置GitLab Runner以及設置.gitlab-ci.yml文件。以下是詳細的步驟指南:
下載GitLab CE安裝包:
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
安裝依賴包:
sudo yum install -y curl policycoreutils-python openssh-serversudo systemctl enable sshdsudo systemctl start sshdsudo firewall-cmd --permanent --add-service=httpsudo systemctl reload firewalldsudo yum install postfixsudo systemctl enable postfixsudo systemctl start postfix
配置外部URL:
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
sudo EXTERNAL_URL="http://gitlab.example.com" yum install -y gitlab-ce
重啟服務:
sudo gitlab-ctl reconfigure
sudo gitlab-ctl start
下載GitLab CE安裝包:
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
安裝依賴包:
sudo yum install -y curl policycoreutils-python openssh-server cronieyum install lokkitsudo lokkit -s http -s sshsudo yum install postfixsudo service postfix startsudo chkconfig postfix on
配置外部URL:
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
sudo EXTERNAL_URL="http://gitlab.example.com" yum -y install gitlab-ce
重啟服務:
sudo gitlab-ctl reconfigure
sudo gitlab-ctl start
下載并安裝GitLab Runner:
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
sudo apt-get install gitlab-runner
注冊Runner:
sudo gitlab-runner register
.gitlab-ci.yml文件在項目根目錄下創建.gitlab-ci.yml文件,定義CI/CD流程:
stages:
- build
- test
- deploy
build-job:
stage: build
script:
- echo "編譯構建中..."
- mkdir -p build
- touch build/info.txt
test-job:
stage: test
script:
- echo "測試運行中..."
- test -f build/info.txt
deploy-job:
stage: deploy
script:
- echo "部署應用中..."
only:
- master
GitLab CI可以使用Docker容器來運行任務,這樣可以避免為每個項目配置不同的環境:
test:
image: node:14
services:
- postgres:13
script:
- npm test
確保開放必要的端口,如SSH(默認22)、HTTP(默認80)和HTTPS(默認443):
sudo firewall-cmd --add-service=ssh --permanent
sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --reload
通過以上步驟,你可以在Linux環境下成功搭建GitLab CI/CD環境。初次登錄時需要為GitLab的root用戶設置密碼。