GitLab安装篇

Foreword

最近准备迁移公司的svn仓库,记录下自己学习及迁移过程.

Install

GitLab目前提供三个版本:(以下都是docker镜像)

只是为了满足基本使用,以CE版本安装.

Run

  • 运行环境要求

    4G内存(最低2G)、4核或8核CPU

启动GitLab是比较简单.但在正式开始使用之前,对它的配置有个全面的了解是很有必要的.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ docker run -d \
--hostname 192.168.1.222 \
--publish 443:443 --publish 80:80 --publish 22:22 \
--name gitlab \
--restart always \
--volume /data/gitlab/config:/etc/gitlab \
--volume /data/gitlab/logs:/var/log/gitlab \
--volume /data/gitlab/data:/var/opt/gitlab \
--network host \
gitlab/gitlab-ce:latest
# 443 https访问端口,80 http访问端口, 22 docker容器端口.
# /data/gitlab/config配置文件目录
# /data/gitlab/logs日志存储目录
# /data/gitlab/data应用数据存储目录
  • --env GITLAB_OMNIBUS_CONFIG=""

    该参数可以覆盖容器中GitLab的配置文件/etc/gitlab/gitlab.rb.具体参数详见gitlab.rb.template

Language translate

使用xlang提供的汉化包.

测试汉化包

我使用的gitlab版本是v11.3.5,在xlang的branch中只能找到11-3-stable-zh.所以我先进行了测试,看是否可用.(如何测试?可以按照这篇文章gitlab汉化操作.)

实际上就是使用汉化包的文件替换/opt/gitlab/embedded/service/gitlab-rails/中的文件

