在 Docker 容器中運行 Gitlab Runner

環境:CentOS 7
目的:在 Docker container 中運行 Gitlab Runner,搭建 Gitlab-CI 環境linux

搭建 docker 運行環境

$ sudo yum install yum-utils device-mapper-persistent-data lvm2
$
$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
$ sudo yum install docker-ce
$
$ sudo systemctl enable docker
$ sudo systemctl start docker
複製代碼

安裝 Runner

安裝步驟:git

  1. 安裝 Gitlab 官方庫
# For RHEL/CentOS/Fedora
 $ curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash
複製代碼
  1. 安裝最新版本的 Gitlab Runner
# For RHEL/CentOS/Fedora
$ sudo yum install gitlab-runner
複製代碼

註冊 Runner

執行註冊命令:docker

$ docker run --rm -t -i -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register \
 --non-interactive \
 --executor "docker" \
 --docker-image alpine:3 \
 --url "https://gitlab.com/" \
 --registration-token "PROJECT_REGISTRATION_TOKEN" \
 --description "docker-runner" \
 --tag-list "docker,aws" \
 --run-untagged \
 --locked="false"
複製代碼

註冊完成後,以下配置文件會被 Runner 自動加載:centos

## 配置文件路徑爲: /srv/gitlab-runner/config
 [[runners]]
  name = "gitlab-runner"
  url = "https://gitlab.com/"
  token = "PROJECT_REGISTRATION_TOKEN"
  executor = "docker"
  [runners.docker]
    tls_verify = false
    image = "alpine:latest"
    privileged = false
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache"]
    shm_size = 0
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
複製代碼

啓動 Runner

$ 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
複製代碼

參數說明:bash

  • -d: 設置容器後臺運行
  • --name:容器名稱
  • -restart always:每次啓動容器就重啓 gitlab-runner
  • -v: 共享目錄掛載

建立 .gitlab-cli.yml 文件

建立文檔示例請點擊這裏app

附:

  • 啓動完成後,執行 $ docker ps 查看容器運行狀況
  • 查看運行日誌
## gitlab-runner 是容器的名字,第一行命令 `--name gitlab-runner`
$ docker logs gitlab-runner
複製代碼
  • 中止或者移除容器
$ docker stop gitlab-runner && docker rm gitlab-runner
複製代碼
相關文章
相關標籤/搜索