I-team 博客的 gitlab-runner 持續集成實踐


做為一個略微看過nodejs語法,但又不懂nodejs的攻城獅,搭建hexo環境很是麻煩,要考慮到翻牆版本兼容等問題。於是乎,博主每換一個電腦,為了能繼續發博客,都需要在新電腦上花一天時間重新搞一下 hexo 環境,樓主感覺還是有簡潔的方案來實現我一提交代碼就可以自動發布博客,不需要再手動操作一波,這樣豈不美哉。so,也就有了今天的經歷,代碼可以持續集成,博客也可以。樓主的解決方案是使用gitlab與gitlab-runner實現博客部署的持續集成,效果真的不要太好。

持續集成工具 gitlab-runner 介紹

gitlab-ci全稱是gitlab continuous integration的意思,也就是持續集成。中心思想是當每一次push到gitlab的時候,都會觸發一次腳本執行,然后腳本的內容包括了測試,編譯,部署等一系列自定義的內容。而gitlab-runner 是 gitlab 提供的持續集成工具。

簡單的說,要讓CI工作可總結為以下幾點:

  • 在倉庫根目錄創建一個名為.gitlab-ci.yml 的文件。
  • 為該項目配置一個runner服務,樓主這里使用的是使用gitlab提供代碼廠庫,在自己的騰訊雲服務器上運行gitlab-runner服務。
  • 完成上面的步驟后,每次push代碼到Git倉庫, runner就會自動開始pipeline。

gitlab-ci的具體部署流程如下圖所示(圖來自網絡,侵權刪)
gitlab-runner

Hexo 博客環境遷移

遷移前版本控制

其實每個nodejs工程根目錄下都有一個package.json文件,里面都包含了我們所用的插件信息,只需要我們在安裝插件的時候注意加上--save,就會自動把插件信息保存到 package.json 中。

如果目錄下沒有 package.json 文件也不要緊,在跟目錄命令行中運行 npm init 即可生成。

博客環境安裝

前面做好版本控制,那接下來的事情就好做了。

  1. 備份你的代碼,注意:代碼中不需要包含 node_modules 文件夾了
  2. 先在新電腦中裝上 nodejs 環境
  3. 由於國內安裝 npm 的一些插件需要翻牆,所以這里直接用淘寶鏡像:cnpm,安裝方法:npm install -g cnpm --registry=https://registry.npm.taobao.org
  4. 安裝hexo客戶端:cnpm install hexo-cli -g
  5. 新建博客目錄:hexo init
  6. 把你備份的代碼放到此目錄下,如果有重復的文件直接覆蓋就行
  7. 安裝 hexo 插件:cnpm install
    就這樣,新的博客環境遷移完成了,執行 hexo s 開始你新的博客征程吧!

gitlab-runner環境搭建

gitlab-runner的安裝

使用gitlab官網提供的下載地址太慢,所以找到了一個國內的鏡像地址:

  1. 新建 gitlab-ci-multi-runner.repo
touch /etc/yum.repos.d/gitlab-ci-multi-runner.repo
  1. 將以下內容寫入文件
[gitlab-ci-multi-runner]
name=gitlab-ci-multi-runner
baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ci-multi-runner/yum/el7
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key
  1. 執行
sudo yum makecache
sudo yum install gitlab-ci-multi-runner
  1. 以上是樓主在centos上的安裝過程,其他系統版本的安裝請移步gitlab-runner其他系統版本的安裝

gitlab-runner注冊到gitlab官網

在終端輸入**gitlab-runner register **會出現以下過程:

[root@localhost ~]# gitlab-runner register
Running in system-mode.                            

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
https://gitlab.com/
Please enter the gitlab-ci token for this runner:
your gitlab-ci token
Please enter the gitlab-ci description for this runner:
[localhost.localdomain]: my-runner
Please enter the gitlab-ci tags for this runner (comma separated):
your tag
Whether to run untagged builds [true/false]:
[false]: true
Registering runner... succeeded                     runner=c5552857
Please enter the executor: parallels, shell, virtualbox, docker+machine, docker-ssh+machine, docker, docker-ssh, ssh, kubernetes:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 

在注冊過程中有兩個比較重要的參數一個是gitlab的URL,另一個就是注冊的token,這兩個參數可以在gitlab上找到,過程是Settings>CI/CD>Runners settings>Specific Runners,如下圖所示
gitlab-runner-settings

另外還需要打開
gitlab-runner-settings

要是自己注冊的gitlab-runner生效還學要禁用Shared Runners

以上過程是樓主在centos上操作的,其他版本請移步gitlab-runner注冊到gitlab

創建.gitlab-ci.yml,並放着工程的根目錄下

.gitlab-ci.yml具體配置請移步官方文檔,下面給出樓主使用的.gitlab-ci.yml具體內容

variables:
  GIT_STRATEGY: none

stages:
  - build_and_deploy

job:
  stage: build_and_deploy
  script:
    - cd /opt/I-team-fly
    - git pull --tags origin dev
    - hexo clean
    - hexo g
    - hexo d
  only: 
    - dev

查看gitlab上的構建結果

gitlab-runner-settings

小結

當然這個過程中還是要涉及到幾次使用ssh-key來設置免密登錄,樓主就不在這里贅述了,請遇到問題的小伙伴自行Google。

參考文章


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM