1、概述html
gitlab是開源代碼託管軟件,有ce和ee兩種版本,通常狀況下ce徹底能知足企業使用,若是不差錢可使用ee版本,這裏使用的是ce版。以前也一直在作CI/CD,最開始採用gitlab+webhook+jenkins,可是這種組合略顯笨重,後來通過一番探索原來gitlab早就本身作好了這一切,那就是gitlab-ci和gitlab-runner,gitlab-ci在安裝gitlab的時候默認已經安裝了,因此無需再次安裝只須要安裝一下gitlab-runner。nginx
!gitlab-ci就是持續集成,每一次push代碼,就觸發一次構建流程包括測試、編譯、(打包)、部署等一系列的內容,這個流程是pipeline執行一系列的腳本構成,根據本身的項目需求製做相應的流程和編寫相關的腳本,.gitlab-ci.yml的腳本解析就由它來負責。git
!gitlab-runner是腳本執行的地方,push代碼後,gitlab-ci會解析.gitlab-ci.yml,而後根據pipeline規則在相應的runner上執行相應的腳本。web
!.gitlab-ci.yml是在git項目的根目錄下的一個文件,記錄了一系列的階段和執行規則。GitLab-CI在push後會解析它,根據裏面的內容調用runner來運行,項目使用gitlab-ci要在相應的項目裏面加上這個文件。docker
2、部署shell
一、安裝gitlabnpm
安裝環境centos7centos
使用yum的方式安裝gitlab-ce,設置repo:ruby
#cat /etc/yum.repos.d/gitlab-ce.repobash
[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'] = "ci@app.com"
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'] = 'ci@app.com'
gitlab_rails['gitlab_email_display_name'] = 'gitlab'
gitlab_rails['gitlab_email_reply_to'] = 'ci@app.com'
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。
二、安裝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.一、註冊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 = true 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 = true 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://glab.nget.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 = true 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 = true service_account_overwrite_allowed = "" pod_annotations_overwrite_allowed = "" [runners.kubernetes.volumes]