GitLab概述 : 是一個利用 Ruby on Rails 開發的開源應用程序,實現一個自託管的Git項目倉庫,可經過 Web 界面進行訪問公開的戒者私人項目。Ruby on Rails 是一個可使你開發、部署、維護 web 應用程序變得簡單的框架 GitLab擁有與Github相似的功能,可以瀏覽源代碼,管理缺陷和註釋。能夠管理團隊對倉庫的訪問, 它很是易於瀏覽提交過的版本並提供一個文件歷史庫。它還提供一個代碼片斷收集功能能夠輕鬆實現代碼複用,便於往後有須要的時候進行查找html
官方網站 :https://about.gitlab.com/ node
GitHub和GitLab的區別 : nginx
相同點 : 兩者都是基於web的Git倉庫,在很大程度上GitLab是仿照GitHub來作的,它們都提供了分享開源項目的平臺,爲開發團隊提供了存儲、分享、發佈和合做開發項目的中心化雲存儲的場所git
不一樣點 : 程序員
一、GitHub若是要使用私有倉庫,是須要付費的 GitLab能夠在上面建立私人的克費倉庫github
二、GitLab讓開發團隊對他們的代碼倉庫擁有更多的控制,相比於GitHub,它有很多的特點 :web
容許克費設置倉庫權限;redis
容許用戶選擇分享一個project的部分代碼sql
容許用戶設置project的獲取權限,進一步的提高安全性vim
能夠設置獲取到團隊總體的改進進度;經過 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克隆地址拉取代碼 , 再上傳代碼 , 看是否免交互