GitLab安裝及使用

背景:在糾結使用Jenkins還是GitLab作爲CI/CD工具,最終決定使用GitLab,原因就是非常看好GitLab的未來。
安裝環境:

  • CentOS 7.8.2003

部署:

  • 允許http、https、ssh連接

>>> sudo yum install -y curl policycoreutils-python openssh-server
>>> sudo systemctl enable sshd
>>> sudo systemctl start sshd
# 由於之前安裝Harbor已經將防火牆關閉,故以下命令可不執行
>>> sudo firewall-cmd --permanent --add-service=http
>>> sudo firewall-cmd --permanent --add-service=https
>>> sudo systemctl reload firewalld
  • 安裝postfix

>>> sudo yum install postfix
>>> sudo systemctl enable postfix
>>> sudo systemctl start postfix
  • 添加gitlab倉庫

這裏安裝的是ee版而非ce版,關於ee和ce的區別參考:https://about.gitlab.com/install/ce-or-ee/
簡單來說就是ce版有的ee版都有,ee版有的ce版不一定有,ce版更改爲ee版需暫時終止服務

>>> curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | sudo bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  6856  100  6856    0     0   6005      0  0:00:01  0:00:01 --:--:--  6008
Detected operating system as centos/7.
Checking for curl...
Detected curl...
Downloading repository file: https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/config_file.repo?os=centos&dist=7&source=script
done.
Installing pygpgme to verify GPG signatures...
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * elrepo: mirrors.neusoft.edu.cn
 * epel: mirror.lzu.edu.cn
gitlab_gitlab-ce/x86_64/signature                                                                                                                                                           |  862 B  00:00:00     
gitlab_gitlab-ce/x86_64/signature                                                                                                                                                           | 1.0 kB  00:00:00 !!! 
gitlab_gitlab-ce-source/signature                                                                                                                                                           |  862 B  00:00:00     
gitlab_gitlab-ce-source/signature                                                                                                                                                           |  951 B  00:00:00 !!! 
gitlab_gitlab-ee-source/signature                                                                                                                                                           |  862 B  00:00:00     
gitlab_gitlab-ee-source/signature                                                                                                                                                           |  951 B  00:00:00 !!! 
Package pygpgme-0.3-9.el7.x86_64 already installed and latest version
Nothing to do
Installing yum-utils...
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * elrepo: mirrors.neusoft.edu.cn
 * epel: mirror.lzu.edu.cn
Package yum-utils-1.1.31-54.el7_8.noarch already installed and latest version
Nothing to do
Generating yum cache for gitlab_gitlab-ee...
Generating yum cache for gitlab_gitlab-ee-source...

The repository is setup! You can now install packages.
  • 安裝

