GitLab-CI配置流水線部署

簡介

基本概念

GitLab-CI

GitLab-CI 即爲 GitLab Continuous Integration,也就是GitLab自帶的持續集成工具。
其思想就是每次用戶push代碼到GitLab上時觸發執行gitlab-ci.yml 腳本,腳本的內容包括了測試,編譯,部署等一系列自定義的內容。

GitLab-Runner

安裝

$ curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash

$ sudo yum install gitlab-ci-multi-runner

詳細可參考GitLab runner官網安裝教程

註冊

向GitLab-CI註冊一個Runner需要兩樣東西:GitLab-CI的url和註冊token。可以在項目代碼倉庫中找到

找到token之後,運行下面這條命令註冊Runner

$ sudo gitlab-ci-multi-runner register

按照提示輸入URL

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

輸入token

Enter the token you obtained to register the Runner:
Please enter the gitlab-ci token for this runner
xxx

輸入描述(此描述後續可在界面上修改)

Enter a description for the Runner, you can change this later in GitLab's UI:
Please enter the gitlab-ci description for this runner
[hostame] my-runner

輸入與這個runner相關的tag,gitlab-ci.yml可通過此tag觸發該runner構建流水線任務(此處也可後續在界面修改)

Enter the tags associated with the Runner, you can change this later in GitLab's UI:
Please enter the gitlab-ci tags for this runner (comma separated):
my-tag,another-tag

是否允許沒有tag的任務執行

Choose whether the Runner should pick up jobs that do not have tags, you can change this later in GitLab's UI (defaults to false):
Whether to run untagged jobs [true/false]:
[false]: true

是否對當前項目鎖定Runner

Choose whether to lock the Runner to the current project, you can change this later in GitLab's UI. Useful when the Runner is specific (defaults to true):
Whether to lock Runner to current project [true/false]:
[true]: true

選擇Runner的執行器

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

如果上一步選擇了docker,這一步需要填寫

If you chose Docker as your executor, you'll be asked for the default image to be used for projects that do not define one in .gitlab-ci.yml:
Please enter the Docker image (eg. ruby:2.1):
alpine:latest

詳細可參考官方文檔

註冊完成之後,GitLab-CI就會多出一條Runner記錄,GitLab-CI會爲這個Runner生成一個唯一的token,以後Runner就通過這個token與GitLab-CI進行通信。
然後點擊頁面右側Shared Runners中的按鈕,使按鈕狀態變爲Disabled Shared Runners。

$ sudo gitlab-ci-multi-runner list/verify

配置

gitlab-ci.yml

這個是在git項目的根目錄下的一個文件,記錄了一系列的階段和執行規則,包含一系列的執行腳本和指定的runner名稱。GitLab-CI在push後會解析它,根據裏面的內容調用runner來運行。

詳細可參考官方文檔

啓動

sudo gitlab-ci-multi-runner start 後臺啓動
sudo gitlab-ci-multi-runner run 前臺啓動
sudo gitlab-ci-multi-runner status 查看狀態
sudo gitlab-runner -debug run 前臺啓動(調試模式,會在console輸出log)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章