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

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