一、註冊Runner
root用戶執行:其中一個就行
gitlab-runner register
gitlab-ci-multi-runner register
會根據提示讓輸入URL 和 token
拿到url和token linux會提示輸入下面幾項 (兩個全輸入true)
註冊好以後,重啓runner
sudo gitlab-ci-multi-runner restart(後臺服務啓動)
回到gitlab頁面就是剛纔看url和token的頁面,就能看到如下
然後點擊鉛筆圖標編輯,進入編輯頁面,給這個runner分配項目,想分配哪個項目就點後面的 Enable,然後保存,點擊 save changes
保存完以後,就完成了項目和runner的關聯
現在就可以去寫個shell腳本用來拉取分支,然後通過yml文件去觸發shell腳本執行
shell腳本(自己存在服務器上,下面.yml文件填的就是這個shell腳本存放在服務器文章)
貼腳本代碼:(直接拷貝,然後只改上面三個參數,其他的不用動)
要拉取得分支名:就是你想把那個分支關聯到這臺服務器上
DEPLOY_PATH=項目根目錄路徑
GIT_URL=git克隆地址
NOW_BRANCH=要拉取的分支名
cd $DEPLOY_PATH
if [ -d "$DEPLOY_PATH/.git" ];then
echo --- Clean Workspace ---
git clean -f
git reset --hard
git remote update origin -p
git checkout $NOW_BRANCH
echo --- 拉取 $NOW_BRANCH 分支 ---
git pull origin $NOW_BRANCH
echo --- OK ---
else
git init
git remote add origin $GIT_URL
git pull origin $NOW_BRANCH
git remote update origin -p
git checkout $NOW_BRANCH
echo --- OK ---
fi
創建 .gitlab-ci.yml 文件
tags就是註冊時填寫的tag
貼上模板:
stages:
- deploy
xjzm_publish_test:
stage: deploy
tags:
- gzh_xjzm_test
only:
- develop
script:
- whoami
- /website/deploy/gzh_deploy.sh
xjzm_publish_product:
stage: deploy
tags:
- gzh_xjzm_product
only:
- master
script:
- whoami
- /website/deploy/gzh_deploy.sh
服務器操作--創建shell腳本文件。
注意:切換到gitlab-runner用戶(自動發佈用戶)