1. 程式人生 > >【Gitlab】gitlab-CI 持續整合以及runner的配置簡版

【Gitlab】gitlab-CI 持續整合以及runner的配置簡版

在我們完成專案開發後,提交到git,當監聽提交後,自動進行編譯,並進行專案的部署,是不是一想就很爽,所以下面引入我們 > 的主角 —— gitlab-CI,中文文件

Gitlab CI

Gitlab-CI 是 GitLab Continuous Integration(Gitlab持續整合)的簡稱。
從Gitlab的8.0版本開始,gitlab就全面集成了Gitlab-CI,並且對所有專案預設開啟。
只要在專案倉庫的根目錄新增.gitlab-ci.yml檔案,並且配置了Runner(執行器),那麼每一次合併請求(MR)或者push都會觸發CI pipeline。

Gitlab-Runner

安裝gitlab-ci-multi-runner(linux)

# 新增yum源
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash

# 安裝
yum install gitlab-ci-multi-runner

使用 gitlab-ci-multi-runner 註冊 Specific Runners

runners.jpg

$ gitlab-ci-multi-runner register
Running in system-mode.

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
>>>>>> 你的URL

Please enter the gitlab-ci token for this runner:
>>>>>> 你的Token

Please enter the gitlab-ci description for this runner:
>>>>>> 你對該runner的描述

Please enter the gitlab-ci tags for this runner (comma separated):
>>>>>> 該runner的標籤

Whether to run untagged builds [true/false]:
[false]: true

>>>>>> 是否執行沒有標記的專案(後期可以改)

Whether to lock Runner to current project [true/false]:
[false]:

>>>>>> 是否鎖定只運行當前專案(後期可以改)

Registering runner... succeeded                     runner=9e48baqx

Please enter the executor: parallels, docker+machine, ssh, virtualbox, docker-ssh+machine, kubernetes, docker, docker-ssh, shell:

>>>>>> 選擇runner的型別(根據你的需求選擇,我選擇的shell,我的專案是node專案)

Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
  1. 首先你的專案中的 getlab-runner.yml 檔案中的命令需要的環境,你的runner所在的伺服器是必須要搭好的,必須你需要執行 npm installnpm run build ,那你的伺服器必須要有node環境;
  2. runner的型別比較重要,你必須要清楚你需要一個什麼型別的runner。比如,開始我是需要一個能夠執行node專案的runner,但是網上的很多教程統一都是都選擇docker,這個時候你電腦上必須要安裝了docker才可以,因為這相當於在docker裡面又安裝了一個docker(docker in docker),所以對於我的需求來說,我選擇shell就可以了;
  3. 通過Gitlab中某個專案的 Settings 下的 CI/CD Pipelines 按鈕進入的頁面中得到的URL和Token所註冊的runner只服務於本專案,當你在其他專案的當前頁面時,可以選擇讓這個runner為當前專案開啟服務。

CI/CD Pipelines 頁

配置 gitlab-runner.yml

mage: node:9.11.1

pages:
 cache:
   paths:
   - node_modules/

 script:
 - cnpm install
 - npm run build
 - sudo rm -rf /data/ftp/http/files/scfAdminDoc
 - sudo mv ./scfAdminDoc /data/ftp/http/files/
 artifacts:
   paths:
   - public
 only:
 - master

具體配置見文件

其他

程式碼推送到gitlab上之後,一直是pending狀態,要等很久才pick到一個runner來執行

這個現象一般發生在本機註冊了多個runner,之前的都刪除了,只留下一個,但是當我們在gitlab管理平臺移除了某個runner的時候,並沒有完全刪除記錄,當執行的 gitlab-ci-multi-runner list 的時候你會發現它仍然還在,如果需要徹底刪除它,還應該這樣做:

cd /etc/gitlab-runner
ls

# 你會看到config.toml檔案,開啟它並刪除已有的那些runner記錄
vim config.toml

配置node環境

  1. 下載node-v9.11.1-linux-x64.tar.xz檔案
  2. 例如放在 /home 資料夾
    ```bash
    # 進入所在目錄,解壓
    tar -xvf node-v9.11.1-linux-x64.tar.xz

    # 重新命名資料夾
    mv node-v9.11.1-linux-x64 nodejs

    # 建立軟連結,配置全域性環境變數(源路徑和目標路徑為相對路徑)
    ln -s /home/nodejs/bin/npm /usr/local/bin/
    ln -s /home/nodejs/bin/node /usr/local/bin/

    # 檢查是否配置成功
    node -v
    npm -v
    ```

scp上上傳

# 普通
scp ./public/* [email protected]:/data/ftp/http/files/
scp 源路徑/原始檔 使用者名稱(root)@伺服器IP:目標路徑


# 免密碼
yum install sshpass
sshpass -p 111111 scp ./public/* [email protected]:/data/ftp/http/files/
sshpass -p 密碼 scp 源路徑/原始檔 使用者名稱(root)@伺服器IP:目標路徑

如果是下載,調換順序

scp [email protected]:/data/ftp/http/files/ ./public/*
scp 使用者名稱(root)@伺服器IP:目標路徑 源路徑/原始檔

個人部落格:午後南雜