一、gitlab介绍
GitLab是一个开源的、基于Git的版本控制系统。
1. 核心功能
-
代码托管
- gitlab允许用户创建项目,并将代码存储在这些项目中,方便用户上传和下载代码,并支持多种编程语言,无论是个人开发者的小型项目,还是团队协作的大型软件项目,都可以使用gitlab来托管代码。
-
版本控制
- gitlab可以记录代码的每一次更改,用户可以查看提交历史来了解代码的演变过程。
-
持续集成/持续部署(CI/CD)
- gitlab通过CI/CD管道实现自动化构建、测试和部署。当开发正推送代码后,gitlab会自动触发构建和测试流程,测试通过后可以进行部署,可以提高开发效率。
-
项目管理
- gitlab项目管理工具,支持创建任务、分配任务、跟踪任务进度,并可以设置里程碑,帮助团队有效管理项目进度和任务分配。
-
代码审查
- 开发可以通过合并请求提交代码更改,团队其他成员可以对代码进行审查、评论和批准,有助于提高代码质量。
-
安全功能
- gitlab提供代码扫描功能,可以检测代码中的安全漏洞,并支持访问控制,设置不同用户或用户组对项目的访问权限。
2. 适用场景
- 软件开发团队
- 企业内部项目管理
- 开源社区项目
- 个人开发者
- 教育机构
- 小型团队和初创公司
二、gitlab部署
以下是gitlab-18.2.4-ce部署
1. 环境准备
- 安装docker和docker-compose
- 服务器配置最好 2核 4G 以上
2. 部署gitlab
2.1 创建目录
bash
mkdir -p /data/gitlab/config
mkdir -p /data/gitlab/logs
mkdir -p /data/gitlab/data
2.2 编写docker-compose.yml文件
bash
version: '3.8'
services:
gitlab:
image: gitlab/gitlab-ce:18.2.4-ce.0
container_name: gitlab
restart: always
hostname: gitlab.example.com
ports:
- "8080:80" # 给 Nginx 代理
- "8443:443" # 如果想用 GitLab 自带 https,可选
- "2222:22" # ssh 克隆用
volumes:
- /data/gitlab/config:/etc/gitlab
- /data/gitlab/logs:/var/log/gitlab
- /data/gitlab/data:/var/opt/gitlab
2.3 启动gitlab容器
bash
docker-compose up -d
3. 调整gitLab 内部配置
vim /data/gitlab/config/gitlab.rb (容器内部是/etc/gitlab/gitlab.rb)
bash
#指定域名
#注意:为什么这里不写https//gitlab.example.com,如果写了https,gitLab内置nginx和外层nginx冲突,gitLab内部nginx会尝试启用443,而外层Nginx也在管443,结果请求来回绕,容易访问502。
external_url 'http://gitlab.example.com'
# 如果 ssh 用 2222 端口,需要加上:
gitlab_rails['gitlab_shell_ssh_port'] = 2222
#因为用了Nginx 反向代理,那就 关闭 GitLab 的内置证书申请
letsencrypt['enable'] = false
更改完之后重载配置
bash
docker exec -it gitlab gitlab-ctl reconfigure
4. gitlab其他操作
bash
#杀掉进程(如果出现执行重载配置是卡住可选择操作)
docker exec -it gitlab pkill -9 -f "cinc-client"
docker exec -it gitlab gitlab-ctl reconfigure
#查看组件是否正常(各个组件显示run就说明ok)
docker exec -it gitlab gitlab-ctl status
#GitLab 在 容器第一次初始化时,会生成一个随机的 root 密码,默认的登录方式是:
用户名:root
密码:保存在容器挂载的配置目录里(/etc/gitlab/initial_root_password)
#查看宿主机挂载位置
cat /data/gitlab/config/initial_root_password
#会有类似输出(!!!如果更改过root密码就不会更新这个文件了)
# WARNING: This value is valid only in the following conditions
# ...
Password: AbCdEfGhIjKlMnOpQrSt
# ...
#如果密码忘记,可以进入容器里重置
docker exec -it gitlab gitlab-rails console -e production
#在控制台输入
user = User.find_by_username('root')
user.password = '你的新密码'
user.password_confirmation = '你的新密码'
user.save!
三、配置nginx反向代理
1. 安装nginx
bash
#拉取nginx镜像
docker pull nginx:1.26.2
#创建持久化目录
mkdir -p /data/nginx/cert
mkdir -p /data/nginx/conf/conf.d
mkdir -p /data/nginx/html
mkdir -p /data/nginx/logs
2. 创建nginx配置文件
创建基础配置文件nginx.conf
vim /data/nginx/conf/nginx.conf
bash
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
client_max_body_size 200m;
client_body_timeout 300;
include /etc/nginx/conf.d/*.conf;
}
创建gitlab代理nginx配置文件
bash
server {
listen 80;
server_name gitlab.example.com; # 替换为你的域名
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name gitlab.example.com; # 替换为你的域名
ssl_session_timeout 30m;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_certificate /etc/nginx/cert/certificate.pem; # 替换为你的 SSL 证书路径
ssl_certificate_key /etc/nginx/cert/private.key; # 替换为你的 SSL 私钥路径
ssl_session_cache shared:SSL:10m;
location / {
client_max_body_size 256m;
proxy_pass http://localhost:8080; # 替换为 GitLab 容器的 IP 或域名
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_intercept_errors on;
}
# GitLab WebSocket 支持(例如 CI/CD 终端)
location /-/ {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
3. 重启nginx
bash
systemctl restart nginx
4.访问gitlab
bash
https://gitlab.example.com #访问你自己的域名
本文来自博客园,转载请注明原文链接:https://www.cnblogs.com/zhaohaiqi/p/19062035