1. 程式人生 > >gitlab之gitlab-ci和gitlab-runner<一>

gitlab之gitlab-ci和gitlab-runner<一>

h+ mac str centos7 coord interval b- 地址 style

一、概述

gitlab是開源代碼托管軟件,有ce和ee兩種版本,一般情況下ce完全能滿足企業使用,如果不差錢可以使用ee版本,這裏使用的是ce版。之前也一直在做CI/CD,最開始采用gitlab+webhook+jenkins,但是這種組合略顯笨重,後來經過一番探索原來gitlab早就自己做好了這一切,那就是gitlab-ci和gitlab-runner,gitlab-ci在安裝gitlab的時候默認已經安裝了,所以無需再次安裝只需要安裝一下gitlab-runner。

!gitlab-ci就是持續集成,每一次push代碼,就觸發一次構建流程包括測試、編譯、(打包)、部署等一系列的內容,這個流程是pipeline執行一系列的腳本構成,根據自己的項目需求制作相應的流程和編寫相關的腳本,.gitlab-ci.yml的腳本解析就由它來負責。

!gitlab-runner是腳本執行的地方,push代碼後,gitlab-ci會解析.gitlab-ci.yml,然後根據pipeline規則在相應的runner上執行相應的腳本。

!.gitlab-ci.yml是在git項目的根目錄下的一個文件,記錄了一系列的階段和執行規則。GitLab-CI在push後會解析它,根據裏面的內容調用runner來運行,項目使用gitlab-ci要在相應的項目裏面加上這個文件。

二、部署

1、安裝gitlab

安裝環境centos7

使用yum的方式安裝gitlab-ce,設置repo:

#cat /etc/yum.repos.d/gitlab-ce.repo

