在Ubuntu上實施GitLab自動化測試,可按以下步驟操作:
安裝GitLab Runner
通過包管理器安裝:
curl -L --output /etc/apt/trusted.gpg.d/gitlab.asc https://packages.gitlab.com/gitlab/gitlab-runner/gpgkey
echo "deb https://packages.gitlab.com/gitlab/gitlab-runner/ubuntu $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/gitlab-runner.list
sudo apt-get update && sudo apt-get install gitlab-runner
或從源碼編譯安裝。
注冊GitLab Runner
獲取項目CI/CD設置中的URL和Token,執行命令注冊:
sudo gitlab-runner register --url <GitLab_URL> --registration-token <Token> --executor shell --description "Ubuntu Runner"
(支持Docker、Shell等多種執行器,根據需求選擇)。
創建.gitlab-ci.yml
文件
在項目根目錄編寫配置文件,定義測試階段和腳本。例如:
stages:
- test
test_job:
stage: test
script:
- npm install
- npm test # 若為Node.js項目
# 或使用其他測試框架(如Maven、Playwright)
artifacts:
reports:
junit: test-results.xml # 生成測試報告
(支持單元測試、集成測試等,可集成Playwright等工具)。
配置CI/CD管道
運行測試并查看結果
可選優化:
cache: paths: - node_modules/
)。參考來源: