GitLab概述 : 是一個利用 Ruby on Rails 開發的開源應用程序,實現一個自托管的Git項目倉庫,可通過 Web 界面進行訪問公開的戒者私人項目。Ruby on Rails 是一個可以使你開發、部署、維護 web 應用程序變得簡單的框架 GitLab擁有與Github類似的功能,能夠瀏覽源代碼,管理缺陷和注釋。可以管理團隊對倉庫的訪問, 它非常易於瀏覽提交過的版本並提供一個文件歷史庫。它還提供一個代碼片段收集功能可以輕松實現代碼復用,便於日后有需要的時候進行查找
官方網站 :https://about.gitlab.com/
GitHub和GitLab的區別 :
相同點 : 二者都是基於web的Git倉庫,在很大程度上GitLab是仿照GitHub來做的,它們都提供了分享開源項目的平台,為開發團隊提供了存儲、分享、發布和合作開發項目的中心化雲存儲的場所
不同點 :
1、GitHub如果要使用私有倉庫,是需要付費的 GitLab可以在上面創建私人的克費倉庫
2、GitLab讓開發團隊對他們的代碼倉庫擁有更多的控制,相比於GitHub,它有不少的特色 :
允許克費設置倉庫權限;
允許用戶選擇分享一個project的部分代碼
允許用戶設置project的獲取權限,進一步的提升安全性
可以設置獲取到團隊整體的改進進度;通過 innersourcing讓不在權限范圍內的人訪問不到該資源
總結 :從代碼私有性方面來看,有時公司並不希望員工獲取到全部的代碼,這個時候GitLab無疑是更好的選擇但對於開源項目而言,GitHub依然是代碼托管的首選
git相關概念 :
git 是一種版本控制系統,是一個命令,是一種工具
gitlib 是用於實現git功能的開發庫
github 是一個基於git實現的在線代碼托管倉庫,包含一個網站界面,向互聯網開放
gitlab 是一個基於git實現的在線代碼倉庫托管軟件,一般用於在企業內部網絡搭建git私服
gitlab-ce 社區版(免費)
gitlab-ee是企業版(收費)
搭建GitLab平台
環境 : 最少4G內存 , 不然后期訪問 gitlab web頁面可能會報錯
[root@gitlab ~]# cat /etc/centos-release CentOS Linux release 7.5.1804 (Core) [root@gitlab ~]# free -h total used free shared buff/cache available Mem: 7.6G 194M 7.2G 11M 208M 7.2G Swap: 2.0G 0B 2.0G
安裝Gitlab的依賴包 :
[root@gitlab ~]# yum -y install curl policycoreutils openssh-server openssh-clients postfix [root@gitlab ~]# systemctl enable postfix [root@gitlab ~]# systemctl start postfix [root@gitlab ~]# iptables -F [root@gitlab ~]# systemctl stop firewalld [root@gitlab ~]# systemctl disable firewalld
安裝Gitlab-ce版
# 下載 gitlab-ce版yum源 [root@gitlab ~]# curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh |bash [root@gitlab ~]# yum -y install gitlab-ce
配置gitlab域名
[root@gitlab ~]# vim /etc/gitlab/gitlab.rb # 13行 external_url 'http://gitlab.example.com' 改為 http://IP
加載配置文件並啟動gitlab(加載配置文件需要點時間)
[root@gitlab ~]# gitlab-ctl reconfigure [root@gitlab ~]# gitlab-ctl status run: alertmanager: (pid 12585) 70s; run: log: (pid 12700) 69s run: gitaly: (pid 12509) 72s; run: log: (pid 12597) 70s run: gitlab-monitor: (pid 12543) 71s; run: log: (pid 12695) 70s run: gitlab-workhorse: (pid 12490) 72s; run: log: (pid 12564) 71s run: logrotate: (pid 12051) 149s; run: log: (pid 12562) 71s run: nginx: (pid 12033) 155s; run: log: (pid 12561) 71s run: node-exporter: (pid 12223) 137s; run: log: (pid 12598) 70s run: postgres-exporter: (pid 12688) 70s; run: log: (pid 12702) 69s run: postgresql: (pid 11762) 205s; run: log: (pid 12546) 71s run: prometheus: (pid 12563) 71s; run: log: (pid 12697) 69s run: redis: (pid 11702) 211s; run: log: (pid 12545) 71s run: redis-exporter: (pid 12266) 120s; run: log: (pid 12554) 71s run: sidekiq: (pid 12001) 162s; run: log: (pid 12548) 71s run: unicorn: (pid 11963) 168s; run: log: (pid 12547) 71s
[root@gitlab ~]# netstat -antup | grep :80
tcp 0 0 0.0.0.0:8060 0.0.0.0:* LISTEN 12033/nginx: master
tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 11981/unicorn maste
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 12033/nginx: master # 默認使用nginx作為web服務器
tcp 0 0 127.0.0.1:8082 0.0.0.0:* LISTEN 12001/sidekiq 5.1.3
[root@gitlab ~]# free -h
total used free shared buff/cache available
Mem: 7.6G 3.3G 1.5G 90M 2.8G 3.8G
Swap: 2.0G 0B 2.0G
# 真的很吃內存
登錄gitlab
第一次登錄 gitlab,需要為root 用戶修改密碼,root用戶也是gitlab的超級管理員
改成密碼后, 登錄 http://192.168.94.11/ 用戶名 :root
gitlab搭建成功
gitlab主配置文件:/etc/gitlab/gitlab.rb # 可以自定義一些郵件服務等
日志目錄:/var/log/gitlab/ # 對應各服務
服務目錄:/var/opt/gitlab/ # 對應各服務的主目錄
倉庫目錄:/var/opt/gitlab/git-data # 記錄項目倉庫等提交信息
重啟服務:gitlab-ctl stop/start/restart # 啟動命令
注冊新用戶和關閉 gitlab注冊功能 :
退出root用戶 默認情況下可以直接注冊賬號 http://192.168.94.11/users/sign_in 開始注冊
可以關閉注冊功能
用管理員用戶登錄 Admin Area→setting→取消Sing-up enabled 選項對勾
然后下拉網頁 , 在Sign-up restrictions 配置項的末尾有 保存修改
這樣就關閉了注冊功能
centos7 部署漢化版gitlab
gitlab中文社區版的項目 :
v7-v8.8是由 Larry Li發起的“GitLab 中文社區版項目” (https://gitlab.com/larryli/gitlab)
從 v8.9 之后,@xhang 開始繼續該漢化項目 (https://gitlab.com/xhang/gitlab)
下載漢化包:
# 上面下載的是gitlab-ce 11.2.1 這里下載對應版本的漢化包 [root@gitlab ~]# git clone https://gitlab.com/xhang/gitlab.git -b v11.2.1-zh
漢化的方法就是把下載的漢化包替換現有的web頁面
[root@gitlab ~]# gitlab-ctl stop [root@gitlab ~]# /usr/bin/cp -rvf /root/gitlab/* /opt/gitlab/embedded/service/gitlab-rails/ # gitlab-rails下面存放的都是web 界面相關的文件
重啟服務
[root@gitlab ~]# gitlab-ctl restart
登錄gitlab查看
gitlab的使用 :
1 新建項目
2 創建用戶
3 重置用戶密碼
4 刪除用戶
1 新建項目
新建項目前,先創建項目所在的組(也就是說這個項目文件是保存在哪個組里)
Visibility Level(項目可見級別):
Private(私有的,只有你自己戒者組內的成員能訪問)
Internal(所有登錄 gitlab平台的用戶都可能訪問)
Public(公開的,所有人都可以訪問)三種選項。即不登錄gitlab也可以訪問
稍后會創建秘鑰 , 實現無交互上傳和下載代碼 , 方便后期進行Jenkins自動部署
2 創建用戶
個人資料可以不填
創建完用戶會發郵件到你的添寫的郵箱 , 點擊進入給創建的用戶修密碼 , 郵件有時候會收到 ,有時候收不到
就算能收到 , 也不及時 , 所以讓root用戶修改密碼是最快 , 最有效的
3 修改密碼
4 刪除用戶
把用戶damowang添加到damowang的群組里 , 身份選擇"主程序員" , 后期就可以提交代碼了 , "開發人員" 是沒有權限提交代碼的
在項目中添加一個文件index.html
安裝 git並 clone代碼
[root@gitlab ~]# yum -y install git
初次運行Git錢的配置
一般在新的系統上, 我們都需要先配置先自己的Git 工作環境 , 配置工作只需要一次 , 以后升級時還會沿用現在的配置
第一個要配置的是你個人的用戶名稱和電子郵箱地址 , 這兩條配置很重要 ,每次Git提交時都會引用這兩條信息 , 說明是誰提交了更新 , 所以會隨更新內容一起被永久納入歷史記錄
Git運行的環境變量有點像 .bashrc , 決定了Git 在各個環節的具體工作方式和行為 , 這些變量可以存放在以下兩個地方 :
~/.gitconfig文件 : 用戶目錄下的配置文件是適用於該用戶 , 若使用 gitconfig時 用--global選項 , 讀取的就是這個文件
[root@git damowang.cn-web]# git config --global user.name "damowang.cn" [root@git damowang.cn-web]# git config --global user.email "damowang.cn@gmail.com" [root@git damowang.cn-web]# cat ~/.gitconfig [user] name = damowang.cn email = damowang.cn@gmail.com
當前項目的 Git 目錄中的配置文件, 也就是工作目錄中的 .git/config 文件 這里的配置僅僅 針對當前項目有效 每一個級別的配置都會覆蓋上層的相同配置,所以 .git/config 里的配置會覆蓋 ~/.gitconfig中的同名變量
如果要在某個特定的項目中使用其他名字或者郵件地址,先進到項目目錄下,然后只要去掉 --global 選 項重新配置即可 最后配置的用戶和郵件地址會保存在當前項目的 .git/config 文件里
[root@git damowang.cn-web]# pwd /git/damowang.cn-web [root@git damowang.cn-web]# cat ./.git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [remote "origin"] url = http://192.168.94.11/damowang/damowang.cn-web.git fetch = +refs/heads/*:refs/remotes/origin/* [branch "master"] remote = origin merge = refs/heads/master [root@git damowang.cn-web]# git config user.name "damowang.cn" [root@git damowang.cn-web]# git config user.email "damowang.cn@gmail.com" [root@git damowang.cn-web]# cat ./.git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [remote "origin"] url = http://192.168.94.11/damowang/damowang.cn-web.git fetch = +refs/heads/*:refs/remotes/origin/* [branch "master"] remote = origin merge = refs/heads/master [user] name = damowang.cn email = damowang.cn@gmail.com
git 常用命令: git config --global user.name “name“ # 設置全局用戶名
git config --global user.email mail # 設置全局郵箱
git config --global --list # 列出用戶全局設置
git add index.html # 添加文件到暫存區
git commit -m “描述內容“ # 提交文件到工作區
git status # 查看工作區的狀態
git push # 提交代碼到git 服務器上
git pull # 獲取代碼到本地
git log #查看操作日志
vim .gitignore #定義忽略文件 git reset --hard HEAD^ # git 版本回滾, HEAD為當前版本,加一個^為上一個,^^為上上一 個版本
git reflog # 獲取每次提交的ID,可以使用--hard根據提交的ID 迚行版本回退
git reset --hard 5ae4b06 # 回退到指定id的版本
git branch # 查看當前所處的分支 git checkout -- file #從服務器更新某個那文件覆蓋本地的文
git checkout -- file # 從服務器更新某個那文件覆蓋本地的文件
把修改過的 index.html 文件更新主版本中
[root@git damowang.cn-web]# echo "mail.damowang.cn" >> index.html [root@git damowang.cn-web]# git add index.html [root@git damowang.cn-web]# git commit -m "add mail.damowang.cn" [master 6bff0da] add mail.damowang.cn 1 file changed, 1 insertion(+), 1 deletion(-) [root@git damowang.cn-web]# # 上傳到master主干下 Username for 'http://192.168.94.11': damowang.cn Password for 'http://damowang.cn@192.168.94.11': Counting objects: 5, done. Writing objects: 100% (3/3), 258 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To http://192.168.94.11/damowang/damowang.cn-web.git 2c6153b..6bff0da master -> master 分支 master 設置為跟蹤來自 origin 的遠程分支 master。 [root@git damowang.cn-web]# rm -rf index.html # 刪除一些代碼 [root@git damowang.cn-web]# git reset --hard HEAD # 回滾到最新版本 HEAD 現在位於 6bff0da add mail.damowang.cn [root@git damowang.cn-web]# ls index.html [root@git damowang.cn-web]# git reflog # 獲取每次提交的ID 6bff0da HEAD@{0}: commit: add mail.damowang.cn 2c6153b HEAD@{1}: clone: from http://192.168.94.11/damowang/damowang.cn-web.git
工作區和暫存區及分支概述
工作區就是編輯文件的目錄區域,需要將工作區的修改好的文件 add到暫存區才能提交到git服務器,在工作區有多個文件的時候可以將一個或多個文件添加至暫存區,再提交到git服務器即可
在服務器創建分支
[root@git damowang.cn-web]# git branch mail # 創建一個分支 [root@git damowang.cn-web]# git checkout mail # 切換到分支 mail 切換到分支 'mail' [root@git damowang.cn-web]# git branch # 查看當前所在分支 * mail master [root@git damowang.cn-web]# echo "damowang" > test.txt [root@git damowang.cn-web]# git add test.txt [root@git damowang.cn-web]# git commit -m "add test.txt" # 提交到暫存區中 [mail f4cd6f0] add test.txt 1 file changed, 1 insertion(+) create mode 100644 test.txt [root@git damowang.cn-web]# git push -u origin mail # 上傳到分支mail Username for 'http://192.168.94.11': damowang.cn Password for 'http://damowang.cn@192.168.94.11': Counting objects: 4, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 282 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) remote: remote: To create a merge request for mail, visit: remote: http://192.168.94.11/damowang/damowang.cn-web/merge_requests/new?merge_request%5Bsource_branch%5D=mail remote: To http://192.168.94.11/damowang/damowang.cn-web.git * [new branch] mail -> mail 分支 mail 設置為跟蹤來自 origin 的遠程分支 mail。
使用ssh密鑰對實現免交互提交拉取代碼
[root@git ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P "" ssh-rsa [root@git ~]# cat ~/.ssh/id_rsa.pub AAAAB3NzaC1yc2EAAAADAQABAAABAQCj7G88QrpBo/Shw4WhpOHyHQKEm8xzCsL7++LWyhXUr4GaX43tMRwoOhAPu/UZkyk0regya0DdXy9dJgiOcUDpaACDZRsFPst5Nw7EKgd90BI4HWAuHQ+gNPxyJtHpyXAJuRXeSWWxiYa5KM+YH5ix0TpSDNbnwxiVQSxB0slgwI25qL0naCBbiKZ0lvQOaDKJoF5aVrQngASQgXBCOlhWrKI6ZcuAMnpW5kD5/4GJr6Y8RibbNros+8BGMwG7z64Hv2Fl5wh6t52quLxTn+pCCkcLcTFiC0DBHcyFdVBqALYU2UaqC+ee6zNDQ13UZTBAKKqjwmws42GEo2j1mjgN root@gitlab
登錄gitlab 增加SSH密鑰
復制git克隆地址拉取代碼 , 再上傳代碼 , 看是否免交互