[Advanced Operations and Maintenance Knowledge] An article will take you to understand the basic operations of GitLab (installation deployment + localization + code push + branch merging + protection of the main branch + GitLab backup)

GitLab is an open source project for warehouse management system. It uses Git as a code management tool, and the web service built on this basis can access public or private projects through the web interface. It has similar functions to Github , able to browse the source code, manage defects and comments, you can view the team's access to the warehouse, it is very special in browsing the submitted version and provide a file history library, team members can use the built-in simple chat program (Wall) to communicate , which also provides a code snippet collection feature for easy code reuse.

官网:https://about.gitlab.com/
国内镜像:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/

Environmental preparation

1. Installation environment

1、Centos7.9
2、2G内存(测试),生产至少需要4G
3、安装包:gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm(下载连接放在文末)
4、禁用防火墙,关闭selinux

2. Install GitLab

It can be downloaded from the official website, or you can choose to install the rpm package (the download link is at the end of the article)

[root@Gitlab ~]# yum -y install policycoreutils-python    #安装依赖
[root@Gitlab ~]# rpm -ivh gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm   #安装软件包
vim /etc/gitlab/gitlab.rb       #  gitlab 配置文件
更改url地址为本机IP地址 external_url 'http://10.0.0.200'
gitlab-ctl reconfigure          # 更改配置文件后需重新配置

/opt/gitlab/                    # gitlab的程序安装目录
/var/opt/gitlab                 # gitlab目录数据目录
/var/opt/gitlab/git-dfata       # 存放仓库数据
gitlab-ctl status               # 查看目前gitlab所有服务运维状态
gitlab-ctl stop                 # 停止gitlab服务
gitlab-ctl stop nginx           # 单独停止某个服务
gitlab-ctl tail                 # 查看所有服务的日志

GitLab的服务构成:
nginx:   静态web服务器
gitlab-workhorse: 轻量级的反向代理服务器
logrotate:日志文件管理工具
postgresql:数据库
redis:缓存数据库
sidekiq:用于在后台执行队列任务(异步执行)。(Ruby)
unicorn:An HTTP server for Rack applications,GitLab Rails应用是托管在这个服务器上面的。(Ruby Web Server,主要使用Ruby编写)

GitLab Sinicization

Direct access to 10.0.0.200, display English interface 

1. Download the Chinese patch

[root@Gitlab ~]# git clone [email protected]:xhang/gitlab.git    #需要先做免密钥

