之前梳理了一篇Gitlab的安装CI持续集成系统环境---部署Gitlab环境完整记录,但是这是bitnami一键安装的,版本比较老。下面介绍使用rpm包安装Gitlab,下载地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el6/,针对centos6和centos7的各版本Gitlab下载。如果下载不下来或者下载巨慢,可以尝试:清华大学镜像
一、下面记录centos6.9系统下的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
|
1)配置系统防火墙,把HTTP和SSH端口开放(关闭iptables或者开放ssh).
[[email protected] ~]# /etc/init.d/iptables stop
[[email protected] ~]# yum install curl openssh-server postfix cronie
[[email protected] ~]# service postfix start
[[email protected] ~]# chkconfig postfix on
[[email protected] ~]# lokkit -s http -s ssh //如果iptables关闭了,这条命令就无需执行了。这条命令是用来设置防火墙的,开放http和ssh访问端口
2)下载gitlab的rpm安装包已提前下载放到百度云里:http://pan.baidu.com/s/1c2EPRLQ
提前密码:qys2[[email protected] ~]# rpm -ivh gitlab-ce-9.4.5-ce.0.el6.x86_64.rpm --force
安装后的gitlab默认路径是/opt/gitlab(程序路径)、 /var/opt/gitlab(配置文件路径)。
3) 接着进行配置[[email protected] ~]# gitlab-ctl reconfigure
上面配置命令执行后,如没有报错,就说明gitlab配置成功。配置后会生成各应用服务配置文件,放在/opt/gitlab/etc下,日志路径为/var/log/gitlab/
4)然后启动gitlab[[email protected] ~]# gitlab-ctl start
[[email protected] ~]# gitlab-ctl status
5)最后就可以使用http://localhost顺利访问Gitlab了。整个安装过程大概10分钟搞定(rpm包下载比较费时间)
|
二、Gitlba安装后的几个细节的配置
|
1
|
|
Gitlab安装后,http://localhost访问,首次访问的时候,如果不知道管理员账号和密码,尽管可以注册用户,但注册的用户都不是管理员。这个时候,可以重置管理员的密码,管理员默认是root。
重置管理员密码(密码要是8位)的方法如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[[email protected] ~]# gitlab-rails console production
Loading production environment (Rails 4.1.1)irb(main):001:0> user = User.where(id:1).first
irb(main):002:0> user.password='12345678'
irb(main):003:0> user.save!这样,Gitlab管理员的登录权限就是:root/12345678,管理员的默认邮箱是部署机的本机邮箱,也是从本机发的邮件。这也就是为什么在开头要安装postfix。
修改下面几处,否则邮件发出后,点击会报错。下面的192.168.1.24是部署机ip。[[email protected] ~]# cd /opt/gitlab/
[[email protected] gitlab]# cat embedded/service/gitlab-rails/config/gitlab.yml|grep 192.168.1.24
host: 192.168.1.24
email_from: [email protected]
[[email protected] gitlab]# cd /var/opt/gitlab/
[[email protected] gitlab]# cat ./gitlab-rails/etc/gitlab.yml|grep 192.168.1.24
host: 192.168.1.24
email_from: [email protected]
最后重启gitlab-ctl生效[[email protected] gitlab]# gitlab-ctl restart
|
在管理员账号(root)登录后,先把"注册"功能关了,这样就只能在管理员账号下创建用户。关闭注册功能方法:
访问http://192.168.1.24/admin/application_settings,如下:
关闭"Sign-up enabled"功能(特别注意:Sign-in enabled登录功能不要关闭了,看清楚!)
三、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
|
[[email protected] ~]# cat gitlab.sh
#!/bin/bash#批量创建gitlab用户userinfo="userinfo.text"
while read line
do password=`echo $line | awk '{print $1}'`
mail=`echo $line | awk '{print $2}'`
username=`echo $line | awk '{print $3}'`
name=`echo $line | awk '{print $4}'`
curl -d "reset_password=$password&email=$mail&username=$username&name=$name&private_token=ucUctguWU6-2qrvRnGiB" "http://192.168.1.24/api/v4/users"
done <$userinfo
[[email protected] ~]# cat userinfo.text
1 [email protected] zhanjiang.feng zhanjiang.feng1 [email protected] hongkang.yan hongkang.yan1 [email protected] yansong.wang yansong.wang1 [email protected] bo.xue bo.xue1 [email protected] junlong.li junlong.li1 [email protected] luyu.cao luyu.cao1 [email protected] xueqing.wang xueqing.wang1 [email protected] xu.guo xu.guo1 [email protected] bing.xing bing.xing1 [email protected] linan linan |
注意:上面userinfo.txt文件里的四行分别表示密码,邮箱,用户名,别名。上面命令执行后,就可以批量创建用户了!
其中密码用1表示重置密码,也就是用户创建之后,会给用户邮箱发送两封邮件:
-> 一封确认绑定邮箱的邮件,一定要点击这个邮件里的confirm确认地址(否则登录无效);
-> 另一封是重置用户密码的邮件。重置后就可以使用邮箱或用户名登陆了。
注意上面脚本中的private_token(这个很重要,否则批量创建不了用户)的值是从gitlab的管理员账号登录后的"settings-Account"界面里找到的,如下:
访问脚本中gitlab的用户接口地址http://192.168.1.24/api/v4/users,试试能否访问!
-----------------------------------------------------------------------------Email的smtp设置-------------------------------------------------------------------------------------------
|
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
31
32
|
上面默认是用部署机本地的postfix发邮件。如果要想使用第三方邮箱发邮件,这就需要修改/var/opt/gitlab/gitlab-rails/etc/unicorn.rb文件:
[[email protected] ~]# # cat /etc/gitlab/gitlab.rb.bak22|grep -v "^#"|grep -v "^$"
external_url 'http://192.168.1.24'
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.163.com"
gitlab_rails['smtp_port'] = 25
gitlab_rails['smtp_password'] = "*******"
gitlab_rails['smtp_domain'] = "163.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
由于该文件会影响gitlab-ctl指令,如果改动了则需要重新运行配置。注意这个重新配置的动作要在上面细节配置之前,否则上面的配置在reconfigure之后就会被覆盖到默认状态![[email protected] ~]# gitlab-ctl reconfigure
--------------------------------------------------------------------------------------------上面使用的是163邮箱,下面再贴下公司企业邮箱(用的是Coremail论客邮件系统,注意邮箱的smtp地址要正确)的配置:[[email protected] ~]# cat /etc/gitlab/gitlab.rb|grep -v "^#"|grep -v "^$"
external_url 'http://192.168.1.24'
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.icoremail.net"
gitlab_rails['smtp_port'] = 25
gitlab_rails['smtp_domain'] = "icoremail.net"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
|
gitlab-ctl的常用命令:
//启动
gitlab-ctl start
//查看运行状态
gitlab-ctl status
//停止
gitlab-ctl stop
//查看错误信息
gitlab-ctl tail
//保存配置
gitlab-ctl reconfigure