一键安装 gitlab并设置邮件发送(简单教程)

安装 GitLab

如果是Ubuntu、Debian等按官方的安装方法:
如果是deepin等linux,可以下载Debian的安装包安装:github仓库
官方文档:https://about.gitlab.com/installation/#centos-7
官网的是企业版(收费),这里安装的是社区版

yum install -y curl policycoreutils-python openssh-server
systemctl enable sshd
systemctl start sshd
firewall-cmd --permanent --add-service=http
systemctl reload firewalld

# 安装邮件服务,有其他方案的也可以不安装
yum install -y postfix
systemctl enable postfix
systemctl start postfix

# 最新版
curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | bash

EXTERNAL_URL="http://gitlab.example.com" yum install -y gitlab-ce

# 这一步安装,国内主机超级慢,可以在境外主机下载包再安装
打开:https://packages.gitlab.com/gitlab/gitlab-ce,选择包,点开,进入新页面,可以看到Download the package;下载回来用yum install ./包名   来安装。


# 对GitLab进行重配置(这一步会生成配置并启动 GitLab,端口冲突启动失败按下文改端口)
gitlab-ctl reconfigure
启动后浏览器打开,第一次访问会提示你修改密码;修改后root加密码登录


# 修改端口,不用修改端口的话就完事了。
# 默认会占用80、8080这两个常用端口,这里我把8080改为8083;80改为8066

vim /etc/gitlab/gitlab.rb

## Advanced settings
unicorn['listen'] = '127.0.0.1'
unicorn['port'] = 8083   #不修改8080端口的这里不用改

# 更改nginx监听端口为8066
nginx['listen_addresses'] = ['*', '[::]']
nginx['listen_port'] = 8066  # nginx['listen_port'] = nil 把nil改为8066
# override only if you use a reverse proxy: https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/settings/nginx.md#setting-the-nginx-listen-port

#不修改8080端口的这里不用改
vim /var/opt/gitlab/gitlab-rails/etc/unicorn.rb
listen "127.0.0.1:8083", :tcp_nopush => true
listen "/var/opt/gitlab/gitlab-rails/sockets/gitlab.socket", :backlog => 1024



