1. 程式人生 > 實用技巧 >版本控制gitlab

版本控制gitlab

目錄

版本控制gitlab

版本控制介紹

版本控制是指對軟體開發過程中各種程式程式碼、配置檔案及說明文件等檔案變更的管理,是軟體配置管理的核心思想之一。

版本控制最主要的功能就是追蹤檔案的變更。它將什麼時候、什麼人更改了檔案的什麼內容等資訊忠實地了記錄下來。每一次檔案的改變,檔案的版本號都將增加。除了記錄版本變更外,版本控制的另一個重要功能是並行開發。軟體開發往往是多人協同作業,版本控制可以有效地解決版本的同步以及不同開發者之間的開發通訊問題,提高協同開發的效率。並行開發中最常見的不同版本軟體的錯誤(Bug)修正問題也可以通過版本控制中分支與合併的方法有效地解決。
具體來說,在每一項開發任務中,都需要首先設定開發基線,確定各個配置項的開發初始版本,在開發過程中,開發人員基於開發基線的版本,開發出所需的目標版本。當發生需求變更時,通過對變更的評估,確定變更的影響範圍,對被影響的配置項的版本進行修改,根據變更的性質使配置項的版本樹繼續延伸或產生新的分支,形成新的目標版本,而對於不受變更影響的配置項則不應發產生變動。同時,應能夠將變更所產生的對版本的影響進行記錄和跟蹤。必要時還可以回退到以前的版本。例如當開發需求或需求變更被取消時,就需要有能力將版本回退到開發基線版本。在曾經出現過的季度升級包拆包和重新組包的過程中,其實就是將部分配置項的版本回退到開發基線,將對應不同需求的不同分支重新組合歸併,形成新的升級包版本。
版本控制是軟體配置管理的核心功能。所有置於配置庫中的元素都應自動予以版本的標識,並保證版本命名的唯一性。版本在生成過程中,自動依照設定的使用模型自動分支、演進。除了系統自動記錄的版本資訊以外,為了配合軟體開發流程的各個階段。還需要定義、收集一些元資料來記錄版本的輔助資訊和規範開發流程,併為今後對軟體過程的度量做好準備。當然如果選用的工具支援,這些輔助資料將能直接統計出過程資料,從而方便軟體過程改進活動的進行。對於配置庫中的各個基線控制項,應該根據其基線的位置和狀態來設定相應的訪問許可權。一般來說,對於基線版本之前的各個版本都應處於被鎖定的狀態,如需要對它們進行變更,則應按照變更控制的流程來進行操作。

常用的版本控制工具:

  • gitlab
  • subversion

