gitlab CI/CD環境搭建
阿新 • • 發佈:2018-11-22
1.安裝gitlab-runner
# Linux x86-64 sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64 sudo chmod +x /usr/local/bin/gitlab-runner sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner sudo gitlab-runner start
以上是Linux64位環境下的安裝,其他的環境參考 https://docs.gitlab.com/runner/install/linux-manually.html
2.註冊gitlab-runner
sudo gitlab-runner register
他會提示你寫gitlab的地址和token,這地址可以在gitlab的網頁上的settings->CI/CD Pipelines 找到如圖:
另外,配置好的runner可能需要開啟-Run untagged jobs,同樣在上圖所示的頁面中有一個Runners activated for this project列表,點選下面runner右側的修改按鈕,在頁面中把 Run untagged jobs勾選即可
其他具體參考:https://docs.gitlab.com/runner/register/index.html
3.在專案根目錄中新增 .gitlab-ci.yml 如下:
# 提交dev分支時觸發
dev job:
only:
- dev
script:
- echo 'dev' >> ci.log
# 提交test分支時觸發
test job:
only:
- test
script:
- ssh [email protected] -i /home/gitlab-runner/.ssh/ci_key << ssh2
- date >> ci.log
- cd /home/ubuntu/first_project/
- git checkout test
- git pull origin test
- ssh2
# 提交master分支時觸發
master job:
only:
- master
script:
- ssh [email protected] -i /home/gitlab-runner/.ssh/ci_key << ssh2
- date >> ci.log
- cd /home/ubuntu/first_project/
- git checkout master
- git pull origin master
- ssh2
然後,本地提交程式碼後runner就會執行.yml裡相關指令碼