作者: 宋发元
最后更新时间:2024-12-24
GitLab安装及使用教程
官方教程 https://docs.gitlab.com/ee/install/docker.html
Docker安装GitLab
宿主机创建容器持久化目录卷
mkdir -p /docker/gitlab/{config,data,logs}
拉取GitLab镜像
docker pull gitlab/gitlab-ce:15.3.1-ce.0
运行GitLab容器(注意修改IP为你的宿主机IP)
docker run -d \
-p 8888:80 \
-p 2222:22 \
-v /docker/gitlab/config:/etc/gitlab \
-v /docker/gitlab/data:/var/opt/gitlab \
-v /docker/gitlab/logs:/var/log/gitlab \
-e GITLAB_OMNIBUS_CONFIG="external_url 'http://192.168.2.110'; gitlab_rails['gitlab_shell_ssh_port'] = 2222; gitlab_rails['time_zone'] = 'Asia/Shanghai'" \
-e TZ=Asia/Shanghai \
--memory 16G \
--cpus=8 \
--restart always \
--name gitlab \
gitlab/gitlab-ce:15.3.1-ce.0
浏览器访问GitLab
查看初始密码
docker exec -it gitlab grep "Password": /etc/gitlab/initial_root_password
浏览器登录Gitlab: http://IP:8888
账号:root
密码:【上一步查看的密码】
设置可见性和访问控制
操作路径:Admin Area > Settings > General > Visibility and access controls
修改超级管理员密码
关闭用户自动注册功能
以root权限登录,在Gitlab的主菜单上,选择Menu --> Admin,如下所示:
在弹出的页面中,取消掉Sign-up enabled选项前面的勾,然后点击底部的Save changes后,就可以使得我们的配置生效了。这样在登录的时候,就无法实现自动注册了。如下所示:
创建用户
在首页找到管理员,如图所示:
点击小人图标
找到Users,创建用户:
填写相关信息
注意:需要填写一下email,用于给被创建用户发送初始登录密码。
创建组
Gitlab上创建项目时,由于新项目的创建必须依赖于一个组,因此,我们在创建项目前,先创建组,如下所示:
输入组的名称和描述,组的URL会随着组名称的输入而自动出现,在组的权限处,我们选择Private,配置完成后如下所示:
点击最下方的Create Group,就可以完成组的创建了。
创建项目
项目的创建与组的创建过程类似,都是输入项目名称,选择组的名称,项目描述以及权限等。
建议直接在组下创建项目,有三种方式:创建空项目、根据模板创建、从其他代码仓库迁移,根据个人情况选择:
修改语言
系统默认为英文,如果需要修改为中文,操作如下。
刷新浏览器即可切换为中文显示
数据备份与恢复
数据备份
进入老Gitlab的docker容器
# 进入docker容器
docker exec -it gitlab bash
备份数据
# 备份数据(在gitlab容器内执行)
gitlab-rake gitlab:backup:create /var/opt/gitlab/backups/
# 备份结束信息
2024-12-24 03:01:37 +0000 -- Dumping repositories ... done
2024-12-24 03:01:37 +0000 -- Dumping uploads ...
2024-12-24 03:01:37 +0000 -- Dumping uploads ... done
2024-12-24 03:01:37 +0000 -- Dumping builds ...
2024-12-24 03:01:37 +0000 -- Dumping builds ... done
2024-12-24 03:01:37 +0000 -- Dumping artifacts ...
2024-12-24 03:01:41 +0000 -- Dumping artifacts ... done
2024-12-24 03:01:41 +0000 -- Dumping pages ...
2024-12-24 03:01:41 +0000 -- Dumping pages ... done
2024-12-24 03:01:41 +0000 -- Dumping lfs objects ...
2024-12-24 03:01:41 +0000 -- Dumping lfs objects ... done
2024-12-24 03:01:41 +0000 -- Dumping terraform states ...
2024-12-24 03:01:41 +0000 -- Dumping terraform states ... done
2024-12-24 03:01:41 +0000 -- Dumping container registry images ... [DISABLED]
2024-12-24 03:01:41 +0000 -- Dumping packages ...
2024-12-24 03:01:41 +0000 -- Dumping packages ... done
2024-12-24 03:01:41 +0000 -- Creating backup archive: 1735009265_2024_12_24_15.3.1_gitlab_backup.tar ...
2024-12-24 03:01:42 +0000 -- Creating backup archive: 1735009265_2024_12_24_15.3.1_gitlab_backup.tar ... done
2024-12-24 03:01:42 +0000 -- Uploading backup archive to remote storage ... [SKIPPED]
2024-12-24 03:01:42 +0000 -- Deleting old backups ... [SKIPPED]
2024-12-24 03:01:42 +0000 -- Deleting tar staging files ...
2024-12-24 03:01:42 +0000 -- Cleaning up /var/opt/gitlab/backups/backup_information.yml
2024-12-24 03:01:42 +0000 -- Cleaning up /var/opt/gitlab/backups/db
2024-12-24 03:01:42 +0000 -- Cleaning up /var/opt/gitlab/backups/repositories
2024-12-24 03:01:43 +0000 -- Cleaning up /var/opt/gitlab/backups/uploads.tar.gz
2024-12-24 03:01:43 +0000 -- Cleaning up /var/opt/gitlab/backups/builds.tar.gz
2024-12-24 03:01:43 +0000 -- Cleaning up /var/opt/gitlab/backups/artifacts.tar.gz
2024-12-24 03:01:43 +0000 -- Cleaning up /var/opt/gitlab/backups/pages.tar.gz
2024-12-24 03:01:43 +0000 -- Cleaning up /var/opt/gitlab/backups/lfs.tar.gz
2024-12-24 03:01:43 +0000 -- Cleaning up /var/opt/gitlab/backups/terraform_state.tar.gz
2024-12-24 03:01:43 +0000 -- Cleaning up /var/opt/gitlab/backups/packages.tar.gz
2024-12-24 03:01:43 +0000 -- Deleting tar staging files ... done
2024-12-24 03:01:43 +0000 -- Deleting backups/tmp ...
2024-12-24 03:01:43 +0000 -- Deleting backups/tmp ... done
2024-12-24 03:01:43 +0000 -- Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data
and are not included in this backup. You will need these files to restore a backup.
Please back them up manually.
2024-12-24 03:01:43 +0000 -- Backup 1735009265_2024_12_24_15.3.1 is done.
拷贝备份文件1735009265_2024_12_24_15.3.1_gitlab_backup.tar
到新服务器宿主机目录/docker/gitlab/data/backups/
。
备份配置文件
把旧宿主机目录/docker/gitlab/config
下的gitlab.rb
和gitlab-secrets.json
两个配置文件也备份,并拷贝到新服务器宿主机目录/docker/gitlab/config
下。
数据恢复
上传备份文件1735009265_2024_12_24_15.3.1_gitlab_backup.tar
到新Gitlab的宿主机目录/docker/gitlab/data/backups/
。
上传备份配置文件gitlab.rb
和gitlab-secrets.json
到新GitLab的宿主机目录/docker/gitlab/config
。
修改权限
修改备份文件权限
# 修改备份文件权限
chown git.git 1735009265_2024_12_24_15.3.1_gitlab_backup.tar
数据恢复
# 进入docker容器
docker exec -it gitlab bash
# 停止相关数据连接服务
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
# 备份文件数据恢复,这里注意文件名:gitlab-rake gitlab:backup:restore BACKUP=备份文件编号
gitlab-rake gitlab:backup:restore BACKUP=1735009265_2024_12_24_15.3.1
# 备份开始信息
2024-12-24 03:25:32 +0000 -- Unpacking backup ...
2024-12-24 03:25:34 +0000 -- Unpacking backup ... done
2024-12-24 03:25:34 +0000 -- Restoring main_database ...
2024-12-24 03:25:34 +0000 -- Be sure to stop Puma, Sidekiq, and any other process that
connects to the database before proceeding. For Omnibus
installs, see the following link for more information:
https://docs.gitlab.com/ee/raketasks/backup_restore.html#restore-for-omnibus-gitlab-installations
Before restoring the database, we will remove all existing
tables to avoid future upgrade problems. Be aware that if you have
custom tables in the GitLab database these tables and all data will be
removed.
Do you want to continue (yes/no)? yes
Removing all tables. Press `Ctrl-C` within 5 seconds to abort
2024-12-24 11:25:48 +0800 -- Cleaning the database ...
2024-12-24 11:25:50 +0800 -- done
# 备份结束信息
2024-12-24 03:26:40 +0000 -- Restoring repositories ... done
2024-12-24 03:26:40 +0000 -- Restoring uploads ...
2024-12-24 03:26:40 +0000 -- Restoring uploads ... done
2024-12-24 03:26:40 +0000 -- Restoring builds ...
2024-12-24 03:26:40 +0000 -- Restoring builds ... done
2024-12-24 03:26:40 +0000 -- Restoring artifacts ...
2024-12-24 03:26:40 +0000 -- Restoring artifacts ... done
2024-12-24 03:26:40 +0000 -- Restoring pages ...
2024-12-24 03:26:40 +0000 -- Restoring pages ... done
2024-12-24 03:26:40 +0000 -- Restoring lfs objects ...
2024-12-24 03:26:40 +0000 -- Restoring lfs objects ... done
2024-12-24 03:26:40 +0000 -- Restoring terraform states ...
2024-12-24 03:26:40 +0000 -- Restoring terraform states ... done
2024-12-24 03:26:40 +0000 -- Restoring packages ...
2024-12-24 03:26:40 +0000 -- Restoring packages ... done
This task will now rebuild the authorized_keys file.
You will lose any data stored in the authorized_keys file.
Do you want to continue (yes/no)? yes
2024-12-24 03:26:48 +0000 -- Deleting tar staging files ...
2024-12-24 03:26:48 +0000 -- Cleaning up /var/opt/gitlab/backups/backup_information.yml
2024-12-24 03:26:48 +0000 -- Cleaning up /var/opt/gitlab/backups/db
2024-12-24 03:26:48 +0000 -- Cleaning up /var/opt/gitlab/backups/repositories
2024-12-24 03:26:48 +0000 -- Cleaning up /var/opt/gitlab/backups/uploads.tar.gz
2024-12-24 03:26:48 +0000 -- Cleaning up /var/opt/gitlab/backups/builds.tar.gz
2024-12-24 03:26:48 +0000 -- Cleaning up /var/opt/gitlab/backups/artifacts.tar.gz
2024-12-24 03:26:48 +0000 -- Cleaning up /var/opt/gitlab/backups/pages.tar.gz
2024-12-24 03:26:48 +0000 -- Cleaning up /var/opt/gitlab/backups/lfs.tar.gz
2024-12-24 03:26:48 +0000 -- Cleaning up /var/opt/gitlab/backups/terraform_state.tar.gz
2024-12-24 03:26:48 +0000 -- Cleaning up /var/opt/gitlab/backups/packages.tar.gz
2024-12-24 03:26:48 +0000 -- Deleting tar staging files ... done
2024-12-24 03:26:48 +0000 -- Deleting backups/tmp ...
2024-12-24 03:26:48 +0000 -- Deleting backups/tmp ... done
2024-12-24 03:26:48 +0000 -- Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data
and are not included in this backup. You will need to restore these files manually.
2024-12-24 03:26:48 +0000 -- Restore task is done.
至此,GitLab数据迁移已完成,登录新的GitLab即可查看数据!
问题解决
超级管理员密码重置
如果root账户密码不正确,则需要重新设置,步骤如下:
# 首先进入gitlab容器内
docker exec -it gitlab /bin/bash
# 进入控制台(这步需要等待一会)
gitlab-rails console -e production
# 查询root用户(即id为1)
user=User.where(id:1).first
# 设置密码
user.password='123456789'
# 保存
user.save!
# 退出
exit
修改权限
如果启动时,报错各种权限问题,可以执行这个指令。
For a comprehensive list of configuration options please see the Omnibus GitLab readme
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md
If this container fails to start due to permission problems try to fix it by executing:
docker exec -it gitlab update-permissions
docker restart gitlab
修改配置(暂未使用到)
vim /docker/gitlab/config/gitlab.rb
# gitlab访问地址
external_url 'http://192.168.2.110'
# ssh连接端口
gitlab_rails['gitlab_shell_ssh_port'] = 2222
# 时区
gitlab_rails['time_zone'] = 'Asia/Shanghai'
让配置生效
docker exec -it gitlab gitlab-ctl reconfigure
重启gitlab服务
docker exec -it gitlab gitlab-ctl restart