在CentOS上使用GitLab進行CI/CD實踐涉及多個步驟,包括安裝GitLab、配置CI/CD流水線、以及運行CI/CD流程。以下是詳細的步驟指南:
更新系統:
sudo yum update -y
安裝必要的依賴包:
sudo yum install -y curl openssh-server openssh-clients postfix policycoreutils-python perl git nodejs rsync cronie
配置防火墻:
sudo systemctl enable sshd
sudo systemctl start sshd
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --permanent --add-service=http
sudo systemctl reload firewalld
設置時區(如果需要):
sudo timedatectl set-timezone Asia/Shanghai
添加GitLab的軟件源:
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
安裝GitLab CE:
EXTERNAL_URL="http://your_server_ip" sudo yum install -y gitlab-ce
配置GitLab:
編輯 /etc/gitlab/gitlab.rb 文件,設置外部URL和其他配置,例如SMTP郵件服務器。
sudo vi /etc/gitlab/gitlab.rb
重新配置并重啟GitLab:
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
安裝Docker:
sudo yum install -y docker
sudo systemctl start docker
sudo systemctl enable docker
添加GitLab Docker倉庫:
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
安裝GitLab容器注冊表:
EXTERNAL_URL="http://your-server-ip" sudo yum install gitlab-ce
啟動GitLab容器:
sudo gitlab-ctl start
訪問GitLab:
打開瀏覽器,訪問 http://your-server-ip,你應該能夠看到GitLab的登錄頁面。
創建 .gitlab-ci.yml 文件:
在項目根目錄下創建 .gitlab-ci.yml 文件,定義CI/CD流程。以下是一個簡單的示例:
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Building."
artifacts:
paths:
- build/
test_job:
stage: test
script:
- echo "Testing."
dependencies:
- build_job
deploy_job:
stage: deploy
script:
- echo "Deploying."
dependencies:
- test_job
提交并運行流水線: 使用以下命令提交更改并觸發流水線:
git add .
git commit -m 'Initial CI/CD configuration'
git push --set-upstream origin main
glab ci status
在GitLab倉庫中,每當有代碼提交時,GitLab CI/CD會自動運行 .gitlab-ci.yml 中定義的流程。開發人員可以在GitLab的CI/CD頁面查看流程的運行狀態和日志。
通過以上步驟,你可以在CentOS上成功安裝和配置GitLab,并實現CI/CD實踐。