Gitlab介紹
Gitlab是一個開源分布式版本控制系統,由Ruby開發,有管理項目源代碼、版本控制、代碼復用與查找等功能。
- gitlab與github的區別:
github是分布式在線代碼托管倉庫,個人版本可直接在線免費使用,企業版本收費且需要服務器安裝。
gitlab是分布式在線代碼倉庫托管軟件,分社區免費版本與企業收費版本,都需要服務器安裝。
- gitlab的優勢:
1. 開源免費,社區免費版本適合中小型公司;
2. 差異化的版本管理,離線同步以及強大分支管理功能;
3. 便捷的GUI操作界面以及強大賬戶權限管理功能;
4. 集成度很高,能夠集成絕大多數的開發工具;
5. 支持內置HA,保證在高並發下仍舊實現高可用性。
- gitlab主要服務構成:
Nginx 靜態Web服務器
Gitlab-workhorse 輕量級的反向代理服務器
Gitlab-shell 用於處理Git命令和修改authorized keys列表
Logrotate 日志文件管理工具
Postgresql 數據庫 Redis 緩存服務器
- gitlab的工作流程:
1. 創建並克隆項目
2. 創建項目某Feature分支
3. 編寫代碼並提交至該分支
4. 推送該項目分支至遠程Gitlab服務器
5. 進行代碼檢查並提交Master主分支合並申請
6. 項目領導審查代碼並確認合並申請
環境准備
gitlab 192.168.10.30 centos7.5 4核8G
開始部署:
- 關閉防火牆和selinux:
[root@localhost ~]# systemctl stop firewalld && systemctl disable firewalld [root@localhost ~]# setenforce 0 && sed -i 's/=enforcing/=disabled/g' /etc/selinux/config
- 添加本地hosts:
[root@localhost ~]# echo "192.168.10.30 gitlab.testdemo.com" >> /etc/hosts
Gitlab安裝配置管理
- 安裝gitlab-ce:
#安裝gitlab組件 [root@localhost ~]# yum install -y curl policycoreutils openssh-server openssh-clients postfix #配置yum倉庫 [root@localhost ~]# curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash #啟動postfix郵件服務 [root@localhost ~]# systemctl start postfix && systemctl enable postfix #安裝gitlab-ce [root@localhost ~]# yum install -y gitlab-ce
- 證書創建與配置加載:
#創建證書目錄及生成證書 [root@localhost ~]# mkdir -p /etc/gitlab/ssl [root@localhost ~]# openssl genrsa -out "/etc/gitlab/ssl/gitlab.testdemo.com.key" 2048 Generating RSA private key, 2048 bit long modulus .......................+++ ....................+++ e is 65537 (0x10001) [root@localhost ~]# [root@localhost ~]# openssl req -new -key "/etc/gitlab/ssl/gitlab.testdemo.com.key" -out "/etc/gitlab/ssl/gitlab.testdemo.com.csr" You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:cn State or Province Name (full name) []:bj Locality Name (eg, city) [Default City]:bj Organization Name (eg, company) [Default Company Ltd]: Organizational Unit Name (eg, section) []: Common Name (eg, your name or your server's hostname) []:gitlab.testdemo.com Email Address []:admin@testdemo.com Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:123456 An optional company name []: [root@localhost ~]# [root@localhost ~]# openssl x509 -req -days 365 -in "/etc/gitlab/ssl/gitlab.testdemo.com.csr" -signkey "/etc/gitlab/ssl/gitlab.testdemo.com.key" -out "/etc/gitlab/ssl/gitlab.testdemo.com.crt" Signature ok subject=/C=cn/ST=bj/L=bj/O=Default Company Ltd/CN=gitlab.testdemo.com/emailAddress=admin@testdemo.com [root@localhost ~]# openssl dhparam -out /etc/gitlab/ssl/dhparams.pem 2048 [root@localhost ~]# chmod 600 /etc/gitlab/ssl/* [root@localhost ~]# ll /etc/gitlab/ssl total 16 -rw-------. 1 root root 424 Nov 10 13:37 dhparams.pem -rw-------. 1 root root 1289 Nov 10 13:35 gitlab.testdemo.com.crt -rw-------. 1 root root 1078 Nov 10 11:53 gitlab.testdemo.com.csr -rw-------. 1 root root 1675 Nov 10 11:51 gitlab.testdemo.com.key [root@localhost ~]#
- nginx SSL代理服務配置:
[root@localhost~]# vim /etc/gitlab/gitlab.rb external_url 'https://gitlab.testdemo.com' nginx['redirect_http_to_https'] = true nginx['ssl_certificate'] = "/etc/gitlab/ssl/gitlab.testdemo.com.crt" nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/gitlab.testdemo.com.key" nginx['ssl_dhparam'] = "/etc/gitlab/ssl/dhparams.pem" # Path to dhparams.pem, eg. /etc/gitlab/ssl/dhparams.pem
- 初始化gitlab相關服務並完成安裝:
-
[root@localhost ~]# gitlab-ctl reconfigure [root@localhost ~]#vim /var/opt/gitlab/nginx/conf/gitlab-http.conf ## Redirects all HTTP traffic to the HTTPS host server { listen *:80; server_name gitlab.testdemo.com; server_tokens off; ## Don't show the nginx version number, a security best practice rewrite ^(.*)$ https://$host$1 permanent; #添加該行內容 # gitlab-ctl restart #重啟gitlab
打開網頁,訪問 https://gitlab.testdemo.com/
第一次訪問提示設置密碼,設置密碼后登錄,默認用戶名是root。
點擊右上方+
→ New porject
,Project name
輸入test-repo,Visibility Level
選擇默認的Private
即可,最后點擊Create project
創建項目。
- gitlab工作流程:
任選一台其它機器
[root@prometheus ~]# yum install -y git [root@prometheus ~]# echo '192.168.10.30 gitlab.testdemo.com' >> /etc/hosts [root@prometheus ~]# mkdir /home/repo && cd /home/repo [root@prometheus repo]# git config --global user.name "admin" [root@prometheus repo]# git config --global user.email "admin@testdemo.com" [root@prometheus repo]# git -c http.sslVerify=false clone https://gitlab.testdemo.com/root/test-repo.git Cloning into 'test-repo'... Username for 'https://gitlab.testdemo.com': root Password for 'https://root@gitlab.testdemo.com': warning: You appear to have cloned an empty repository. [root@prometheus repo]# [root@prometheus repo]# ll total 0 drwxr-xr-x 3 root root 18 Nov 10 22:21 test-repo [root@prometheus repo]# cd test-repo/ [root@prometheus test-repo]# echo "print "This is a test code"" >> test.py [root@prometheus test-repo]# git add . [root@prometheus test-repo]# git commit -m "First commit" [master (root-commit) 48b30bc] First commit 1 file changed, 1 insertion(+) create mode 100644 test.py [root@prometheus test-repo]# git -c http.sslVerify=false push origin master Username for 'https://gitlab.testdemo.com': root Password for 'https://root@gitlab.testdemo.com': Counting objects: 3, done. Writing objects: 100% (3/3), 227 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://gitlab.testdemo.com/root/test-repo.git * [new branch] master -> master [root@prometheus test-repo]#
刷新瀏覽器,即可看到剛推送到gitlab服務端的代碼。
參考文檔:https://blog.csdn.net/miss1181248983/article/details/102485715