在Debian上實現GitLab持續集成與部署,核心步驟如下:
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
sudo apt install gitlab-ce
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
訪問 http://服務器IP
完成初始設置(設置管理員密碼、域名等)。curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash
sudo apt install gitlab-ci-multi-runner
sudo gitlab-runner register
輸入GitLab地址、項目Token等信息,選擇執行器類型(如Shell、Docker)。.gitlab-ci.yml
文件在項目根目錄創建配置文件,定義流水線階段(構建、測試、部署)和任務。示例:
stages:
- build
- test
- deploy
build_job:
stage: build
image: node:18
script:
- npm install
artifacts:
paths:
- node_modules/
test_job:
stage: test
image: node:18
script:
- npm test
deploy_job:
stage: deploy
image: alpine
script:
- echo "Deploying to production..."
- scp -r . user@server:/path/to/deploy/
only:
- main # 僅主分支觸發部署
image
:指定Docker鏡像(如Node.js、Python環境)。script
:執行具體命令(構建、測試、部署)。artifacts
:緩存構建產物,供后續階段使用。only
/except
:控制分支觸發條件。kubectl
命令。參考來源: