還怕Github被牆?項目團隊開發必備技能!
- 2019 年 10 月 6 日
- 筆記
項目團隊開發之從自建GitLab倉庫伺服器到熟練使用
0.導語
最近在做一個大項目,程式碼量非常大,需要一個程式碼庫存放,而現有的Github、GitLab不適合存放私密的程式碼,於是自建程式碼伺服器,存放程式碼,於是今天這篇文章應任而生!
本次構建採用GitLab搭建與部署,並進行程式碼上傳與下載的測試,以供團隊開發!
下面所有的部署於Ubuntu18.04!
1.如何安裝?
安裝一些ssh、郵件等服務!
sudo apt-get install curl openssh-server postfix ca-certificates
接下來就是安裝我們的gitlab-ca,對於這個有兩種方案:
第一:從下面鏈接下載,然後用dpkg進行安裝!
https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu/pool/trusty/main/g/gitlab-ce/
第二:直接在線安裝!
這裡推薦這種方案!
首先添加信任 GitLab 的 GPG 公鑰:
curl https://packages.gitlab.com/gpg.key 2> /dev/null | sudo apt-key add - &>/dev/null
配置清華源鏡像路徑:
vi /etc/apt/sources.list.d/gitlab-ce.list
並把下面鏈接寫入進去:
deb https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu xenial main
如果是其他系統,可以進入下面鏈接,選擇:
https://mirror.tuna.tsinghua.edu.cn/help/gitlab-ce/
開始安裝:
sudo apt-get update sudo apt-get install gitlab-ce
到這裡,或許你會看到下面成功的頁面,但或許又是另一個場面,如下面成功與失敗場面:

成功

失敗
如果失敗了,解決方法為:修改用戶與組,打開配置文件:
sudo vi /etc/gitlab/gitlab.rb
修改為:
user['username']="gitlab" user['group']="gitlab"
下面操作不管是不是失敗都得執行!還是繼續這個配置文件修改,找到external_url,需要修改為http://localhost,至於埠加不加看自己。
然後再次重新配置:
sudo gitlab-cli reconfigure
最後看到如下頁面即可成功:

啟動gitlab服務:
sudo gitlab-ctl start

瀏覽器輸入:localhost


2.如何使用?
2.1上傳倉庫
下面來測試一下如何上傳程式碼到倉庫中:
首先創建文件夾,並初始化git。
light@city:~$ mkdir tet light@city:~$ cd test/ light@city:~/test$ git init 已初始化空的 Git 倉庫於 /home/light/test/.git/
創建一些文件:
light@city:~/test$ cat readme.md light@city:~/test$ echo "test" >> readme.md light@city:~/test$ cat readme.md test
提交剛才的test項目:
light@city:~/test$ git add . light@city:~/test$ git commit -m "test" [master (根提交) 1f50c4b] test 1 file changed, 1 insertion(+) create mode 100644 readme.md light@city:~/test$ git remote add origin [email protected]:root/test.git light@city:~/test$ git push origin master 對象計數中: 3, 完成. 寫入對象中: 100% (3/3), 212 bytes | 212.00 KiB/s, 完成. Total 3 (delta 0), reused 0 (delta 0) remote: remote: The private project root/test was successfully created. remote: remote: To configure the remote, run: remote: git remote add origin gitlab@localhost:root/test.git remote: remote: To view the project, visit: remote: http://localhost/root/test remote: To 192.168.100.188:root/test.git * [new branch] master -> master
可以看到上面提交成功,下面看一下伺服器上是否有剛才提交的程式碼。

可以發現成功了!
2.2 下載倉庫
直接git clone ,通過剛才安裝時候配置的用戶@ip地址:root/倉庫名.git。
light@city:~$ git clone [email protected]:root/test.git 正克隆到 'test'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Total 3 (delta 0), reused 0 (delta 0) 接收對象中: 100% (3/3), 完成.
3.卸載
最後,如果想卸載gitlab,可以通過下面方法:
sudo apt-get remove gitlab-ca
刪除所有gitlab相關的數據目錄:
rm -rf /opt/gitlab rm -rf /etc/gitlab rm -rf /var/log/gitlab