[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1

#yum provides gitlab-ce

選擇想要使用的版本盡享安裝即可,這裏我安裝的是gitlab-ce-11.2.2-ce.0.el7.x86_64。

#yum -y install gitlab-ce-11.2.2-ce.0.el7.x86_64

結下來就是進行初始化、啟動,首先要修改初始化文件,一般是在/etc/gitlab/gitlab.rb,主要是修改如下幾項:

external_url ‘http://10.2.6.7‘,#這個是gitlab url,就是以後訪問要使用的,所以一定要設置好,嚴格按照格式設置,"http://"不能省略,生產環境建議使用域名。

郵件設置:

gitlab_rails[‘smtp_enable‘] = true
gitlab_rails[‘smtp_address‘] = "smtp.exmail.qq.com"
gitlab_rails[‘smtp_port‘] = 25
gitlab_rails[‘smtp_user_name‘] = "[email protected]"
gitlab_rails[‘smtp_password‘] = "1qaz*@WSX"
gitlab_rails[‘smtp_domain‘] = "app.com"
gitlab_rails[‘smtp_authentication‘] = "login"
gitlab_rails[‘smtp_enable_starttls_auto‘] = true
gitlab_rails[‘smtp_tls‘] = false

gitlab_rails[‘gitlab_email_enabled‘] = true
gitlab_rails[‘gitlab_email_from‘] = ‘[email protected]
gitlab_rails[‘gitlab_email_display_name‘] = ‘gitlab‘
gitlab_rails[‘gitlab_email_reply_to‘] = ‘[email protected]
gitlab_rails[‘gitlab_email_subject_suffix‘] = ‘dd‘

gitlab_rails[‘time_zone‘] = ‘Asia/Shanghai‘

還有就是數據目錄存放,默認存放到/var/lib/gitlab下面,時間久了可能導致磁盤空間不夠用,可以修改為其他較大空間的目錄。

啟動:

#gitlab-ctl reconfigure

啟動完成後,查看一下狀態:

技術分享圖片

出現以上證明啟動成功,使用上面設置的external_url ‘http://10.2.6.7‘,訪問一下,生產環境建議設置一個域名,使用nginx代理一下。

首次訪問會提示更新密碼,設置一個新的密碼就可以了,默認用戶名root。

2、安裝gitlab-runner

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

#yum provides gitlab-runner

選擇匹配gitlab-ci的版本,這裏選擇gitlab-runner-11.2.0-1.x86_64

#yum -y install gitlab-runner-11.2.0-1.x86_64

2.1、註冊gitlab-runner

參考:https://docs.gitlab.com/runner/register/index.html

根據提示填寫即可。

#gitlab-runner register

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://10.42.68.77
Please enter the gitlab-ci token for this runner:
_9cgUcGks7zQ-2swKmhL
Please enter the gitlab-ci description for this runner:
[ui]: sp
Please enter the gitlab-ci tags for this runner (comma separated):
ll
Registering runner... succeeded runner=_9cgUcGk
Please enter the executor: parallels, docker-ssh+machine, kubernetes, docker, docker-ssh, virtualbox, docker+machine, shell, ssh:
docker
Please enter the default Docker image (e.g. ruby:2.1):
apline:latest

#這裏的url填寫gitlab的訪問地址,token是在gitlab管理頁面獲得:

技術分享圖片

executor選擇使用docker,上面設置完成後就註冊完成了。

技術分享圖片

接下來是修改配置文件,runner註冊完成後,會生成一個配置文件/etc/gitlab-runner/config.toml.

concurrent = 1
check_interval = 0

[[runners]]
  name = "ui"
  url = "http://10.42.68.77"
  token = "927ad1dacc341bc977836856a8533e"
  executor = "docker"
  cache_dir = "/data/tmp/cache"
  [runners.docker]
    tls_verify = false
    image = "docker:dind"
    privileged = false
    disable_cache = false
    volumes = ["/var/run/docker.sock:/var/run/docker.sock","/data/tmp/cache:/data/tmp/cache"]
    cache_dir = "/data/tmp/cache"
    shm_size = 0
  [runners.cache]

concurrent = 1

#設置一個runner並發jobs,concurrent = 20,表示這個runner可以同時最多執行20個job。

下面貼一份dind的配置:

cat /etc/gitlab-runner/config.toml

concurrent = 8
check_interval = 0

[[runners]]
  name = "kn2-saturn"
  url = "http://glab.nget.com/"
  token = "443ffc1bdaa0be1983c49b88e61"
  executor = "docker"
  cache_dir = "/data/tmp/cache"
  [runners.docker]
    tls_verify = false
    image = "docker:dind"
    privileged = false
    disable_cache = false
    volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/data/tmp/cache:/data/tmp/cache"]
    cache_dir = "/data/tmp/cache"
    shm_size = 0
  [runners.cache]

[[runners]]
  name = "kn2-shared"
  url = "http://gitlab.xinpinget.com/"
  token = "53abc8c08c14f386a40dd807853bc6"
  executor = "docker"
  cache_dir = "/data/tmp/cache"
  environment = ["httpProxy=http://10.4.15.13:8118", "no_proxy=localhost,.local,127.0.0.1,0.0.0.0,registry.npm.taobao.org"]
  [runners.docker]
    tls_verify = false
    image = "docker:dind"
    privileged = false
    disable_cache = false
    volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/data/tmp/cache:/data/tmp/cache"]
    cache_dir = "/data/tmp/cache"
    shm_size = 0
  [runners.cache]

[[runners]]
  name = "kn2,kubernetes"
  url = "http://gitb.nget.com"
  token = "ce6d7c6c6bf96c05dd3ed9a3edb0"
  executor = "kubernetes"
  [runners.cache]
  [runners.kubernetes]
    host = ""
    bearer_token_overwrite_allowed = false
    image = ""
    namespace = ""
    namespace_overwrite_allowed = ""
    privileged = false
    service_account_overwrite_allowed = ""
    pod_annotations_overwrite_allowed = ""
    [runners.kubernetes.volumes]

gitlab之gitlab-ci和gitlab-runner<一>