GitLab代码管理平台部署及使用;


一、前置条件

1.yum安装依赖包

[root@localhost ~]# yum install -y curl policycoreutils policycoreutils-python-utils openssh-server openssh-clients

2.开启sshd和邮件服务

[root@localhost ~]# systemctl enable sshd
[root@localhost ~]# systemctl start sshd

[root@localhost ~]# yum install -y postfix
[root@localhost ~]# systemctl start postfix
[root@localhost ~]# systemctl enable postfix

二、部署gitlab

gitlab-ce的rpm包清华源地址

本篇使用版本:
gitlab-ce-14.9.5-ce.0.el7.x86_64.rpm

1.下载gitlab-ce的rpm软件包

[root@localhost ~]# wget -c https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-14.9.5-ce.0.el7.x86_64.rpm

2.rpm安装gitlab

[root@localhost ~]# rpm -ivh gitlab-ce-14.9.5-ce.0.el7.x86_64.rpm --nodeps --force

如下即安装成功:
在这里插入图片描述

3.修改gitlab配置

[root@localhost ~]# cp /etc/gitlab/gitlab.rb /etc/gitlab/gitlab.rb.default
[root@localhost ~]# vim /etc/gitlab/gitlab.rb
#配置用户访问gitlab的访问/下载代码地址
external_url 'http://gitlab.wonderlink.cc'
#修改存放gitlab的数据目录(去掉注释)
#默认数据目录为/var/opt/gitlab/git-data/repositories
git_data_dirs({
  "default" => {
    "path" => "/hqtbj/hqtwww/gitlab-data"
   }
})

4.重载gitlab配置

[root@localhost ~]# gitlab-ctl reconfigure
[root@localhost ~]# gitlab-ctl restart

5.访问gitlab
默认用户:root
密码:需要查看/etc/gitlab/initial_root_password文件

[root@localhost ~]# cat /etc/gitlab/initial_root_password 
...
Password: EzGKd7xxxxxxxxxxxxxxxxxxx5xQ=
# NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.

