[轉]gitlab ci/cd 釋出
轉自https://meigit.readthedocs.io/en/latest/configure_gitlab_i18n_and_create_gitlab_ci_with_gitlab_runner.html
目錄
- GitLab的漢化與CI持續整合gitlab-runner的配置
- 實驗環境
- GitLab環境檢查
- GitLab漢化
- 配置CI/CD
- 什麼是GitLab Runner
- 安裝GitLab Runner執行器
- 註冊GitLab Runner
- 由於多次註冊,Runner ID不是從1開始處理方法
- 啟動執行器
- 觸發GitLab Runner執行流水線任務
本文講解在CentOS7安裝GitLab(使用外部Nginx配置)
實驗環境
- server服務端: 作業系統為CentOS 7.6,IP:192.168.56.14, git:2.16.5。
檢視server服務端資訊:
[root@server ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
[root@server ~]# ip a show|grep 192
inet 192.168.56.14/24 brd 192.168.56.255 scope global noprefixroute enp0s3
[root@server ~]# git --version
git version 2.16.5
GitLab使用者資訊:
賬號 密碼
root 1234567890
meizhaohui 1234567890
- 虛擬機器因為重啟了,重新檢查一下GitLab能否正常執行。
GitLab環境檢查
進行檢查:
[root@server ~]# systemctl status nginx ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2019-06-27 19:58:19 CST; 57min ago Process: 13664 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 13660 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 13659 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) Main PID: 13666 (nginx) Tasks: 2 Memory: 10.5M CGroup: /system.slice/nginx.service ├─13666 nginx: master process /usr/sbin/nginx └─13667 nginx: worker process Jun 27 19:58:18 server.hopewait systemd[1]: Starting The nginx HTTP and reverse proxy server... Jun 27 19:58:19 server.hopewait nginx[13660]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok Jun 27 19:58:19 server.hopewait nginx[13660]: nginx: configuration file /etc/nginx/nginx.conf test is successful Jun 27 19:58:19 server.hopewait systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument Jun 27 19:58:19 server.hopewait systemd[1]: Started The nginx HTTP and reverse proxy server.
Nginx服務正常執行,訪問GitLab服務http://192.168.56.14,發現可以正常訪問:
我們使用meizhaohui這個賬號來下載hellopython專案:
D:\Desktop $ git clone [email protected]:higit/hellopython.git Cloning into 'hellopython'... remote: Enumerating objects: 9, done. Receiving objects: 100% (9/9), done. (9/9) remote: Counting objects: 100% (9/9), done. remote: Compressing objects: 100% (3/3), done. remote: Total 9 (delta 0), reused 0 (delta 0)
說明仍然可以下載,GitLab執行正常。
GitLab漢化
下面我們開始今天的主題,進行GitLab的漢化和CI的配置。
檢視GitLab版本:
[root@server ~]# cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
11.10.0
檢視GitLab中文社群官方倉庫https://gitlab.com/xhang/gitlab, 檢查對應的漢化包版本資訊:
我們下載GitLab版本對應的漢化包,11-10-stable-zh這個版本:
[root@server ~]# git clone https://gitlab.com/xhang/gitlab.git -b 11-10-stable-zh
Cloning into 'gitlab'...
remote: warning: ignoring extra bitmap file: /var/opt/gitlab/git-data/repositories/@pools/04/ad/04ad22d7630382dd5ece1410d2d8a131c44bdf54b53eb6b22a0276994b836d53.git/objects/pack/pack-573496940d56eadcba5a8d435e5b0f2345c9f918.pack
remote: Enumerating objects: 979008, done.
remote: Counting objects: 100% (979008/979008), done.
remote: Compressing objects: 100% (203291/203291), done.
remote: Total 979008 (delta 764750), reused 977616 (delta 763404)
Receiving objects: 100% (979008/979008), 400.57 MiB | 36.88 MiB/s, done.
Resolving deltas: 100% (764750/764750), done.
下載完後,檢視的漢化包資訊:
[root@server ~]# ls -lad gitlab/
drwxr-xr-x. 29 root root 4096 Jun 27 21:33 gitlab/
[root@server ~]# cd gitlab/
[root@server gitlab]# git remote -v
origin https://gitlab.com/xhang/gitlab.git (fetch)
origin https://gitlab.com/xhang/gitlab.git (push)
[root@server gitlab]# git branch
* 11-10-stable-zh
停止GitLab相關服務:
[root@server ~]# gitlab-ctl stop
ok: down: alertmanager: 0s, normally up
ok: down: gitaly: 0s, normally up
ok: down: gitlab-monitor: 0s, normally up
ok: down: gitlab-workhorse: 0s, normally up
ok: down: logrotate: 1s, normally up
ok: down: node-exporter: 0s, normally up
ok: down: postgres-exporter: 1s, normally up
ok: down: postgresql: 0s, normally up
ok: down: prometheus: 0s, normally up
ok: down: redis: 0s, normally up
ok: down: redis-exporter: 0s, normally up
ok: down: sidekiq: 0s, normally up
ok: down: unicorn: 0s, normally up
[root@server ~]# systemctl stop gitlab-runsvdir
[root@server ~]# systemctl stop nginx
[root@server ~]# ps -ef|grep gitlab
root 26384 13568 0 21:46 pts/0 00:00:00 grep --color=auto gitlab
[root@server ~]# ps -ef|grep nginx
root 26386 13568 0 21:46 pts/0 00:00:00 grep --color=auto nginx
說明GitLab相關服務已經停止。
備份/opt/gitlab/embedded/service/gitlab-rails/
資料夾,防止後續操作失敗導致GitLab無法執行:
[root@server ~]# cp -rf /opt/gitlab/embedded/service/gitlab-rails/ /opt/gitlab/embedded/service/gitlab-rails.bak
# 檢查是否備份成功
[root@server ~]# ls -lad /opt/gitlab/embedded/service/gitlab-rails*
drwxr-xr-x 24 root root 4096 Jun 23 14:56 /opt/gitlab/embedded/service/gitlab-rails
drwxr-xr-x 24 root root 4096 Jun 27 21:49 /opt/gitlab/embedded/service/gitlab-rails.bak
[root@server ~]# du -sh /opt/gitlab/embedded/service/gitlab-rails*
253M /opt/gitlab/embedded/service/gitlab-rails
253M /opt/gitlab/embedded/service/gitlab-rails.bak
去除cp的別名,複製gitlab漢化包中的檔案到/opt/gitlab/embedded/service/gitlab-rails/
目錄下:
[root@server ~]# alias cp alias cp='cp -i' [root@server ~]# unalias cp [root@server ~]# cp -rf gitlab/* /opt/gitlab/embedded/service/gitlab-rails/ cp: cannot overwrite non-directory ‘/opt/gitlab/embedded/service/gitlab-rails/log’ with directory ‘gitlab/log’ cp: cannot overwrite non-directory ‘/opt/gitlab/embedded/service/gitlab-rails/tmp’ with directory ‘gitlab/tmp’
使配置生效:
[root@server ~]# systemctl start gitlab-runsvdir [root@server ~]# gitlab-ctl reconfigure ...... 執行劇本,忽略 Running handlers: Running handlers complete Chef Client finished, 5/609 resources updated in 01 minutes 10 seconds gitlab Reconfigured! [root@server ~]#
啟動GitLab和Nginx:
[root@server ~]# gitlab-ctl start
ok: run: alertmanager: (pid 22346) 697s
ok: run: gitaly: (pid 22326) 697s
ok: run: gitlab-monitor: (pid 22340) 697s
ok: run: gitlab-workhorse: (pid 22334) 697s
ok: run: logrotate: (pid 22336) 697s
ok: run: node-exporter: (pid 22338) 697s
ok: run: postgres-exporter: (pid 22348) 697s
ok: run: postgresql: (pid 22328) 697s
ok: run: prometheus: (pid 22344) 697s
ok: run: redis: (pid 22324) 697s
ok: run: redis-exporter: (pid 22342) 697s
ok: run: sidekiq: (pid 22332) 697s
ok: run: unicorn: (pid 22330) 697s
[root@server ~]# systemctl start nginx
[root@server ~]#
訪問GitLab服務http://192.168.56.14,發現可以正常訪問,並顯示中文的頁面:
點選右上角的個人圖示,在彈出的下拉選項中點選Settings
進入到Settings
設定介面:
點選左側的preferences
標籤頁,進入到個人偏好設定介面,下拉到Localization
本地化的位置:
點選Language
語言下拉框選擇 “簡體中文”,並將週一設定為每週的第一天,並點選Savechanges
儲存修改:
儲存後,按F5重新整理一下頁面,可以看到頁面顯示已經變成中文了:
修改影象時,儲存時,提示 “Request failed with status code 500” 異常,檢視日誌資訊:
[root@server ~]# tail -f /var/log/nginx/gitlab_error.log
2019/06/29 19:13:07 [crit] 24457#0: *206 open() "/var/lib/nginx/tmp/client_body/0000000001" failed (13: Permission denied), client: 192.168.56.1, server: 192.168.56.14, request: "POST /profile HTTP/1.1", host: "192.168.56.14", referrer: "http://192.168.56.14/profile"
2019/06/29 19:13:51 [crit] 24457#0: *207 open() "/var/lib/nginx/tmp/client_body/0000000002" failed (13: Permission denied), client: 192.168.56.1, server: 192.168.56.14, request: "POST /profile HTTP/1.1", host: "192.168.56.14", referrer: "http://192.168.56.14/profile"
2019/06/29 19:15:37 [crit] 24457#0: *212 open() "/var/lib/nginx/tmp/client_body/0000000003" failed (13: Permission denied), client: 192.168.56.1, server: 192.168.56.14, request: "POST /profile HTTP/1.1", host: "192.168.56.14", referrer: "http://192.168.56.14/profile"
發現許可權不夠,我們檢視一下相關目錄的許可權:
[root@server ~]# ls -lah /var/lib/nginx/tmp/
total 0
drwx------. 7 root root 78 May 10 16:10 .
drwx------. 3 root root 17 May 10 16:10 ..
drwx------. 2 nginx root 6 Jun 22 23:04 client_body
drwx------. 2 nginx root 6 Jun 22 23:04 fastcgi
drwx------. 2 nginx root 6 Jun 22 23:04 proxy
drwx------. 2 nginx root 6 Jun 22 23:04 scgi
drwx------. 2 nginx root 6 Jun 22 23:04 uwsgi
[root@server ~]# ls -lad /var/lib/nginx/tmp/
drwx------. 7 root root 78 May 10 16:10 /var/lib/nginx/tmp/
[root@server ~]# chmod 755 /var/lib/nginx/tmp/
[root@server ~]# ls -lad /var/lib/nginx/tmp/
drwxr-xr-x. 7 root root 78 May 10 16:10 /var/lib/nginx/tmp/
[root@server ~]# ls -lahd /var/lib/nginx/
drwx------. 3 root root 17 May 10 16:10 /var/lib/nginx/
[root@server ~]# chmod 755 /var/lib/nginx
[root@server ~]# ls -lahd /var/lib/nginx/
drwxr-xr-x. 3 root root 17 May 10 16:10 /var/lib/nginx/
[root@server ~]# ls -lad /var/lib/
drwxr-xr-x. 33 root root 4096 Jun 23 20:18 /var/lib/
將/var/lib/nginx/
和/var/lib/nginx/tmp/
目錄增加rx許可權,再上傳影象能夠正常修改成功!可以看看很酷的頭像:
我們將”meizhaohui”這個賬號設定為管理員,後期可以直接使用這個賬號登陸操作GitLab。
設定後,使用”meizhaohui”登陸,設定頭像等屬性!
配置CI/CD
我們新建一個部落格專案bluelog
,並將部落格專案的程式碼上傳入庫:
D:\data\github_tmp\higit $ git clone [email protected]:higit/bluelog.git Cloning into 'bluelog'... warning: You appear to have cloned an empty repository. D:\data\github_tmp\higit $ git clone [email protected]:higit/bluelog.git Cloning into 'bluelog'... warning: You appear to have cloned an empty repository. D:\data\github_tmp\higit $ ls bluelog/ D:\data\github_tmp\higit $ ls bluelog/ D:\data\github_tmp\higit $ cd bluelog\ D:\data\github_tmp\higit\bluelog (master -> origin) $ git diff D:\data\github_tmp\higit\bluelog (master -> origin) $ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) .flaskenv .gitignore LICENSE Pipfile Pipfile.lock README.md README_origin.md bluelog/ logs/ nothing added to commit but untracked files present (use "git add" to track) D:\data\github_tmp\higit\bluelog (master -> origin) $ git add -A D:\data\github_tmp\higit\bluelog (master -> origin) $ git commit -m"upload bluelog code" D:\data\github_tmp\higit\bluelog (master -> origin) $ git push origin master:master Enumerating objects: 1115, done. Counting objects: 100% (1115/1115), done. Delta compression using up to 12 threads Compressing objects: 100% (1040/1040), done. Writing objects: 100% (1115/1115), 3.99 MiB | 5.91 MiB/s, done. Total 1115 (delta 261), reused 0 (delta 0) remote: Resolving deltas: 100% (261/261), done. To 192.168.56.14:higit/bluelog.git * [new branch] master -> master
上傳完成後,檢視bluelog
專案:
我們點選”配置CD/CD”按鈕:
我們點選”選擇一個GitLab CI Yaml模板”:
選擇Bash
模板:
會自動加入Bash模板的內容,我們點選”提交修改”按鈕進行提交,並檢查CI/CD中的流水線工程:
發現流水線任務的狀態是"卡住(stuck)"
"等待中"
,說明我們的流水線配置還不正確,沒能正確的執行。
提示作業卡住了,請檢查執行器
,我們檢視具體哪個JOB卡住了:
我們檢視build這個作業的詳情頁面:
可以看到提示由於您沒有任何可以執行此作業的活躍執行器,因此作業卡住了。轉到Runner頁面
,說明我們沒有配置執行器,我們點選”Runner頁面”跳轉到執行器配置頁面:
終於到了GitLab Runner介面了,這個就是我們接下來要重點講的GitLabRunner
,也就是執行器
!
什麼是GitLab Runner
-
Runner是一個執行任務的程序。您可以根據需要配置任意數量的Runner。
-
Runner可以放在不同的使用者、伺服器,甚至本地機器上。
-
每個Runner可以處於以下狀態中的其中一種:
active
Runner已啟用,隨時可以處理新作業paused
Runner已暫停,暫時不會接受新的作業
-
要開始使用作業,您可以向專案新增特定的執行器或使用共享的執行器。
-
可以設定
專用Runner
、共享Runner
、群組Runner
。 -
手動設定專用Runner的步驟:
- 安裝 GitLab Runner
- 在Runner設定時指定URL
- 在Runner設定時使用註冊令牌
- 啟動Runner
-
GiTLab Runner就是執行器,類似於Jenkins,可以為我們執行一些CI持續整合、構建的指令碼任務,執行器具有執行指令碼、排程、協調的工作能力。
接下來我們為blog專案bluelog
設定一個專用執行器blog
。
安裝GitLab Runner執行器
Install GitLab Runner官方文件指出:
GitLab Runner can be installed and used on GNU/Linux, macOS, FreeBSD, and Windows. There are three ways to install it. Use Docker, download a binary manually, or use a repository for rpm/deb packages. Below you can find information on the different installation methods.
即GitLab Runner可以通過二進位制檔案安裝、Docker映象安裝、包倉庫安裝。
我們使用通過第三種方式包倉庫安裝,即新增Yum源來進行安裝。
我們就在GitLab伺服器上面安裝GitLab Runner。
新增官方YUM源:
[root@server ~]# curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 6753 0 6753 0 0 3420 0 --:--:-- 0:00:01 --:--:-- 3419 Detected operating system as centos/7. Checking for curl... Detected curl... Downloading repository file: https://packages.gitlab.com/install/repositories/runner/gitlab-runner/config_file.repo?os=centos&dist=7&source=script done. Installing pygpgme to verify GPG signatures... Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.cn99.com * centos-sclo-rh: ap.stykers.moe * extras: ap.stykers.moe * updates: ap.stykers.moe base | 3.6 kB 00:00:00 centos-sclo-rh | 3.0 kB 00:00:00 docker-ce-stable | 3.5 kB 00:00:00 epel | 5.3 kB 00:00:00 extras | 3.4 kB 00:00:00 gitlab-ce | 2.9 kB 00:00:00 ius | 1.3 kB 00:00:00 mariadb | 2.9 kB 00:00:00 runner_gitlab-runner-source/signature | 836 B 00:00:00 Retrieving key from https://packages.gitlab.com/runner/gitlab-runner/gpgkey Importing GPG key 0xE15E78F4: Userid : "GitLab B.V. (package repository signing key) <[email protected]>" Fingerprint: 1a4c 919d b987 d435 9396 38b9 1421 9a96 e15e 78f4 From : https://packages.gitlab.com/runner/gitlab-runner/gpgkey Retrieving key from https://packages.gitlab.com/runner/gitlab-runner/gpgkey/runner-gitlab-runner-366915F31B487241.pub.gpg runner_gitlab-runner-source/signature | 951 B 00:00:00 !!! updates | 3.4 kB 00:00:00 (1/6): epel/x86_64/updateinfo | 977 kB 00:00:01 (2/6): docker-ce-stable/x86_64/primary_db | 29 kB 00:00:02 (3/6): ius/x86_64/primary | 123 kB 00:00:03 (4/6): gitlab-ce/7/primary_db | 2.9 MB 00:00:06 (5/6): updates/7/x86_64/primary_db | 6.4 MB 00:00:07 (6/6): epel/x86_64/primary_db 73% [====================================- ] 1.0 MB/s | 13 MB 00:00:04 ETA Installing yum-utils... Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.cn99.com * centos-sclo-rh: ap.stykers.moe * extras: ap.stykers.moe * updates: ap.stykers.moe Package yum-utils-1.1.31-50.el7.noarch already installed and latest version Nothing to do Generating yum cache for runner_gitlab-runner... Importing GPG key 0xE15E78F4: Userid : "GitLab B.V. (package repository signing key) <[email protected]>" Fingerprint: 1a4c 919d b987 d435 9396 38b9 1421 9a96 e15e 78f4 From : https://packages.gitlab.com/runner/gitlab-runner/gpgkey Generating yum cache for runner_gitlab-runner-source... The repository is setup! You can now install packages.
檢視Yum源中有哪些版本:
[root@server ~]# yum search --showduplicates gitlab-runner
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* centos-sclo-rh: ap.stykers.moe
* extras: ap.stykers.moe
* updates: ap.stykers.moe
============================================================ N/S matched: gitlab-runner =============================================================
gitlab-runner-10.0.0-1.x86_64 : GitLab Runner
gitlab-runner-10.0.1-1.x86_64 : GitLab Runner
gitlab-runner-10.0.2-1.x86_64 : GitLab Runner
gitlab-runner-10.1.0-1.x86_64 : GitLab Runner
gitlab-runner-10.1.1-1.x86_64 : GitLab Runner
gitlab-runner-10.2.0-1.x86_64 : GitLab Runner
gitlab-runner-10.2.1-1.x86_64 : GitLab Runner
gitlab-runner-10.3.0-1.x86_64 : GitLab Runner
gitlab-runner-10.3.1-1.x86_64 : GitLab Runner
gitlab-runner-10.4.0-1.x86_64 : GitLab Runner
gitlab-runner-10.5.0-1.x86_64 : GitLab Runner
gitlab-runner-10.6.0-1.x86_64 : GitLab Runner
gitlab-runner-10.6.1-1.x86_64 : GitLab Runner
gitlab-runner-10.7.0-1.x86_64 : GitLab Runner
gitlab-