gitlab 服務元件

  • Nginx:靜態 web 伺服器。

  • gitlab-shell:用於處理 Git 命令和修改 authorized keys 列表。(Ruby)

  • gitlab-workhorse: 輕量級的反向代理伺服器。(go)

    它會處理一些大的 HTTP 請求,比如檔案上傳、檔案下載、Git push/pull 和 Git 包下載。
    其它請求會反向代理到 GitLab Rails 應用,即反向代理給後端的 unicorn。
    
  • logrotate:日誌檔案管理工具。

  • postgresql:資料庫。

  • redis:快取資料庫。

  • sidekiq:用於在後臺執行佇列任務(非同步執行)。(Ruby)

  • unicorn:An HTTP server for Rack applications,GitLab Rails 應用是託管在這個伺服器上面的。(Ruby Web Server, 主要使用 Ruby 編寫

常用命令

gitlab-ctl start:啟動全部服務
gitlab-ctl restart:重啟全部服務
gitlab-ctl stop:停止全部服務
gitlab-ctl reconfigure:使配置檔案生效(一般修改完主配置檔案/etc/gitlab/gitlab.rb,需要執行此命令)
gitlab-ctl show-config :驗證配置檔案
gitlab-ctl uninstall:刪除gitlab(保留資料)
gitlab-ctl cleanse:刪除所有資料,從新開始
gitlab-ctl tail 檢視服務的日誌

Gitlab基礎目錄

/var/opt/gitlab/git-data/repositories:庫預設儲存目錄
/opt/gitlab: 應用程式碼和相應的依賴程式
/var/opt/gitlab:gitlab-ctl reconfigure命令編譯後的應用資料和配置檔案,不需要人為修改配置
/etc/gitlab: 配置檔案目錄
/var/log/gitlab:此目錄下存放了gitlab各個元件產生的日誌
/var/opt/gitlab/backups/:備份檔案生成的目錄

gitlab部署

使用國內源下載gitlab

清華大學gitlab映象源

#配置好yum源和epel源下載軟體包和依賴包
[root@localhost ~]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-13.2.2-ce.0.el7.x86_64.rpm

[root@localhost ~]# yum -y install git  curl openssh-server openssh-clients postfix cronie policycoreutils-python

#啟動postfix服務並設定開機自啟
[root@localhost ~]# systemctl restart postfix
[root@localhost ~]# systemctl enable postfix

#安裝gitlab
[root@localhost ~]# rpm -ivh gitlab-ce-13.2.1-ce.0.el7.x86_64.rpm
warning: gitlab-ce-13.2.1-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID f27eab47: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:gitlab-ce-13.2.1-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


#修改配置檔案
[root@localhost ~]# vim /etc/gitlab/gitlab.rb
#找到
external_url 'http://gitlab.example.com'
#修改為
external_url 'http://192.168.32.125'

#過載配置檔案並重啟gitlab
[root@localhost ~]# gitlab-ctl reconfigure
[root@localhost ~]# gitlab-ctl restart
ok: run: alertmanager: (pid 25928) 0s
ok: run: gitaly: (pid 25938) 1s
ok: run: gitlab-exporter: (pid 25956) 0s
ok: run: gitlab-workhorse: (pid 25967) 0s
ok: run: grafana: (pid 25980) 1s
ok: run: logrotate: (pid 25993) 0s
ok: run: nginx: (pid 25999) 1s
ok: run: node-exporter: (pid 26005) 0s
ok: run: postgres-exporter: (pid 26089) 1s
ok: run: postgresql: (pid 26101) 0s
ok: run: prometheus: (pid 26110) 0s
ok: run: puma: (pid 26123) 0s
ok: run: redis: (pid 26128) 1s
ok: run: redis-exporter: (pid 26133) 0s
ok: run: sidekiq: (pid 26140) 0s


#檢視當前的gitlab版本
[root@localhost ~]# head -1 /opt/gitlab/version-manifest.txt
gitlab-ce 13.2.1

輸入ip訪問,初始化

輸入兩次密碼,至少8位字元長度
登入

如果忘記密碼,在服務端破解

[root@localhost ~]# gitlab-rails console -e production
--------------------------------------------------------------------------------
 GitLab:       13.2.1 (b55baf593e6) FOSS
 GitLab Shell: 13.3.0
 PostgreSQL:   11.7
--------------------------------------------------------------------------------
Loading production environment (Rails 6.0.3.1)
irb(main):001:0> user = User.where(id: 1).first			//id為1的是超級管理員
=> #<User id:1 @root>
irb(main):002:0> user.password = 'qwer!@#$'		//密碼必須至少8個字元
=> "qwer!@\#$"
irb(main):003:0> user.password_confirmation = 'qwer!@#$'
=> "qwer!@\#$"
irb(main):004:0> user.save!				 //儲存修改,若無問題將返回true
Enqueued ActionMailer::MailDeliveryJob (Job ID: 8f342f3c-843e-46cf-9cf3-dea3cd207354) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", {:args=>[#<GlobalID:0x00007fcdd5ce6b48 @uri=#<URI::GID gid://gitlab/User/1>>]}
=> true
irb(main):005:0> exit
[root@localhost ~]# 

gitlab管理

1. 專案管理



新增SSH KEY

[root@localhost ~]# ssh-keygen -t ed25519 -C "test"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/root/.ssh/id_ed25519): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_ed25519.
Your public key has been saved in /root/.ssh/id_ed25519.pub.
The key fingerprint is:
SHA256:KaXbKQqEzsyLW/s0k/ZnKYlT/uVP6fQnSOXEIraowJ8 test
The key's randomart image is:
+--[ED25519 256]--+
|                 |
|                 |
|        .    .   |
| .     o .o . +  |
|. ..  o So o =   |
|=.  o..+... ...  |
| =o **o+o...+.   |
|...=o=E.+o +... .|
|oo..o..=. ..o .o |
+----[SHA256]-----+
[root@localhost ~]# cat .ssh/id_ed25519
id_ed25519      id_ed25519.pub  
[root@localhost ~]# cat .ssh/id_ed25519.pub 
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIErIna7REJlY1JqX7Z4kZ2fMyyM/Dd1x6q5HnfStPbQ2 test



2. 使用者管理



輸入登入密碼

登入測試

改密碼之後就可以正常登入使用了

3. 組管理


新增使用者到組

將一個組新增到專案中


克隆專案

[root@localhost ~]# git clone [email protected]:root/test.git
Cloning into 'test'...
The authenticity of host '192.168.32.125 (192.168.32.125)' can't be established.
ECDSA key fingerprint is SHA256:frx90ADy/hsYsjrFg0CGVr1aMVpLECeXnXsTnerpZNg.
ECDSA key fingerprint is MD5:0f:89:af:a1:cb:02:5a:a5:f0:00:50:49:bc:53:97:cb.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.32.125' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
[root@localhost ~]# ls
anaconda-ks.cfg  test