![在这里插入图片描述](https://img-blog.csdnimg.cn/87ea769c780d4f0fa2e4f53eac0278cc.png

三、gitlab设置中文

gitlab界⾯⿏标依次点击如下:
 (1)用户头像;
 (2)preferences;
 (3)Language
 (4)Chinese
 (5)save changes(保存);

在这里插入图片描述
在这里插入图片描述

四、gitlab集成邮箱配置

因为gitlab在创建用户/重置用户密码时会给用户发送邮件等场景,所以需要配置gitlab集成邮箱进行使用;
1.安装并开启邮件服务(如过在第一步的时候执行过了的话就可以跳过这步)

[root@localhost ~]# yum install -y postfix
[root@localhost ~]# systemctl start postfix
[root@localhost ~]# systemctl enable postfix

2.修改gitlab的邮件配置

[root@localhost ~]# vim /etc/gitlab/gitlab.rb
#开启邮件配置
gitlab_rails['smtp_enable'] = true
#修改为对应的邮箱服务器域名(我这里的是腾讯企业邮箱)
gitlab_rails['smtp_address'] = "smtp.exmail.qq.com"
#对应的端口协议
gitlab_rails['smtp_port'] = 465
#登录邮箱的用户名
gitlab_rails['smtp_user_name'] = "fandxxxi@cxxxe.com"
#登录邮箱的密码(其他的邮箱为授权码,例如QQ、163邮箱)
gitlab_rails['smtp_password'] = "123456xxx"
#修改为对应的邮箱服务器域名
gitlab_rails['smtp_domain'] = "smtp.exmail.qq.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['smtp_pool'] = false
#gitlab发送邮件的发送人
gitlab_rails['gitlab_email_from'] = 'fandxxxi@cxxxe.com'

3.重载gitlab配置使其生效

[root@localhost ~]# gitlab-ctl reconfigure

4.测试发送邮件
首先gitlab-rails console -e production进入gitlab控制台
然后输入Notify.test_email('fandaoshuai@cngotone.com', '邮件主题', '邮件内容test').deliver_now进行测试;

#登录gitlab控制台
[root@localhost ~]# gitlab-rails console -e production
#测试邮件发送
Notify.test_email('fandxxxi@cxxxe.com', '邮件主题', '邮件内容test').deliver_now

在这里插入图片描述
然后去相应收件人的邮箱里查看是否有测试邮件发送过来
在这里插入图片描述
到这里gitalb与邮件的集成就成功了,这时就可以实现gitlab自动发送邮件了;可以新建个用户测试

5.新建个测试用户配置上邮件看是否收到重置密码的邮件
在这里插入图片描述

去相应的用户邮箱里查看
在这里插入图片描述
用户点击"Click here to set your password"链接即可修改自己的密码;
在这里插入图片描述

五、gitlab使用外置nginx代理

应用内网IP外网IP
gitlab10.8.0.9
nginx10.8.0.243.143.244.141

1.修改gitlab配置

[root@localhost ~]# vim /etc/gitlab/gitlab.rb
#gitlab 的域名
external_url 'http://gitlab.wonderlink.cc'
#gitlab 监听的端口
nginx['listen_port'] = 8888

2.yum快速部署一个nginx

[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum -y install nginx

3.添加 nginx 代理服务器配置

[root@k8s-master ~]# vim /etc/nginx/proxy-gitlab.conf
upstream git{
    #域名:gitlab中配置的external_url
    #端口:gitlab中配置的nginx['listen_port']
    #server 域名:端口
    server gitlab.wonderlink.cc:8888;
}

server {
        listen  80;
        #用户最终访问的gitlab地址
        server_name  gitlab.wonderlink.cc;
        location / {
            #这个大小的设置非常重要,如果 git 版本库里面有大文件,设置的太小,文件push 会失败,根据情况调整
            client_max_body_size 50m;
            proxy_redirect off;
            #以下确保 gitlab中项目的 url 是域名而不是 http://git,不可缺少
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            # 反向代理到 gitlab 内置的 nginx 
            #proxy_pass http://10.8.0.9:8888;
            proxy_pass http://git;
            index index.html index.htm;
        }
}
[root@k8s-master ~]# vim /etc/hosts
#在最后一行手动添加gitlab的地址解析
10.8.0.9 gitlab.wonderlink.cc
[root@k8s-master ~]# nginx  -t
[root@k8s-master ~]# nginx -s reload

到这里可以看出我gitlab的external_url地址和nginx的server_name地址是一样的,这样是方便后面给gitlab添加https;如果不需要添加https的话,可以设置为不同的,最终用户访问gitlab使用nginx设置的即可;

4.设置外网域名解析尝试访问
这一步是将我的gitlab.wonderlink.cc域名映射到这台nginx公网IP上实现外网访问,我这里使用阿里云做的解析;
在这里插入图片描述

再次尝试访问:
在这里插入图片描述
在这里插入图片描述
使用nginx反向代理成功;

六、gitlab添加https

1.申请gitlab域名(gitlab.wonderlink.cc)的证书
我这里是在的阿里申请的免费证书
在这里插入图片描述
2.在gitlab处添加自己的https证书

#将申请好的证书放在ssl目录下,需要自己创建
[root@localhost ~]# mkdir /etc/gitlab/ssl
[root@localhost ~]# ll /etc/gitlab/ssl/
总用量 16
-rw-r--r--. 1 root root 1679 315 2023 9490454_gitlab.wonderlink.cc.key
-rw-r--r--. 1 root root 4130 315 10:44 9490454_gitlab.wonderlink.cc_nginx.zip
-rw-r--r--. 1 root root 3818 315 2023 9490454_gitlab.wonderlink.cc.pem

[root@localhost ~]# vim /etc/gitlab/gitlab.rb
#给gitlab的访问地址添加上https,一定要添加!!
external_url 'https://gitlab.wonderlink.cc'
#gitlab开启https时会自动配置,但是只有三个月的使用期限,使用自己的证书的话进行如下设置
letsencrypt['enable'] = false
#在/etc/gitlab/gitlab.rb禁用自动更新。否则,gitlab-ctl reconfigure 可能会尝试更新证书,从而覆盖它们。如果gitlab自签的证书过期,替换的一定要禁用自动更新
letsencrypt['auto_renew'] = false
#设置为true,代表不使用http,而使用https
nginx['redirect_http_to_https'] = true
#放置对应证书的密钥即可
nginx['ssl_certificate'] = "/etc/gitlab/ssl/9490454_gitlab.wonderlink.cc.pem"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/9490454_gitlab.wonderlink.cc.key"

[root@localhost ~]# gitlab-ctl reconfigure
[root@localhost ~]# gitlab-ctl restart

3.给外置反向代理nginx配置gitlab的https

[root@k8s-master gitlab]# pwd 
/etc/nginx/conf.d/cert/gitlab
[root@k8s-master gitlab]# ll 
total 16
-rw-r--r-- 1 root root 1679 Mar 15 22:44 9490454_gitlab.wonderlink.cc.key
-rw-r--r-- 1 root root 4130 Mar 15 22:44 9490454_gitlab.wonderlink.cc_nginx.zip
-rw-r--r-- 1 root root 3818 Mar 15 22:44 9490454_gitlab.wonderlink.cc.pem

[root@k8s-master ~]# vim /etc/nginx/proxy-gitlab.conf
upstream git{
    server gitlab.wonderlink.cc:8888;
}

server {
        listen  80;
        server_name  gitlab.wonderlink.cc;
        rewrite ^(.*)$  https://$host$1 permanent;
}

server {
        listen  443 ssl;
        server_name gitlab.wonderlink.cc;
        location / {
            #这个大小的设置非常重要,如果 git 版本库里面有大文件,设置的太小,文件push 会失败,根据情况调整
            client_max_body_size 50m;
            proxy_redirect off;
            #以下确保 gitlab中项目的 url 是域名而不是 http://git,不可缺少
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            # 反向代理到 gitlab 内置的 nginx
            proxy_pass https://git;
            index index.html index.htm;
        }

        ssl_certificate /证书所在目录/9490454_gitlab.wonderlink.cc.pem;
        ssl_certificate_key /证书所在目录/9490454_gitlab.wonderlink.cc.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;
}

访问如下
在这里插入图片描述
注:在这里给大家说下为什么gitlab自身和反向代理nginx都要设置https,只在nginx上配置不可以吗?答案是不可以,如果只在nginx处配置https的话在gitlab界面clone代码时甚至时会没有https,如下图:
个别情况是显示https,但是点击右边的复制再粘贴至其他位置时会自动变成http;
在这里插入图片描述

七、gitlab修改主分支main为master

gitlab界⾯⿏标依次点击如下:
 (1)用户头像;
 (2)菜单;
 (3)管理员;
 (4)设置;
 (5)仓库;
 (6)默认分支修改为master即可;

在这里插入图片描述

八、gitlab集成ldap

gitlab自身支持使用ldap登录,只需要配置下即可,关于gitlab集成ldap的配置,会在后面的OpenLdap帖子里写出;



Git常用命令补充

git init #初始化本地仓库
git remote add origin http://gitlab.xxx.cc/root/ceshi2.git #从远程分支获取仓库
git fetch   #获取远程所有分支
git checkout -b master origin/master  #创建本地分支并关联远程的分支
git add a.txt #添加文件到本地仓库
git commit a.txt -m '注释' #提交到本地仓库
git push  #推送到相同名字的远程分支
git push -uf origin dev #推送到远程指定分支
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不知名运维:

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值