首先确保系统包为最新版本,避免依赖冲突:
sudo apt update && sudo apt upgrade -y
GitLab需要curl(下载工具)、openssh-server(SSH访问)、ca-certificates(SSL证书)、tzdata(时区设置)等依赖:
sudo apt install -y curl openssh-server ca-certificates tzdata postfix
安装postfix时,选择“Internet Site”类型并设置服务器域名(用于邮件通知)。
通过官方脚本添加GitLab社区版(CE)仓库,确保获取最新版本:
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
使用apt安装GitLab社区版(默认安装最新稳定版):
sudo apt install -y gitlab-ce
编辑GitLab主配置文件,设置外部访问URL(替换为服务器IP或域名):
sudo nano /etc/gitlab/gitlab.rb
找到external_url行,修改为:
external_url 'http://your_server_ip' # 或 'http://your_domain.com'
保存退出后,重新配置GitLab以应用更改:
sudo gitlab-ctl reconfigure
启动GitLab并设置为开机自启:
sudo gitlab-ctl start
sudo systemctl enable gitlab
在浏览器中输入配置的external_url(如http://192.168.1.100),首次访问需设置管理员密码(默认管理员账号为root)。
sudo ufw allow http
sudo ufw allow https
sudo ufw enable
/etc/gitlab/gitlab.rb添加SMTP设置:gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = 'your-email@example.com'
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.example.com"
gitlab_rails['smtp_port'] = 587
gitlab_rails['smtp_user_name'] = "your-email@example.com"
gitlab_rails['smtp_password'] = "your-email-password"
gitlab_rails['smtp_domain'] = "example.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
保存后重新配置:sudo gitlab-ctl reconfigure
/etc/gitlab/gitlab.rb:letsencrypt['enable'] = true
letsencrypt['auto_renew'] = true
letsencrypt['contact_emails'] = ['your-email@example.com']
重新配置后,GitLab会自动申请并配置SSL证书。.gitlab-ci.yml文件,定义构建、测试、部署流程(如示例):stages:
- build
- test
- deploy
build:
stage: build
script:
- echo "Building the project..."
test:
stage: test
script:
- echo "Running tests..."
deploy:
stage: deploy
script:
- echo "Deploying the project..."
提交代码后,GitLab会自动触发CI/CD流水线。通过以上步骤,即可在Debian系统上完成GitLab开发环境的搭建,并支持代码托管、协作、CI/CD等核心功能。