Try   HackMD

Gitlab runner 1.11.4 設定筆記

僅作為個人使用的備忘錄,紀錄一些遇到的問題
並給需要在 gitlab 8 做設定的人參考

本篇設定時的版本: GitLab Community Edition 8.10.3

參考文章

[教學] 如何使用Gitlab CI來達到自動化測試與佈署

設定

docker

在設定 runner 前,必須先確保 docker 已經裝好了
安裝參考簡短筆記 docker 設定筆記 Ubuntu 16

直接用腳本裝 docker:

wget -qO- https://softwaresing.github.io/linux-install/docker.sh | bash

註冊 runner

以下指令使用 runner v1.11.4 做設定

docker run --rm -t -i -v /path/to/config:/etc/gitlab-runner --name gitlab-runner gitlab/gitlab-runner:v1.11.4 register

runner v1.11.4 為 gitlab 8 能用的最高版本
如果你的 gitlab 是更高的版本,可以替換成更高的來用

設定的問題可以看參考文章內的說明
以下僅記錄我有遇到的

設定時若出現 status=301 Moved Permanently 的錯誤訊息,要注意可能是 http 與 https 的問題
可以嘗試將第一個要輸入的URL改成 https ,也許就能通過

設定的最後2個問題
Please enter the executor: 建議用 docker 就好
Please enter the Docker image: 沒有指定的話預設使用的 image ,用 ubuntu:latest 即可

開啟 runner (開機自動啟動)

docker run -d --name gitlab-runner --restart always -v /path/to/config:/etc/gitlab-runner -v /var/run/docker.sock:/var/run/docker.sock gitlab/gitlab-runner:v1.11.4

開啟後即可在 runner 頁面看到機器有連上了

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

.gitlab-ci.yml

service

DB 等服務可以透過 service 增加,例如:

services: - mongo:latest - memcached:latest

他們就會開在同名的位置上,也就是 mongomemcached

mongodb://mongo/

cache

需注意 global cache 如果沒有設定 key ,每個 job 的 cache 會是分開的
(不確定後來的版本是否有修正)

cache: key: cache paths: - node_modules/

ssh

如果要使用 ssh ,只能使用 key ,不能用密碼
ssh key 設定參考: SSH 公開金鑰認證:不用打密碼登入 Linux 設定教學,安全又方便

CI指令參考
https://docs.gitlab.com/ee/ci/ssh_keys/

before_script: - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )' - eval $(ssh-agent -s) - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - - mkdir -p ~/.ssh - chmod 700 ~/.ssh script: - ssh -o StrictHostKeyChecking=no $DEPLOY_USER@$DEPLOY_SERVER 'echo "test" && echo "test2"'

ssh 連線後,直接執行後面字串的指令的話,是沒有部份關鍵字的,像是 nvm, node 等會找不到
如需要用 nvm ,可以在腳本前加入以下指令

. ~/.nvm/nvm.sh
 - ssh -o StrictHostKeyChecking=no $DEPLOY_USER@$DEPLOY_SERVER '. ~/.nvm/nvm.sh && echo "node version:" `node --version`'

ssh 的問題可以參考以下連結
https://unix.stackexchange.com/questions/332532/how-to-set-path-when-running-a-ssh-command