在CentOS上實踐GitLab CI/CD涉及多個步驟,包括安裝必要的軟件包、配置GitLab、注冊GitLab Runner以及編寫和觸發CI/CD流水線。以下是一個詳細的指南:
首先,確保你的CentOS服務器已經安裝了必要的軟件包。你可以使用以下命令來安裝這些軟件包:
sudo yum install -y curl policycoreutils openssh-server openssh-client postfix
然后,啟動并設置SSH服務開機自啟:
sudo systemctl enable sshd
sudo systemctl start sshd
接下來,配置防火墻以允許HTTP和HTTPS流量:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
你可以通過多種方式在CentOS上安裝GitLab,包括使用官方腳本和Docker。以下是使用官方腳本的方法:
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
sudo yum install -y gitlab-ce
sudo gitlab-ctl reconfigure
在GitLab項目中,進入Settings -> CI/CD -> Runners,點擊“Register an instance runner”。
復制提供的Registration token。
在你的CentOS服務器上,以root用戶身份執行以下命令來注冊Runner:
sudo gitlab-runner register --non-interactive --executor "docker" --docker-image alpine:latest --url "http://your_server_ip" --registration-token "your_registration_token" --description "docker-runner" --tag-list "newdocker" --run-untagged "true" --locked "false" --docker-volumes /var/run/docker.sock:/var/run/docker.sock --docker-privileged "true" --access-level "not_protected"
在項目的根目錄下創建一個名為.gitlab-ci.yml
的文件,定義CI/CD流程。以下是一個簡單的示例:
stages:
- build
- test
- deploy
build:
stage: build
script:
- echo "Building the application..."
artifacts:
paths:
- build/
test:
stage: test
script:
- echo "Running tests..."
artifacts:
paths:
- test/
deploy:
stage: deploy
script:
- echo "Deploying the application..."
only:
- master
當你將代碼推送到GitLab倉庫時,GitLab Runner將自動執行.gitlab-ci.yml
文件中定義的流水線。你可以在GitLab CI/CD頁面上查看CI過程以及結果。
.gitlab-ci.yml
文件中的配置。以上步驟應該可以幫助你在CentOS上成功實踐GitLab CI/CD。