Gitlab部署

一、部署gitlab

1.1 官方下载

1.1.1 官方网站

https://about.gitlab.com/

1.1.2 CE社区版下载

https://about.gitlab.com/install/?version=ce

1.2 安装过程

1.2.1 安装依赖包

#centos
yum install postfix -y

#ubuntu
apt install postfix -y
#邮件发送支持

1.2.3 添加GitLab-ce仓库

#
#centos
curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash

#ubuntu
curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash

1.2 4 通过仓库安装

#centos
EXTERNAL_URL="https://gitlab.example.com" yum install -y gitlab-ce
#ubuntu
EXTERNAL_URL="https://gitlab.example.com" apt install -y gitlab-ce

#IP访问(如果域名没有实现配好,可能会安装失败,先用ip安装,再换域名也可以)
EXTERNAL_URL="https://gitlab.example.com" apt install -y gitlab-ce

#安装时设置访问地址,根据实际情况配置自己的IP或域名。

1.2.5 查看初始密码

cat /etc/gitlab/initial_root_password 

二、配置gitlab

2.1 重要目录

#配置文件
/etc/gitlab

#安装目录
/opt/gitlab

#运行pid目录
/run/gitlab

#日志目录
/var/log/gitlab

#数据目录
/var/opt/gitlab

2.2 启动前配置

2.2.1 配置url

vim /etc/gitlab/gitlab.rb
#打开配置文件

external_url 'http://gitlab.example.com'
#修过为本机IP或域名,若安装时已配置,则跳过此步骤。

2.2.2 重新配置

cd /opt/gitlab/bin
#进入gitlab安装目录

./gitlab-ctl reconfigure
#执行重配置命令

2.2.3 添加环境变量

cat << EOF > /etc/profile.d/gitlab.sh
#gitlab
PATH=\$PATH:/opt/gitlab/bin
EOF
#创建环境变量配置文件

. /etc/profile.d/gitlab.sh
#加载环境变量
  1. 3启动GitLab

使用gitlab-ctl启动服务

gitlab-ctl start
#启动gitlab服务

restart |status |stop |reconfigure
#其他可用操作选项

三、gitlab维护

3.1 重置root密码

sudo gitlab-rails console
#进入console模式
u=User.where(id:1).first
#查找id1用户,及管理员
u.password='12345678'
#设置密码
u.password_confirmation='12345678' 
#确认密码
u.save!
#保存配置

3.2 更改gitlab存储位置

更改仓库存储位置
默认时GitLab的仓库存储位置在“/var/opt/gitlab/git-data/repositories”,在实际生产环境中显然我们不会存储在这个位置,一般都会划分一个独立的分区来存储仓库的数据,我这里规划把数据存放在“/data/git-data”目录下。

root@test1:~/tools# mkdir -pv /data/git-data 
mkdir: 已创建目录 "/data"
mkdir: 已创建目录 "/data/git-data"
root@test1:~# chown -R git.git /data/git-data  #修改创建目录的属主和属组为git用户 
root@test1:~/tools# cp /etc/gitlab/gitlab.rb /etc/gitlab/gitlab.rb.bak 
root@test1:~/tools# vim /etc/gitlab/gitlab.rb 
#启用git_data_dirs参数,并修改如下: 
git_data_dirs  路径 "/data/git-data"

#并修改external_url的值修改为规划的访问域名 
external_url 'http://test.gitlab.net'
root@test1:~/tools# gitlab-ctl reconfigure  #重新编译gitlab.rb文件,使用做的修改生效 
重新编辑后,GitLab在仓库目录会自动创建一个repositories文件,如下: 
root@test1:~# ls -ld /data/git-data/repositories/ 
drwxrws--- 2 git git 4096 1月  4 14:15 /data/git-data/repositories/

3.3 与LDAP集成

3.3.1 编辑配置文件

vim /etc/gitlab/gitlab.rb

3.3.2 开启LDAP集成

### LDAP Settings
是配置文件的LDAP配置段落

# gitlab_rails['ldap_enabled'] = false
>>取消注释,并设置为true
gitlab_rails['ldap_enabled'] = true

3.3.3 LDAP配置部分

gitlab_rails['ldap_servers'] = YAML.load <<-'EOS'
   main: # 'main' is the GitLab 'provider ID' of this LDAP server
     label: 'LDAP'
     #开启LDAP后再登录界面将出现“LDAP”的登录方式。名称可以自定义。
     host: '_your_ldap_server'
     #填入LDAP的服务器IP或域名
     port: 389
     #填写LDAP端口,默认是389,若使用SSL传输则使用663端口。
     uid: 'sAMAccountName'
     #登录名
     bind_dn: '_the_full_dn_of_the_user_you_will_bind_with'
     #LDAP管理员用户名,格式cn=manager,dc=domain,dc=com
     password: '_the_password_of_the_bind_user'
     #LDAP管理员用户密码
     encryption: 'plain' # "start_tls" or "simple_tls" or "plain"
     #加密方式
     verify_certificates: true
#    smartcard_auth: false
     #是否支持智能卡认证
     active_directory: true
     #目标LDAP服务器是否是AD
     allow_username_or_email_login: false
     #是否支持用户名和邮箱登录
     lowercase_usernames: false
     #将从LDAP服务器接收到的用户名小写使用
     block_auto_created_users: false
     #禁止自动创建的用户
     base: ''
     #开始搜索用户的目录
     user_filter: ''
     ## EE only 以下是gitlab企业版的功能
#    group_base: ''
#    admin_group: ''
#    sync_ssh_keys: false
EOS

3.3.4 关于LDAP集成的官方说明文档

https://docs.gitlab.com/ee/administration/auth/ldap/index.html

3.4 配置提醒邮箱

3.5开启tls/ssl/https

开启https需要修改gitlab配置文件,并准备好证书。

#创建证书存放目录,gitlab的默认证书路径是/etc/gitlab/ssl,如果目录不存在则创建。
mkdir /etc/gitlab/ssl
#上传证书crt和key文件,名称不限。

#编辑gitlab配置文件
vim /etc/gitlab/gitlab.rb

#指定gitlab的https url
external_url 'https://gitlab.example.com'

#禁用letsencrypt
letsencrypt['enable'] = false

#自动将http重定向到http
nginx['redirect_http_to_https'] = true


#重新配置gitlab
gitlab-ctl reconfigure
#使用https访问gitlab

四、迁移与升级

4.1 备份

gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
#暂停服务

gitlab-rake gitlab:backup:create
#执行备份

/var/opt/gitlab/backups/
#备份文件路径
#备份完成后会删除旧的备份文件

#如果要迁移到另一个GitLab实例,需要将/etc/gitlab下的gitlab.rb和gitlab-secrets.json
/etc/gitlab

gitlab-ctl start
#启动服务

4.2 从备份恢复

gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
#暂停服务

/var/opt/gitlab/backups
#备份文件需要拷贝到指定目录中。

gitlab-rake gitlab:backup:restore BACKUP=162087480_2021_05_14_13.11.3_gitlab_backup.tar
#执行恢复

gitlab-ctl start
#启动服务

4.2 升级

标签: Gitlab

添加新评论