1
2
3
$ cp -rf gitlab-11-3-stable-zh/* /opt/gitlab/embedded/service/gitlab-rails/
$ gitlab-ctl reconfigure
$ gitlab-ctl restart

测试暂时没发现问题,就是使用11-3-stable-zh版本的汉化包.

如何将汉化包加入容器?

我用的是镜像,难道我将汉化包文件路径映射到容器中吗?好吧,来测试一把.

1
2
3
4
5
6
7
8
9
10
11
$ docker run -d \
--hostname 192.168.1.222 \
--publish 443:443 --publish 80:80 --publish 22:22 \
--name gitlab \
--restart always \
--volume /data/gitlab/config:/etc/gitlab \
--volume /data/gitlab/logs:/var/log/gitlab \
--volume /data/gitlab/data:/var/opt/gitlab \
--volume /data/gitlab/data/zh/gitlab:/opt/gitlab/embedded/service/gitlab-rails \
--network host \
gitlab/gitlab-ce:11.3.5-ce.0

结果悲剧了,一直不断自动重启.这种问题也不是我能解决的,只能先放弃了.

怎么办呢?搜索了一番,找到了twang2218/gitlab-ce-zh提供的Dockerfile.

在twang2218的Dockerfile中找到了使用替换文件的操作

image-20181023142449054

既然twang2218能打包成功,说明Dockerfile是正确的.现在只要替换为我使用的版本和汉化包版本应该同样有效.(他山之石可以攻玉)

构建汉化包镜像
生成Dockerfile

在github上直接Forktwang2218/gitlab-ce-zh的代码,再clone到本地.

1
$ git clone https://github.com/steven-ji/gitlab-ce-zh.git

修改version.sh文件,增加11.3.5

1
2
3
4
5
6
7
8
$ vim version.sh
export VERSIONS=(
10.7.7
10.8.7
11.0.5
11.1.4
11.3.5
)

使用脚本生成Dockerfile文件.会生成11.3目录,并且里面有个Dockerfile文件.

1
$ ./build generate 11.3

注:由于xlang提供v11.3.5的汉化包为11.3-stable-zh,所以需要修改Dockerfile文件中的v11.3.5-zh为11.3-stable-zh

通过docker hub自动构建镜像

关联github即可,具体操作忽略.

使用汉化版gitlab
1
2
3
4
5
6
7
8
9
10
$ docker pull jilingjun1014/gitlab-zh:v11.3.5
$ docker run -d \
--publish 443:443 --publish 80:80 --publish 22:22 \
--name gitlab \
--restart always \
--volume /data/gitlab/config:/etc/gitlab \
--volume /data/gitlab/logs:/var/log/gitlab \
--volume /data/gitlab/data:/var/opt/gitlab \
--network host \
jilingjun1014/gitlab-zh:v11.3.5

Config

GitLab启动加载的配置文件为gitlab.rb,先创建出来.

1
$ touch /data/gitlab/config/gitlab.rb

邮件通知配置

邮件的配置方式比较多,我的是以阿里云企业邮箱为主.(以下提供链接)

1
2
3
4
5
6
7
8
9
10
gitlab_rails['gitlab_email_from'] = "username@company.com"
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.mxhichina.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "username@company.com"
gitlab_rails['smtp_password'] = "password"
gitlab_rails['smtp_domain'] = "mxhichina.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
测试邮箱
1
2
3
4
# ssh到Gitlab的docker容器中.
$ docker exec -ti gitlab /bin/bash
$ gitlab-rails console
$ Notify.test_email('destination_email@address.com', 'Message Subject', 'Message Body').deliver_now

配置外部访问的URL

不使用内置的nginx时可以不配置.

  • 支持https
1
2
3
4
5
# 配置文件中添加如下,则访问GitLab的路径就是:http://gitlab.example.com
external_url "http://gitlab.example.com"
external_url "https://gitlab.example.com"
# 重新加载配置
$ gitlab-ctl reconfigure

配置端口

  • 配置web访问端口

    配置文件external_url属性指定.(实际上,修改的是容器内的端口)

1
2
external_url "http://gitlab.example.com:9080"
external_url "https://gitlab.example.com:9443"

如:访问端口为9080和9443,则在容器启动时,宿主机与容器的映射端口为-p 9443:9443 -p 9080:9080

  • 配置容器ssh端口

    配置文件:gitlab_rails['gitlab_shell_ssh_port'] = 9022

配置支持Https

GitLab安装时默认绑定安装了一个内置的Nginx.

使用内置Nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# set access url
external_url "http://gitlab.example.com"
external_url "https://gitlab.example.com"
# compatable http
nginx['redirect_http_to_https'] = true
# set certs
nginx['ssl_certificate'] = "/etc/gitlab/ssl/gitlab.example.com.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/gitlab.example.com.key"
# set errer page info
nginx['custom_error_pages'] = {
'404' => {
'title' => 'Example title',
'header' => 'Example header',
'message' => 'Example message'
}
}
使用外部Nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 关闭内置nginx
nginx['enable'] = false
# 配置访问用户.Debian/Ubuntu the default user is www-data for both Apache/Nginx whereas for RHEL/CentOS the Nginx user is nginx.
web_server['external_users'] = ['www-data','nginx']
# 配置nginx服务器ip白名单
gitlab_rails['trusted_proxies'] = ['192.168.1.0/24', '192.168.2.1', '2001:0db8::/32' ]
# Define the external url
external_url 'http://git.example.com'
# Disable the built-in nginx
nginx['enable'] = false
# Disable the built-in unicorn
unicorn['enable'] = false
# Set the internal API URL
gitlab_rails['internal_api_url'] = 'http://git.example.com'
# Define the web server process user (ubuntu/nginx)
web_server['external_users'] = ['www-data']

配置Git Repository数据存储路径

Git repository数据存储的默认路径是:/var/opt/gitlab/git-data.

注:如果采用了docker镜像方式,在启动时已经指定了宿主机的目录/data/gitlab/data,所以下面的配置可以不用.

1
2
3
4
5
# default修改存储目录,alternative增加一个存储目录
git_data_dirs({
"default" => { "path" => "/var/opt/gitlab/git-data" },
"alternative" => { "path" => "/mnt/nas/git-data" }
})

GitLab重新加载配置

对配置的所有修改,都需要重载配置并重启.

1
2
$ gitlab-ctl reconfigure
$ gitlab-ctl restart

Example

image-20181014223921033

Environment

  • mac 10.13.6, gitlab-ce 11.3.5-ce.0, docker 18.03.1-ce

Set configuration

Set Store path、Suport https、Set emial

  • 配置文件路径:/Users/jilingjun/Applications/data/gitlab/config

    • ssl文件路径:/Users/jilingjun/Applications/data/gitlab/config/ssl
  • 数据存储路径:/Users/jilingjun/Applications/data/gitlab/data

  • 日志存储路径:/Users/jilingjun/Applications/data/log/gitlab

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$ vim /Users/jilingjun/Applications/data/gitlab/config/gitlab.rb
# set access url
external_url "http://gitlab.example.com"
external_url "https://gitlab.example.com"
# set ssh port
gitlab_rails['gitlab_shell_ssh_port'] = 9022
# compatable http
nginx['redirect_http_to_https'] = true
# set certs
nginx['ssl_certificate'] = "/etc/gitlab/ssl/gitlab.example.com.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/gitlab.example.com.key"
# set errer page info
nginx['custom_error_pages'] = {
'404' => {
'title' => 'Example title',
'header' => 'Example header',
'message' => 'Example message'
}
}
# set email config.Here use aliyun enterprise mail.
gitlab_rails['gitlab_email_from'] = "username@company.com"
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.mxhichina.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "username@company.com"
gitlab_rails['smtp_password'] = "password"
gitlab_rails['smtp_domain'] = "mxhichina.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true

Generator And Use self certificate

参考之前写的《生成自签名根证书》

RUN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ docker run -d \
--hostname 192.168.1.222 \
--publish 443:443 --publish 80:80 --publish 22:22 \
--name gitlab \
--restart always \
--volume /data/gitlab/config:/etc/gitlab \
--volume /data/gitlab/logs:/var/log/gitlab \
--volume /data/gitlab/data:/var/opt/gitlab \
--network host \
jilingjun1014/gitlab-zh:v11.3.5

# See gitlab container logs
$ docker logs -f gitlab

# Update Permission
$ docker exec gitlab update-permissions
$ docker restart gitlab

# See gitlab container logs
$ docker logs -f gitlab

Reference

GitLab Docker Images

Gitlab汉化

坚持原创技术分享,您的支持将鼓励我继续创作!
Fork me on GitHub