背景
GitLab
是一個強(qiáng)大的版本控制系統(tǒng)和協(xié)作平臺,記錄一下在實際工作中關(guān)于 GitLab
的安裝使用記錄。
一開始使用 GitLab
時,是在 CentOS7
上直接以 rpm
包的方式進(jìn)行安裝,僅作為代碼托管工具來使用,版本: 14.10.4
。
后續(xù)預(yù)研 GitLab
的 CI/CD
及流水線時,采用 Docker
方式安裝,版本: 16.2.3-jh
;引入了 GitLab Runner
,版本: 16.2.0
。
系統(tǒng)環(huán)境
[root@gitlab1 opt]# uname -a
Linux gitlab1 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@gitlab1 opt]# cat /proc/version
Linux version 3.10.0-1127.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ) #1 SMP Tue Mar 31 23:36:51 UTC 2020
[root@gitlab1 opt]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[root@gitlab2 ~]# docker -v
Docker version 20.10.18, build b40c2f6
RPM方式安裝GitLab
下載地址,清華鏡像:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/
[root@gitlab1 local]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-14.10.4-ce.0.el7.x86_64.rpm
錯誤: 無法驗證 mirrors.tuna.tsinghua.edu.cn 的由 “/C=US/O=Let's Encrypt/CN=R3” 頒發(fā)的證書:
頒發(fā)的證書已經(jīng)過期。
解決:將https修改為http
[root@gitlab1 local]# rpm -ivh gitlab-ce-14.10.4-ce.0.el7.x86_64.rpm
警告:gitlab-ce-14.10.4-ce.0.el7.x86_64.rpm: 頭V4 RSA/SHA1 Signature, 密鑰 ID f27eab47: NOKEY
錯誤:依賴檢測失?。? policycoreutils-python 被 gitlab-ce-14.10.4-ce.0.el7.x86_64 需要
解決:yum install -y curl policycoreutils-python openssh-server
[root@gitlab1 local]# rpm -ivh gitlab-ce-14.10.4-ce.0.el7.x86_64.rpm
警告:gitlab-ce-14.10.4-ce.0.el7.x86_64.rpm: 頭V4 RSA/SHA1 Signature, 密鑰 ID f27eab47: NOKEY
準(zhǔn)備中... ################################# [100%]
正在升級/安裝...
1:gitlab-ce-14.10.4-ce.0.el7 ################################# [100%]
It looks like GitLab has not been configured yet; skipping the upgrade script.
*. *.
*** ***
***** *****
.****** *******
******** ********
,,,,,,,,,***********,,,,,,,,,
,,,,,,,,,,,*********,,,,,,,,,,,
.,,,,,,,,,,,*******,,,,,,,,,,,,
,,,,,,,,,*****,,,,,,,,,.
,,,,,,,****,,,,,,
.,,,***,,,,
,*,.
_______ __ __ __
/ ____(_) /_/ / ____ _/ /_
/ / __/ / __/ / / __ `/ __ \
/ /_/ / / /_/ /___/ /_/ / /_/ /
\____/_/\__/_____/\__,_/_.___/
Thank you for installing GitLab!
GitLab was unable to detect a valid hostname for your instance.
Please configure a URL for your GitLab instance by setting `external_url`
configuration in /etc/gitlab/gitlab.rb file.
Then, you can start your GitLab instance by running the following command:
sudo gitlab-ctl reconfigure
For a comprehensive list of configuration options please see the Omnibus GitLab readme
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md
Help us improve the installation experience, let us know how we did with a 1 minute survey:
https://gitlab.fra1.qualtrics.com/jfe/form/SV_6kVqZANThUQ1bZb?installation=omnibus&release=14-10
# 編輯配置,設(shè)置訪問地址
[root@gitlab1 local]# vi /etc/gitlab/gitlab.rb
external_url 'http://gitlab1'
# 重新配置生效
[root@gitlab1 local]# gitlab-ctl reconfigure
Running handlers:
Running handlers complete
Chef Infra Client finished, 606/1618 resources updated in 05 minutes 34 seconds
Warnings:
Rehashing of trusted certificates present in `/etc/gitlab/trusted-certs` failed. If on a FIPS-enabled machine, ensure `c_rehash` binary is available in $PATH.
Notes:
Default admin account has been configured with following details:
Username: root
Password: You didn't opt-in to print initial root password to STDOUT.
Password stored to /etc/gitlab/initial_root_password. This file will be cleaned up in first reconfigure run after 24 hours.
NOTE: Because these credentials might be present in your log files in plain text, it is highly recommended to reset the password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password.
gitlab Reconfigured!
Note:
- 如何查看版本信息?在GitLab后臺中查看版本:在部署域名/IP后加上help。
- 如果修改了ip或者端口等配置信息,每次更改完之后,務(wù)必記得重新加載配置:gitlab-ctl reconfigure。
[root@gitlab1 local]# cat /etc/gitlab/initial_root_password | grep Password
使用瀏覽器訪問配置的地址,輸入用戶名與密碼。
GitLab
常用命令
- gitlab-ctl start:啟動gitlab,
- gitlab-ctl stop:停止gitlab。
- gitlab-ctl status:查看gitlab狀態(tài)
- gitlab-ctl restart:重啟服務(wù)
作為代碼倉庫, GitLab
使用包括了以下幾方面:
- 創(chuàng)建組
- 創(chuàng)建項目
- 添加SSHkey
- 克隆
- 用戶注冊
- 用戶審核
- 加入組
這種方式下,如果想使用 CI/CD
功能,則需要接著安裝 GitLab Runner
(不過不建議用這種直接安裝的方式來用,實際生產(chǎn)更多的是使用 Docker
以及 Kubernetes
),分兩步:安裝和注冊,下載地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-runner/yum/el7/
Note:
- GitLab Runner注冊分兩步:獲取runner token,注冊
- GitLab Runner分為共享型、分組型、項目型,分別在不同的位置
- 注冊分為交互式和非交互式兩種方式。
Docker方式安裝使用GitLab
新開一臺虛擬機(jī),使用 Docker
的方式來安裝和運行 GitLab
和 GitLab Runner
。
[root@gitlab2 ~]# cd /opt/
[root@gitlab2 opt]# mkdir gitlab
[root@gitlab2 opt]# export GITLAB_HOME=/opt/gitlab
根據(jù)官方文檔,安裝運行 GitLab Runner
的命令如下:
sudo docker run --detach \
--hostname gitlab.example.com \
--publish 443:443 --publish 80:80 --publish 22:22 \
--name gitlab \
--restart always \
--volume $GITLAB_HOME/config:/etc/gitlab \
--volume $GITLAB_HOME/logs:/var/log/gitlab \
--volume $GITLAB_HOME/data:/var/opt/gitlab \
--shm-size 256m \
registry.gitlab.cn/omnibus/gitlab-jh:latest
實際執(zhí)行時,簡化了參數(shù),設(shè)置了主機(jī)名,修改了映射端口號。
docker run -d \
-h gitlab2 \
-p 80:80 -p 222:22 \
--name gitlab \
--restart always \
-v $GITLAB_HOME/config:/etc/gitlab \
-v $GITLAB_HOME/logs:/var/log/gitlab \
-v $GITLAB_HOME/data:/var/opt/gitlab \
--shm-size 256m \
registry.gitlab.cn/omnibus/gitlab-jh:latest
Note: 宿主機(jī)的端口22修改為222,否則報錯: Error starting userland proxy: listen tcp4 0.0.0.0:22: bind: address already in use.
訪問極狐 GitLab URL
,并使用用戶名 root
和來自以下命令的密碼登錄:
[root@gitlab2 ~]# docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password
Password: zIrC8HPFfuxVmGSyx27nRbgTRwLaoiIhu+a2edEySMw=
通過 Docker
來對 GitLab
的服務(wù)管理。
docker start gitlab
docker stop gitlab
docker restart gitlab
docker rm gitlab
Docker方式安裝注冊gitlab-runner
安裝GitLab Runner
根據(jù) GitLab
的版本,指定了 GitLab Runner
的版本。
docker run -d --name gitlab-runner --restart always \
-v /opt/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
gitlab/gitlab-runner:v16.2.0
# 查看安裝的GitLab Runner的版本信息
[root@gitlab2 ~]# docker exec -it gitlab-runner gitlab-runner --version
Version: 16.2.0
Git revision: 782e15da
Git branch: 16-2-stable
GO version: go1.20.5
Built: 2023-07-21T22:52:42+0000
OS/Arch: linux/amd64
注冊GitLab Runner
接下來,創(chuàng)建組,創(chuàng)建項目,克隆,為項目注冊一個 Runner
。
Create new group ——> Create new project ——> git clone http://gitlab2/iot/test.git
# 注冊為`Shell`類型的執(zhí)行器。
docker run --rm -v /opt/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner:v16.2.0 \
register \
--non-interactive \
--executor "shell" \
--url "http://192.168.44.149" \
--registration-token "GR1348941NNVnhpcsLqgBbL-1JxPT" \
--description "shell-runner" \
--tag-list "shell,test" \
--run-untagged="true" \
--locked="false" \
--access-level="not_protected"
注冊過程中遇到的問題:
- ERROR: Registering runner… error runner=GR1348941NNVnhpcs status=only http or https scheme supported
原因是我將 --url
的值寫成了主機(jī)名,得寫成 HTTP
地址鏈接的形式。
- ERROR: Registering runner… failed runner=GR1348941NNVnhpcs status=couldn’t execute POST against http://gitlab2/api/v4/runners: Post “http://gitlab2/api/v4/runners”: dial tcp: lookup gitlab2 on 192.168.44.2:53: no such host
原因是我將 --url
的值寫成了主機(jī)名,在容器中無法通過主機(jī)名注冊成功,然后改成了 IP
地址,注冊成功,以下是注冊成功的效果。
[root@gitlab2 ~]# docker run --rm -v /opt/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner:v16.2.0 \
> register \
> --non-interactive \
> --executor "shell" \
> --url "http://192.168.44.149" \
> --registration-token "GR1348941NNVnhpcsLqgBbL-1JxPT" \
> --description "shell-runner" \
> --tag-list "shell,test" \
> --run-untagged="true" \
> --locked="false" \
> --access-level="not_protected"
Runtime platform arch=amd64 os=linux pid=7 revision=782e15da version=16.2.0
Running in system-mode.
WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecated in GitLab Runner 15.6 and will be replaced with support for authentication tokens. For more information, see https://gitlab.com/gitlab-org/gitlab/-/issues/380872
Registering runner... succeeded runner=GR1348941NNVnhpcs
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"
CI/CD
為了體驗下 GitLab Runner
的 CI/CD
功能,接下來以一段后端服務(wù)構(gòu)建與部署的配置來模擬實際的流水線過程,在創(chuàng)建的項目根目錄下新建文件: .gitlab-ci.yml
(提交后項目會自動啟動流水線的執(zhí)行)。
stages:
- build
- deploy
build:
stage: build
tags:
- shell
only:
- master
script:
- echo "mvn clean"
- echo "mvn package"
deploy:
stage: deploy
tags:
- test
only:
- master
script:
- echo "java -jar x.jar"
CI/CD
過程中遇到的問題:
- 第一次添加上述配置文件并提交后,并沒有觸發(fā)流水線。。
原來在我用的新版 GitLab
中的主分支從 master
改為了 main
,修改 .gitlab-ci.yml
中的分支名稱即可。
- 流水線失敗,Job日志:fatal: unable to access ‘http://gitlab2/iot/test.git/’: Could not resolve host: gitlab2
感覺還是因為 Docker
容器無法獲取宿主機(jī)的主機(jī)名,進(jìn)入 gitlab-runner
所在的容器, ping
宿主機(jī) ip
是通的,但是主機(jī)名不通,簡單的辦法是在 config.toml
文件中添加 clone_url = "http://192.168.44.149"
,以 IP
的方式來拉取代碼。
# 沒有自帶ping命令,自行安裝
root@249b6b18ffa8:/# apt update
root@249b6b18ffa8:/# apt install -y iputils-ping
# 修改配置,增加clone_url配置,跟url并列
[root@gitlab2 ~]# vi /opt/gitlab-runner/config/config.toml
clone_url = "http://192.168.44.149"
# 重啟gitlab-runner所在容器
[root@gitlab2 ~]# docker restart gitlab-runner
gitlab-runner
改成以上配置后,手動啟動一個 Pipeline
,執(zhí)行成功。
# 進(jìn)入容器
[root@gitlab2 ~]# docker exec -it gitlab-runner /bin/bash
# 列出所有的runner
root@249b6b18ffa8:/# gitlab-runner list
Runtime platform arch=amd64 os=linux pid=27 revision=782e15da version=16.2.0
Listing configured runners ConfigFile=/etc/gitlab-runner/config.toml
shell-runner Executor=shell Token=XQEENnyfzyEVs8-2iDB9 URL=http://192.168.44.149
# 查看runner的狀態(tài)
root@249b6b18ffa8:/# gitlab-runner verify
Runtime platform arch=amd64 os=linux pid=37 revision=782e15da version=16.2.0
Running in system-mode.
Verifying runner... is alive runner=XQEENnyf
Note:
- CI Lint可以用來校驗.gitlab-ci.yml的語法;
- 一個流水線包含多個作業(yè),每個作業(yè)下至少包含一個script。
- 修改服務(wù)器的IP之后,Docker容器無法從服務(wù)器外部訪問
# 服務(wù)器IP更改之后,linux數(shù)據(jù)包轉(zhuǎn)發(fā)配置net.ipv4.ip_forward會變?yōu)?,即關(guān)閉狀態(tài)。
[root@gitlab2 ~]# sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 0
# 重啟docker,docker會默認(rèn)將net.ipv4.ip_forward值臨時改為1(再次新增或更改IP之后會失效)
[root@gitlab2 ~]# systemctl restart docker
小總結(jié)
以上是安裝和使用 GitLab
的過程,以及CI/CD初體驗, GitLab
是一個功能豐富、易于使用和安全可靠的協(xié)作平臺,可以幫助團(tuán)隊更好地管理和開發(fā)項目。
- 集成的工作流程:
GitLab
提供了一個集成的工作流程,包括代碼托管、問題跟蹤、持續(xù)集成和部署等功能。這使得團(tuán)隊成員可以在同一個平臺上協(xié)作開發(fā)和管理項目。 - 代碼托管:
GitLab
提供了強(qiáng)大的代碼托管功能,支持 Git 分布式版本控制系統(tǒng)。團(tuán)隊成員可以輕松地共享和管理代碼,進(jìn)行版本控制和協(xié)作開發(fā)。 - 問題跟蹤:
GitLab
提供了問題跟蹤功能,可以幫助團(tuán)隊成員記錄和解決項目中的問題。通過問題跟蹤,團(tuán)隊成員可以更好地協(xié)作和追蹤項目進(jìn)展。 - 持續(xù)集成和部署:
GitLab
提供了強(qiáng)大的持續(xù)集成和部署功能,可以自動化構(gòu)建、測試和部署代碼。這可以大大提高開發(fā)效率和代碼質(zhì)量。 - 安全性:
GitLab
提供了強(qiáng)大的安全性功能,包括訪問控制、權(quán)限管理和代碼審查等。這可以幫助團(tuán)隊保護(hù)代碼和敏感信息的安全。
Reference
- https://docs.gitlab.cn/runner/register/
If you have any questions or any bugs are found, please feel free to contact me.文章來源:http://www.zghlxwxcb.cn/news/detail-667319.html
Your comments and suggestions are welcome!文章來源地址http://www.zghlxwxcb.cn/news/detail-667319.html
到了這里,關(guān)于GitLab與GitLab Runner安裝(RPM與Docker方式),CI/CD初體驗的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!