vim /var/opt/gitlab/nginx/conf/gitlab-http.conf
server {
  listen *:8066;    #把80改为8066


vim /var/opt/gitlab/gitlab-shell/config.yml
#这个端口把8080改为8083,不改提交代码会报错。
gitlab_url: "http://127.0.0.1:8083"


# 重新配置gitlab(更新配置)
# gitlab-ctl reconfigure第二次使用(第一次是安装后)必须在“部分服务”启动的状态下才可以执行成功。
gitlab-ctl reconfigure

等两分钟后看服务状态:gitlab-ctl status

修改了配置文件一定要执行以下两个步骤:
gitlab-ctl reconfigure
gitlab-ctl restart


# 升级
更新之前进行备份,默认备份位置:/var/opt/gitlab/backups
gitlab-rake gitlab:backup:create STRATEGY = copy
更新到最新版本的GitLab。
yum install -y gitlab-ce

参考:
https://www.jianshu.com/p/a9bb06a48b25
http://blog.csdn.net/arybd/article/details/54635295
https://www.cnblogs.com/Gyoung/p/7729095.html
https://www.cnblogs.com/wenwei-blog/p/5861450.html


以下是老版本的安装方法了,新版本更简单了。

GitLab 下载:http://www.gitlab.cc/downloads/
GitLab 中文文档:http://doc.gitlab.cc
安装方式官方有详细说明,这里使用Centos 6安装,其他系统参考官方文档。

#切换到root用户登录
yum -y install curl openssh-server openssh-clients postfix cronie
service postfix start
chkconfig postfix on

#这句是用来做防火墙的。避免用户通过ssh方式和http来访问
sudo lokkit -s http -s ssh

添加GitLab仓库,并安装到服务器上

第一步:
curl -sS http://packages.gitlab.cc/install/gitlab-ce/script.rpm.sh | sudo bash
yum -y install gitlab-ce

或者使用国内的镜像源:
vim /etc/yum.repos.d/gitlab-ce.repo
[gitlab-ce]
name=gitlab-ce
baseurl=http://mirrors.lifetoy.org/gitlab-ce/yum/el6
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key

yum -y install gitlab-ce

或者使用
vim /etc/yum.repos.d/gitlab-ce.repo
[gitlab-ce]
name=gitlab-ce
baseurl=http://mirrors.lifetoy.org/gitlab-ce/yum/el6/
repo_gpgcheck=0
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key

yum -y install gitlab-ce

-----------------------------------------------------------------------------------------

第二步:
rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el6/gitlab-ce-8.9.6-ce.0.el6.x86_64.rpm
#截至到7.16为止,8.9.6版本还没有中文汉化版

或者
rpm -ivh http://mirrors.lifetoy.org/gitlab-ce/yum/el6/gitlab-ce-8.8.7-ce.1.el6.x86_64.rpm

查看gitlab-ce安装的软件包:rpm -ql gitlab-ce |more

启动gitLab

gitlab-ctl reconfigure第二次使用(第一次是安装后)必须在“部分服务”启动的状态下才可以执行成功。
gitlab-ctl start


启用HTTPS(Let's Encrypt)

在宝塔或Nginx中新建站点,绑定域名,申请证书,开启强制SSL;再设置反代:127.0.0.1:8099,传递域名,如:gitlab.leolan.top

vim /etc/gitlab/gitlab.rb
# 修改为如下,注意,一定要为http,如果写了https会开启证书,导致配置出错。
external_url 'http://gitlab.leolan.top'

gitlab-ctl reconfigure
执行完之后使用gitlab.leolan.top就能访问了,虽然在gitlab中切换到http协议显示的是http,也是可以使用https的,不影响。

Gitlab官网新版本(10.5以上)直接可以在Gitlab配置文件中配置Let's Encrypt证书,详见:https://about.gitlab.com/2018/02/22/gitlab-10-5-released/


使用mysql数据库代替gitlab-ce

添加yum仓库源

wget -P /etc/yum.repos.d  http://mirrors.aliyun.com/repo/Centos-6.repo  #Centos7 源不一样,官网找。
yum -y install mysql mysql-server mysql-devel gcc

初始化mysql

设定root用户密码为:12345gitlab

/etc/init.d/mysqld start
/usr/bin/mysqladmin -u root password '12345gitlab'

创建gitlab相关的数据库

创建gitlab用户,并设定密码为:gitlab12345aa

mysql -uroot -p'12345gitlab'
create user 'gitlab'@'%' identified by 'gitlab12345aa';
set storage_engine=innodb;
create database if not exists `gitlabhq_production` default character set `utf8` collate `utf8_unicode_ci`;
grant select,insert,update,delete,create,create temporary tables,drop,index,alter,lock tables on `gitlabhq_production`.* to 'gitlab'@'%';

删除用户名为空的主机,并刷新mysql权限

mysql -uroot -p'12345gitlab' mysql -e "delete from user where user='';flush privileges;"

测试gitlab用户连接数据库

mysql -h127.0.0.1 -ugitlab -p'gitlab12345aa'

gitlab配置文件:vim /etc/gitlab/gitlab.rb

gitlab_rails['db_adapter'] = "mysql2"
gitlab_rails['db_encoding'] = "utf8"
gitlab_rails['db_database'] = "gitlabhq_production"
gitlab_rails['db_pool'] = 10
gitlab_rails['db_username'] = "gitlab"
gitlab_rails['db_password'] = "**gitlab12345aa**"
gitlab_rails['db_host'] = "127.0.0.1"
gitlab_rails['db_port'] = 3306
postgresql['enable'] = false


vim /opt/gitlab/embedded/service/gitlab-rails/.bundle/config
BUNDLE_RETRY: 5
BUNDLE_PATH: "/opt/gitlab/embedded/service/gem"
BUNDLE_JOBS: 9
`BUNDLE_WITHOUT: development:test:postgres`
BUNDLE_DISABLE_SHARED_GEMS: '1'


cd /opt/gitlab/embedded/service/gitlab-rails
/opt/gitlab/embedded/bin/bundle install
gitlab-ctl reconfigure
gitlab-ctl restart
gitlab-rake gitlab:setup

到此gitlab-ce更换成mysql完成。

打开http://IP/ 即可访问,首次访问GitLab,系统会让你重新设置管理员的密码,设置成功后会返回登录界面.
默认的管理员账号是root,如果你想更改默认管理员账号,请输入上面设置的新密码登录系统后修改帐号名


修改gitlab通过域名访问

vim /etc/gitlab/gitlab.rb
external_url 'http://your.domain'


重启配置
sudo gitlab-ctl reconfigure

实时查看所有执行日志
gitlab-ctl tail

查看版本:cat /opt/gitlab/embedded/service/gitlab-rails/VERSION

/var/opt/gitlab/gitlab-workhorse
/opt/gitlab/embedded/service/gitlab-rails/public
/opt/gitlab/etc/gitlab-workhorse

/var/log/gitlab/gitlab-workhorse

配置文件路径:
/var/opt/gitlab/nginx
/var/opt/gitlab/nginx/conf/nginx.conf
/var/opt/gitlab/nginx/conf/gitlab-http.conf

汉化

法一(适用于9.0以上的新版本)

git clone https://gitlab.com/xhang/gitlab.git
gitlab-ctl stop
\cp -rf ./gitlab/* /opt/gitlab/embedded/service/gitlab-rails/   #加\不使用alies

以下两个报错是正常的
cp: cannot overwrite non-directory ‘/opt/gitlab/embedded/service/gitlab-rails/log’ with directory ‘./gitlab/log’
cp: cannot overwrite non-directory ‘/opt/gitlab/embedded/service/gitlab-rails/tmp’ with directory ‘./gitlab/tmp’


# 重新加载配置,并启动
gitlab-ctl reconfigure
gitlab-ctl restart

参考:http://blog.51cto.com/passed/2073623


法二(适用于9.0以下的版本)

中文社区:https://gitlab.com/larryli/gitlab
确认gitlab版本与当前汉化版本的 VERSION 是否相同,当前最新的汉化版本为 8.13.5 。
如果安装版本小于当前汉化版本,请先升级。如果安装版本大于当前汉化版本,请在项目中提交新的 issue。
如果版本相同,首先在本地clone仓库。

下载汉化补丁包

  • 1.先停止gitlab
    gitlab-ctl stop
  • 2.克隆gitLab.com仓库,有中文补丁
    git clone https://gitlab.com/xhang/gitlab.git #注意对应的版本地址不同

或gitcafe.com镜像,速度更快,暂时不支持中文补丁
git clone https://git.oschina.net/qiai365/gitlab-ce.git
或者
git clone https://git.oschina.net/johnwu/gitlab-zh.git

cd gitlab
git branch -a |tac


8.8版本的汉化补丁(8-8-stable是英文稳定版,8-8-zh是中文版,两个diff结果便是汉化补丁)
git diff origin/8-8-stable origin/8-8-zh > /tmp/8.8.diff

应用汉化补丁

cd /opt/gitlab/embedded/service/gitlab-rails
git apply /tmp/8.8.diff

启动gitlab:gitlab-ctl start


指定代码库路径(修改代码版本库位置)

  • 1.设置存放代码库的主目录:
vim /etc/gitlab/gitlab.rb
# 默认是在/var/opt/gitlab/git-data下
git_data_dir "/var/opt/gitlab/git-data"
  • 2.访问刚搭建的GitLab站点,创建一个group,比如devusers
    这时会创建**/var/opt/gitlab/git-data/devusers文件夹。
    然后在
    /var/opt/gitlab/git-data/repositories/创建一个文件夹,比如devusers**

  • 3.将现有的所有git项目文件复制到这个文件夹
    cp -r /data/git/* /var/opt/gitlab/git-data/devusers

  • 4.修改一下复制过来的文件夹的所有者:
    chown -R git:git /var/opt/gitlab/git-data/devusers

  • 5.导入项目

cd /var/opt/gitlab
gitlab-rake gitlab:import:repos

持续构建

Runners

通过每次提交代码时gitlab-ci检测到push之后,把要执行的操作转交给gitlab runners去执行(编译、打包、发布等动作)。

Docker版:https://www.leolan.top/index.php/posts/222.html

# 安装runners,注意gitlab的版本要和runners的版本基本搭配,跨太大的版本无法成功添加runners。
最新版:https://docs.gitlab.com/runner/install/

#####################################################################
9.0以下版本(比如宝塔面板的gitlab):
# For Debian/Ubuntu
wget --content-disposition https://packages.gitlab.com/runner/gitlab-ci-multi-runner/packages/ubuntu/precise/gitlab-ci-multi-runner_1.10.7_amd64.deb/download.deb
dpkg -i gitlab-ci-multi-runner_1.10.7_amd64.deb

# For CentOS
wget https://image.leolan.top/gitlab-ci-multi-runner-1.10.7-1.x86_64.rpm
或者(以下方法特别慢)
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | bash
yum install gitlab-ci-multi-runner-1.10.7-1.x86_64.rpm

# 配置
安装好 GitLab Runner 之后,我们只要启动 Runner 然后和 CI 绑定就可以了:
打开你 GitLab 中的项目设置页面,在项目设置中找到 runners
运行 gitlab-ci-multi-runner register
1.输入 CI URL
2.输入 Token(低版本的有下划线,下划线后面的也要输)
3.输入 Runner 的名字(随便输)
4.输入 gitlab-ci 的标签(随便输)
5.是否允许于未标记的版本?(指没有打标签的版本,不选true的话要触发构建必须要打标签)
输入:true
6.是否将Runner锁定到当前项目?(锁定后只能在一个项目中适用这个Runner)
输入:false
7.选择 Runner 的类型(执行程序),简单起见还是选 Shell 吧。
完成

#####################################################################
9.0以上版本(也适用最新版本):
官方地址:https://docs.gitlab.com/runner/install/linux-manually.html
# Ubuntu最新版
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | bash
apt-get install gitlab-ci-multi-runner

# Linux x86-64
wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

# Linux x86
wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386

# Linux arm
wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm

# Docker
curl -sSL https://get.docker.com/ | sh


chmod +x /usr/local/bin/gitlab-runner
useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
gitlab-runner start

# 配置
安装好 GitLab Runner 之后,我们只要启动 Runner 然后和 CI 绑定就可以了:
打开你 GitLab 中的项目设置页面,在项目设置中找到 runners
运行 gitlab--runner register
1.输入 CI URL
2.输入 Token(低版本的有下划线,下划线后面的也要输)
3.输入 Runner 的名字(随便输)
4.输入 gitlab-ci 的标签(随便输)
5.是否允许于未标记的版本?(指没有打标签的版本,不选true的话要触发构建必须要打标签)
输入:true
6.是否将Runner锁定到当前项目?(锁定后只能在一个项目中适用这个Runner)
输入:false
7.选择 Runner 的类型(执行程序),简单起见还是选 Shell 吧。
完成
#####################################################################

# 查看运行状态
gitlab-runner list

# 编写配置文件,具体看参考链接解释
进入项目目录,可以是多条命令,但我这边为了方便用shell脚本
vim .gitlab-ci.yml

stages:                                       #场景
  - build
  - build_live

Build_Test:                                   #构建任务名
    stage: build
    script:
      - echo "开始更新"
      - expect build.sh                       #脚本放在项目目录下,这个脚本用expect的方式执行。
      - /home/gitlab-runner/xxxxx-test.sh     #这里注意,脚本只能放在/home/gitlab-runner/下,并注意所属组和所有者,否则没有权限执行。
      - echo "更新完成"
    only:
      - dev                                   #设置dev分支提交时才触发执行

Build_Live:
    stage: build_live
    script:
      - echo "开始更新"
      - expect build_live.sh                  #脚本放在项目目录下,这个脚本用expect的方式执行。
      - /home/gitlab-runner/xxxxx-live.sh     #这里注意,脚本只能放在/home/gitlab-runner/下,并注意所属组和所有者,否则没有权限执行。
      - echo "更新完成"
    only:
      - master                                #设置master分支提交时才触发执行

##################################################################################
#这里改用了expect进行交互命令自动应答;注意:runners在本机,所有其他电脑push之后,会在本机执行代码,所有会有登陆远程主机的情况。
# 如果构建也是在本机则切换到root,因为build.sh是用gitlab-runner用户运行的,没有权限。
vim build.sh

#!/usr/bin/expect

set timeout 30
spawn ssh root@192.168.10.18 -p 2288 -i /home/gitlab-runner/xxxxxx02.pem
# xxxxxx02.pem文件是放在gitlab-runner所在主机的/home/gitlab-runner/目录下,权限700,用户组、所属用户都是:gitlab-runner
# 如果是在本机构建,这切换到root,上面这句改为:【spawn su root】并删除下面的yes/no这一项。
# 以下是例子,按实际情况修改。
expect "Password:"
send "root密码\r"

expect {
"*yes/no" { send "yes\r"; exp_continue }
}
expect "*#"
send "cd /www/wwwroot/testweb\r"
expect "*#"
send "git fetch --all\r"
expect "*#"
send "git reset --hard origin/master\r"
expect "*#"
# 注意用awk时$要转意;命令内不要有"",expect脚本会把双引号内的内容识别为字符串。
send "kill -9 `ps aux | grep nuxt-test | grep opt | awk '{print \$2}'`\r"
expect "*#"
send "npm run build\r"
expect "*#"
sleep 1            #某些框架在运行run build时会卡一下,不设置sleep会直接多条命令连在一起,导致命令没有被执行。
expect "*#"
send "chown -R gitlab-runner:gitlab-runner .\r"
expect "*#"
sleep 1
expect "*#"
send "exit\r"


chmod +x build.sh

# 配置完成,把本地的代码提交到gitlab就行了,gitlab检测到.gitlab-ci.yml后就会发送到runners上执行;然后登陆远程主机执行构建任务。

参考:
https://segmentfault.com/a/1190000013362589
https://segmentfault.com/a/1190000006120164
https://www.jianshu.com/p/df433633816b
http://blog.csdn.net/lusyoe/article/details/52714121
https://www.cnblogs.com/restful/p/6711063.html


Web Hook

参考:
gitlab与jenkins集成
https://segmentfault.com/a/1190000007671099
https://www.jianshu.com/p/935409ce4c9a


修改项目地址(创建项目后地址不是正确的)

一般在docker中运行就会出现这种情况,创建项目后发现项目地址是主机名,而不是ip地址,由于在docker容器里面,只能更改为ip才能正常工作。

vim /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml
# 修改 host:后的值为ip
gitlab-ctl restart

邮件发送配置

官网配置说明:支持多家企业邮件
在配置文件中新增邮件配置代码,保存后运行gitlab-ctl reconfigure
https://docs.gitlab.com/omnibus/settings/smtp.html


备份恢复

# 手动备份
目录:/var/opt/gitlab/backups
配置文件:/etc/gitlab/gitlab.rb
nginx配置文件:/var/opt/gitlab/nginx/conf
邮件配置:/etc/postfix/main.cf
数据备份:gitlab-rake gitlab:backup:create

# 自动备份、定时自动备份(crontab)
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1 

# Gitlab备份目录
当然你也可以通过/etc/gitlab/gitlab.rb配置文件来修改默认存放备份文件的目录
gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"
gitlab-ctl reconfigure   #重载配置


# 设置备份过期时间
设置只保存最近7天的备份,编辑 /etc/gitlab/gitlab.rb 配置文件,单位为秒
gitlab_rails['backup_keep_time'] = 604800
gitlab-ctl reconfigure   #重载配置

参考:https://blog.csdn.net/ouyang_peng/article/details/77070977

文章作者: Leo
本文链接:
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 LeoLan的小站
环境搭建 持续构建 gitlab
喜欢就支持一下吧