>>> yum install -y gitlab-ee
     _______ __  __          __
    / ____(_) /_/ /   ____ _/ /_
   / / __/ / __/ /   / __ `/ __ \
  / /_/ / / /_/ /___/ /_/ / /_/ /
  \____/_/\__/_____/\__,_/_.___/
  

Upgrade complete! If your GitLab server is misbehaving try running
  sudo gitlab-ctl restart
before anything else.
If you need to roll back to the previous version you can use the database
backup made during the upgrade (scroll up for the filename).

  Verifying  : gitlab-ee-13.1.1-ee.0.el7.x86_64                                                                                                                                                                1/1 

Installed:
  gitlab-ee.x86_64 0:13.1.1-ee.0.el7                                                                                                                                                                               

Complete!
  • 修改配置

  1. 由於該主機80端口已被佔用,故將nginx監聽端口改爲9091,將unicorn端口由常用的8080端口改爲9092
  2. 由於我的gitlab只存在於內網環境,故暫時不考慮https
# 修改配置
>>> vi /etc/gitlab/gitlab.rb
external_url 'http://xx.xx.xx.xx:9091'
nginx['listen_port'] = 9091
unicorn['port'] = 9092
# 使配置生效
>>> gitlab-ctl reconfigure
...
Running handlers:
Running handlers complete
Chef Client finished, 9/799 resources updated in 20 seconds
gitlab Reconfigured!
# 重啓gitlab
>>> gitlab-ctl restart
ok: run: alertmanager: (pid 6783) 0s
ok: run: gitaly: (pid 6805) 0s
ok: run: gitlab-exporter: (pid 6833) 0s
ok: run: gitlab-workhorse: (pid 6850) 1s
ok: run: grafana: (pid 6865) 0s
ok: run: logrotate: (pid 6958) 1s
ok: run: nginx: (pid 6965) 0s
ok: run: node-exporter: (pid 6977) 0s
ok: run: postgres-exporter: (pid 6984) 1s
ok: run: postgresql: (pid 6995) 0s
ok: run: prometheus: (pid 7004) 1s
ok: run: puma: (pid 7025) 0s
ok: run: redis: (pid 7030) 1s
ok: run: redis-exporter: (pid 7036) 0s
ok: run: sidekiq: (pid 7049) 0s

參考:
https://about.gitlab.com/install/#centos-7
https://www.jianshu.com/p/b04356e014fa
https://blog.csdn.net/wangyy130/article/details/85633303

使用

打開 http://xx.xx.xx.xx:9091
默認管理賬戶爲root
在這裏插入圖片描述

  • 從github導入項目

在這裏插入圖片描述
在github中創建accesstoken https://github.com/settings/tokens
在這裏插入圖片描述
如果只選擇read:packages則只能看到公開的項目,勾選repo則能夠看到私有的項目
由於網絡原因導入過程比較慢,耐心等待就好
在這裏插入圖片描述
如果出現下圖,是因爲訪問github超時,多刷新幾次就好了在這裏插入圖片描述


2020-06-29更新

  • 從github導入項目

由於網絡原因,使用從github導入會非常非常慢,這裏推薦使用從url導入,僅需幾秒鐘就搞定

在這裏插入圖片描述


2020-06-30更新

  • 同時推送給gitlab和github

上面介紹瞭如何從github將項目遷移至gitlab,但如果項目想要同時push到gitlab和github該怎麼做呢?
我們在github和gitlab同時創建空的名爲test的私有項目,然後從gitlab clone下該項目:

>>> git clone http://xx.xx.xx.xx:9091/name/test.git 
Cloning into 'test'...
Username for 'http://xx.xx.xx.xx:9091': name
Password for 'http://[email protected]:9091': 
warning: You appear to have cloned an empty repository.

添加文件並提交

>>> touch test
>>> git add .
>>> git commit -m 'added test'

關鍵步驟來了,如果想同時push給gitlab和github,則直接編輯.git/config文件

>>> vim .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = http://xx.xx.xx.xx:9091/name/test.git
        url = https://github.com/name/test.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master

push

>>> git push origin master
Username for 'http://xx.xx.xx..xx:9091': name
Password for 'http://[email protected]:9091': 
Counting objects: 3, done.
Writing objects: 100% (3/3), 199 bytes | 199.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://xx.xx.xx.xx:9091/name/test.git
 * [new branch]      master -> master
Username for 'https://github.com': [email protected]
Password for 'https://[email protected]@github.com': 
To https://github.com/name/test.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/name/test.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

發現push到github時出現了拒絕,原因是這個項目本來是在gitlab上的,中間添加github,github會拒絕接受push,這時只需添加-f參數,強制push,但這會完全覆蓋github項目原來的文件

>>> git push -f origin master
Username for 'http://xx.xx.xx.xx:9091': name
Password for 'http://[email protected]:9091': 
Everything up-to-date
Username for 'https://github.com': [email protected]
Password for 'https://[email protected]@github.com': 
Counting objects: 3, done.
Writing objects: 100% (3/3), 199 bytes | 199.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/name/test.git
 + ab30681...d91ec3e master -> master (forced update)

參考:https://medium.com/@hongjhoumaxcian/%E5%A6%82%E4%BD%95%E5%90%8C%E6%99%82%E6%8E%A8%E9%80%81git-repo-%E5%88%B0gitlab%E5%92%8Cgithub-how-to-push-git-repo-to-both-gitlab-and-github-simultaneously-f7d868fd13ab

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章