DevOps GitLab CICD 實踐2——Runner 部署

前置步驟:DevOps GitLab CICD 實踐1——GitLab 部署html

官方文檔

文檔地址git

雖然有官方指引,但我的感受指引的不夠清晰,致使初次配置可能頻繁失敗docker

Run GitLab Runner in a container

This is how you can run GitLab Runner inside a Docker container.bash

General GitLab Runner Docker image usage

GitLab Runner Docker images (based on Ubuntu or Alpine Linux) are designed as wrappers around the standard gitlab-runner command, like if GitLab Runner was installed directly on the host.app

The general rule is that every GitLab Runner command that normally would be executed as:curl

gitlab-runner [Runner command and options...]
複製代碼

can be executed with:ide

docker run [chosen docker options...] gitlab/gitlab-runner [Runner command and options...]
複製代碼

For example, getting the top-level help information for GitLab Runner command could be executed as:gitlab

docker run --rm -t -i gitlab/gitlab-runner --help

NAME:
   gitlab-runner - a GitLab Runner

USAGE:
   gitlab-runner [global options] command [command options] [arguments...]

VERSION:
   10.7.0 (7c273476)

(...)
複製代碼

In short, the gitlab-runner part of the command is replaced with docker run [docker options] gitlab/gitlab-runner, while the rest of Runner’s command stays as it is described in the register documentation. The only difference is that the gitlab-runner command is executed inside of a Docker container.post

Docker image installation and configuration

  1. Install Docker first:性能

    curl -sSL https://get.docker.com/ | sh
    複製代碼
  2. You need to mount a config volume into the gitlab-runner container to be used for configs and other resources:

    docker run -d --name gitlab-runner --restart always \
       -v /srv/gitlab-runner/config:/etc/gitlab-runner \
       -v /var/run/docker.sock:/var/run/docker.sock \
       gitlab/gitlab-runner:latest
    複製代碼

    Tip: On macOS, use /Users/Shared instead of /srv.

    Or, you can use a config container to mount your custom data volume:

    docker run -d --name gitlab-runner-config \
         -v /etc/gitlab-runner \
         busybox:latest \
         /bin/true
    複製代碼

    And then, run the Runner:

    docker run -d --name gitlab-runner --restart always \
         -v /var/run/docker.sock:/var/run/docker.sock \
         --volumes-from gitlab-runner-config \
         gitlab/gitlab-runner:latest
    複製代碼
  3. Register the runner you just launched by following the instructions in the Docker section of Registering Runners. The runner won’t pick up any jobs until it’s registered.

安裝步驟

獲取Gitlab Runner祕鑰

能夠經過Gitlab管理員帳號獲取,也能夠讓每個用戶自行配置

  • 普通用戶查看祕鑰

進入任意一個倉庫的設置中,查看CICD配置

1554266163188.png

準備註冊專用Runner令牌

1554266217632.png

  • 管理員查看令牌

進入總設置頁面,配置全局Runner令牌

1554266287842.png

Runner註冊

因爲Runner通常運行復雜構建、打包任務,推薦配置在性能、帶寬更大的機房

準備Docker環境

$ curl -sSL https://get.docker.com/ | sh
$ systemctl start docker
複製代碼

註冊

能夠根據須要選擇註冊Runner類型

同時,爲了方便配置,使用單行註冊而且關閉交互

單行註冊官方文檔

命令說明:

  • -v掛載的目的是爲了將註冊後的配置文件持久化,用於運行容器
  • --rm指定容器運行結束後自動刪除中止的容器
  • -it指定使用命令行交互方式運行,便於查看註冊結果
$ docker run --rm -it \
  -v /www/wwwroot/gitlab/srv/gitlab-runner/config:/etc/gitlab-runner \
  gitlab/gitlab-runner:alpine-v11.8.0 register \
  --non-interactive \
  --executor "docker" \
  --docker-image docker:stable \
  --url "Gitlab URL" \
  --registration-token "令牌" \
  --description "描述" \
  --tag-list "標籤1,標籤2" \
  --run-untagged \
  --docker-privileged \
  --locked="false"
複製代碼

註冊成功提示

1554277014308.png

此時管理面板顯示新的Runner已經註冊

1554277086811.png

Runner 啓動

掛載本地配置信息並啓動Runner

$ docker run -d --name gitlab-runner --restart always \
 	-v /www/wwwroot/gitlab/srv/gitlab-runner/config:/etc/gitlab-runner \
    -v /var/run/docker.sock:/var/run/docker.sock \
    gitlab/gitlab-runner:alpine-v11.8.0
複製代碼

此時可見Runner已經保持和Gitlab的聯繫

1554277457677.png
相關文章
相關標籤/搜索