将Gitlab主机上的公钥复制到gitlab官网(https://gitlab.com)自己的账号配置中

用http下载方式理论上也可以,但是我这边测试下载不上

2. View all branch versions

[root@Gitlab gitlab]# git branch -a    #需要在安装好的gitlab目录下

3. Compare versions and generate patch packages

[root@Gitlab gitlab]# git diff remotes/origin/10-2-stable remotes/origin/10-2-stable-zh > ../10.2.2-zh.diff
[root@Gitlab gitlab]# cd ..
[root@Gitlab ~]# ls
10.2.2-zh.diff  gitlab  gitlab.bak  gitlab-ce-10.2.2-ce

4. Stop the server

gitlab-ctl stop

5. Patching

[root@Gitlab ~]# patch -d /opt/gitlab/embedded/service/gitlab-rails -p1 < 10.2.2-zh.diff 

6. Startup and reconfiguration 

gitlab-ctl start
gitlab-ctl reconfigure

Visit again to display the Chinese interface

GitLab configuration using

For the first login, you need to configure the password first, the shortest is eight characters, the login user name is root, and the password is the password configured by yourself

1. Configuration Appearance

After configuration, the login interface will be modified

After logging in, the upper left corner has been changed

2. Turn off automatic registration

Operate according to actual needs. If it is an intranet, it doesn’t matter whether you cancel or not. If there is a public network, you can log in to our gitlab if you are found and registered at will, which is very dangerous.

Scroll down to save after canceling 

3. Create groups and users

create group

create user 

After creating, click edit and add user password here 

 

4. Add users to groups

5. Create a warehouse (project)

6. Log in to the koten user to test whether you can see the empty test warehouse

When the browser is in privacy mode, users who log in to koten for the first time need to change their password, or the original password can be the same as the new password

7. Add ssh-keys to GitLab

A server key can only be added to one gitlab server, and a user can add multiple keys

8. Add a remote warehouse and push the local code to the remote warehouse

[root@Gitlab ~]# mkdir git_test
[root@Gitlab ~]# cd git_test
[root@Gitlab git_test]# git init    #初始化git仓库
Initialized empty Git repository in /root/git_test/.git/
[root@Gitlab git_test]# git remote add origin [email protected]:koten_group/test.git    #添加远程仓库
[root@Gitlab git_test]# git checkout -b master    #创建本地master分支
Switched to a new branch 'master'
[root@Gitlab git_test]# git remote rename origin koten-origin    #更改origin名字
[root@Gitlab git_test]# touch 1.txt
[root@Gitlab git_test]# git add .                  #添加项目到缓存区
[root@Gitlab git_test]# git commit -m "file 1.txt" #添加项目到本地仓库
[master (root-commit) e364410] file 1.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 1.txt
[root@Gitlab git_test]# git push -u koten-origin master    #推送本地仓库至远程仓库
Counting objects: 3, done.
Writing objects: 100% (3/3), 199 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:koten_group/test.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from koten-origin.

Refresh the files that can see the remote warehouse 

9. Clone the code to another host and test the push

If you do not do authentication, you will be asked to enter the password of gitlab, we use key for authentication 

[root@Web01 ~]# ssh-keygen
[root@Web01 ~]# cat .ssh/id_rsa.pub    #复制进koten用户的ssh密钥

 clone code

[root@Web01 git_test]#   git config --global user.email "[email protected]"
[root@Web01 git_test]#   git config --global user.name "Your Name"
[root@Web01 ~]# mkdir git_test
[root@Web01 ~]# cd git_test
[root@Web01 git_test]# git init
Initialized empty Git repository in /root/git_test/.git/
[root@Web01 git_test]# git clone [email protected]:koten_group/test.git    #克隆远程仓库代码
Cloning into 'test'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
[root@Web01 git_test]# tree
.
└── test
    └── 1.txt

1 directory, 1 file

push code

[root@Web01 git_test]# git branch dev
[root@Web01 git_test]# git checkout dev
[root@Web01 git_test]# touch dev_test
[root@Web01 git_test]# git add .
[root@Web01 git_test]# git commit -m "add dev_test"
[dev cac97e4] add dev_test
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 dev_test
[root@Web01 git_test]# git remote add origin [email protected]:koten_group/test.git    #添加远程仓库
[root@Web01 git_test]# git push -u origin dev    #推送dev分支到远程仓库
Counting objects: 5, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 464 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
remote: 
remote: To create a merge request for dev, visit:
remote:   http://10.0.0.200/koten_group/test/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote: 
To [email protected]:koten_group/test.git
 * [new branch]      dev -> dev
Branch dev set up to track remote branch dev from origin.

10. Merge branch requests

For the first time, you can click Create Merge Request in the upper right corner

 

 

 Since the identity given by dev is that the developer has the authority to independently accept the merge request, if the identity we give is a reporter, we need to manage it under the root account and accept the request. If the master branch is protected, we also need to accept the request through root

After merging, although the remote warehouse on the root side can display the merged files, but not in the gitlab local warehouse, we can pull to update the warehouse

[root@Gitlab git_test]# ls
1.txt
[root@Gitlab git_test]# git pull
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
From 10.0.0.200:koten_group/test
   e364410..600d0ef  master     -> koten-origin/master
Updating e364410..600d0ef
Fast-forward
 dev      | 0
 dev_test | 0
 test     | 1 +
 3 files changed, 1 insertion(+)
 create mode 100644 dev
 create mode 100644 dev_test
 create mode 160000 test
[root@Gitlab git_test]# ls
1.txt  dev  dev_test  test

11. Set up and protect the main branch

We can set the master as protection so that users other than the root user cannot push the code directly to the main branch

[root@Web01 git_test]# git checkout master    #切换到master分支
Switched to branch 'master'
[root@Web01 git_test]# git merge dev          #合并dev分支
Updating 53298b6..cac97e4
Fast-forward
 dev_test | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 dev_test    
[root@Web01 git_test]# git push -u origin master    #推送主分支
To [email protected]:koten_group/test.git    
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]:koten_group/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 merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

12. Test push protected branches on the root side

#由于其他分支进行推送,和master端内容不一致,所以无法进行推送,使用git pull把代码拉取到本地,或者git fetch 把代码拉取到本地仓库后进行合并(注意:git pull = git tetch+git merge)
[root@Gitlab git_test]# git push -u koten-origin master 
Branch master set up to track remote branch master from koten-origin.
Everything up-to-date

GitLab Backup

对gitlab进行备份将会创建一个包含所有库和附件的归档文件。对备份的恢复只能恢复到与备份时的gitlab相同的版本。将gitlab迁移到另一台服务器上的最佳方法就是通过备份和还原。

gitlab提供了一个简单的命令行来备份整个gitlab,并且能灵活的满足需求。
备份文件将保存在配置文件中定义的backup_path中,文件名为TIMESTAMP_gitlab_backup.tar,TIMESTAMP为备份时的时间戳。TIMESTAMP的格式为:EPOCH_YYYY_MM_DD_Gitlab-version。
如果自定义备份目录需要赋予git权限
配置文件/etc/gitlab/gitlab.rb 中加入
gitlab_rails['backup_path'] = '/data/backup/gitlab'
gitlab_rails['backup_keep_time'] = 604800       备份保留的时间(以秒为单位,这个是七天默认值),

mkdir -p /data/backup/gitlab
chown -R git.git /data/backup/gitlab
完成后执行
gitlab-ctl reconfigure

gitlab rpm package download link: link: https://pan.baidu.com/s/1WCXrPxDzEchFys4i7kjr8A?pwd=q80w

I am koten, with 10 years of experience in operation and maintenance, and I continue to share dry goods in operation and maintenance. Thank you for your reading and attention!

Guess you like

Origin blog.csdn.net/qq_37510195/article/details/130795169