ubuntu部署自己的gitlab
安装
首先在在官网下载对应版本的安装包。
然后安装必要的依赖。
sudo apt update
sudo apt upgrade
sudo apt install -y curl openssh-server ca-certificates postfix
注意: 在安装 postfix 的过程中,可能会弹出一个配置界面。
- 选择 "Internet Site",然后回车。
- 将 "mail name" 设置为您服务器的外部 DNS 域名或 IP 地址,然后回车。如果还有其他配置提示,通常接受默认值即可。
如果你在安装postfix时忘记配置,或者想重新配置,执行 sudo dpkg-reconfigure postfix
依赖装好后,通过 dkpg -i 安装包安装gitlab。
修改配置
安装完后即可通过127.0.0.1访问页面,初始账号为root,密码通过 sudo cat /etc/gitlab/initial_root_password命令可以获得。
登录之后,去用户->edit_profile->password修改密码。
然后进行一些必要配置。
配置文件通过下面命令进行修改:sudo vim /etc/gitlab/gitlab.rb
修改后,执行 sudo gitlab-ctl reconfigure即可重新加载。
下面命令用于服务管理:
sudo gitlab-ctl start # 启动所有 GitLab 组件
sudo gitlab-ctl stop # 停止所有 GitLab 组件
sudo gitlab-ctl restart # 重启所有 GitLab 组件
sudo gitlab-ctl status # 查看 GitLab 状态
在gitlab.rb文件中必须要修改三个参数。
# 修改为您的新域名或 IP 地址,例如:
external_url 'http://your.new.domain.com'
# 用于ssh的域名(或者是名称),其实没什么用
gitlab_rails['gitlab_ssh_host']
# 真正控制ssh的端口,
gitlab_rails['gitlab_shell_ssh_port'] = '2222'
例子:
external_url 'https://gitlab.company.com'
gitlab_rails['gitlab_ssh_host'] = 'mygitlab.com'
gitlab_rails['gitlab_shell_ssh_port'] = 2222
用户用于clone
git clone ssh://[email protected]:2222/group/project.git
事实上你在本地添加秘钥进行设置后,可以用任意域名+2222来访问
本地设置
修改你本地.ssh中的config文件。
Host mygitlab.com
HostName aaa.bbb
User git
Port 2222
IdentityFile /Users/niys/.ssh/miyao
IdentitiesOnly yes
你实际通过访问的是aaa.bbb:2222,但你Host得和上面的 gitlab_ssh_host相同,这样才能匹配到秘钥和访问地址。
秘钥的创建方法:
本地生成 SSH Key(如果你还没有)
ssh-keygen -t ed25519 -C "[email protected]"
一路回车即可
生成后文件在:
~/.ssh/id_ed25519
~/.ssh/id_ed25519.pub
2️⃣ 把 公钥 加到 GitLab
复制公钥:
cat ~/.ssh/id_ed25519.pub
登录 GitLab Web:
右上角头像 → Preferences → SSH
- 粘贴公钥
- 保存
3️⃣ 测试 SSH 是否成功(关键一步)
ssh -T [email protected] -p 2222
成功会看到:
Welcome to GitLab, @root!
防止被黑
修改/etc/ssh/sshd_config
### 1. 全局配置项(放在文件顶部) ###
Port 22
Port xxxx
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
# 注意:新版本 Ubuntu 中 ChallengeResponseAuthentication 已更名为 KbdInteractiveAuthentication
KbdInteractiveAuthentication no
# 必须放在全局位置
UsePAM yes
# 其他默认参数...
# AcceptEnv LANG LC_*
# Subsystem sftp /usr/lib/openssh/sftp-server
### 2. 条件匹配块(必须放在文件最末尾) ###
# 针对 22 端口:只允许你的 sudo 用户登录
Match LocalPort 22
# 请将 your_sudo_user 替换为你实际的用户名
AllowUsers your_sudo_user
PasswordAuthentication yes
# 针对 xxxx端口:只允许 git 用户登录
Match LocalPort xxxx
AllowUsers git
第二步:配置防火墙
如果你启用了 ufw 防火墙,必须手动放行这两个端口,否则你会被锁在系统之外。
# 允许 sudo 用户使用的 22 端口
sudo ufw allow 22/tcp
# 允许 git 用户使用的 xxxx端口
sudo ufw allow xxxx/tcp
# 重新加载防火墙
sudo ufw reload
第三步:检查并重启 SSH 服务
在重启之前,务必检查配置文件语法是否正确:
sudo sshd -t
如果没有任何输出,说明语法正确。如果有错误,请根据提示修改后再继续。
确认无误后重启服务:
sudo systemctl restart ssh
CI/CD
安裝gitlab-runner
完整执行:你的二进制安装命令(原样保留,顺序执行即可)
# 1. 下载最新稳定版 GitLab Runner ARM64 二进制文件
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm64
# 2. 赋予执行权限(必须,二进制文件默认无执行权限)
sudo chmod +x /usr/local/bin/gitlab-runner
# 3. 创建官方默认的 gitlab-runner 系统用户(纯系统用户,无登录shell,官方标准)
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
核心步骤:注册为 systemctl 标准系统服务(你最需要的,重中之重)
# 4. 生成 systemctl 标准服务文件(官方模板,一键生成,无需手动编写)
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
# 5. 重载 systemctl 服务配置,让系统识别新注册的服务(必须执行)
sudo systemctl daemon-reload
# 6. 设置开机自启(核心,重启树莓派自动运行,永久生效)
sudo systemctl enable gitlab-runner
# 7. 启动 gitlab-runner 服务
sudo systemctl start gitlab-runner
方案 1:【重中之重】默认 gitlab-runner 用户 权限配置(无坑版,必做)
给默认 gitlab-runner 用户配置【sudo 免密权限】(万能首选,一次配置终身无忧)
# 用visudo编辑sudoers文件,语法校验,防止改错导致sudo失效,最安全的方式
sudo visudo
在文件的最后一行,粘贴以下内容,不要修改任何其他内容:
gitlab-runner ALL=(ALL) NOPASSWD: ALL
保存退出(树莓派默认 nano 编辑器):Ctrl+O → 回车确认文件名 → Ctrl+X 退出
✅ 方案 2:补充权限 - 将 gitlab-runner 加入核心用户组(继承权限,无副作用)
树莓派默认登录用户是 pi,将 gitlab-runner 加入 pi/sudo/docker 组,继承所有目录和命令权限,按顺序执行:
# 加入pi用户组:获取/home/pi目录的读写执行权限
sudo usermod -aG pi gitlab-runner
# 加入sudo组:系统管理员权限,加固sudo免密配置
sudo usermod -aG sudo gitlab-runner
# 加入docker组:如果需要用docker执行器构建,必须加这行(可选)
sudo usermod -aG docker gitlab-runner
# 修复/home/gitlab-runner工作目录权限(Runner的默认工作目录,必执行)
sudo chown -R gitlab-runner:gitlab-runner /home/gitlab-runner
sudo chmod -R 755 /home/gitlab-runner
使用apt安装gitlab-runner
# 安装所有必备依赖
sudo apt install -y curl ca-certificates apt-transport-https gnupg2 software-properties-common -y
# 导入GitLab官方密钥
curl -L "https://packages.gitlab.com/runner/gitlab-runner/gpgkey" | sudo gpg --dearmor -o /usr/share/keyrings/gitlab-runner-archive-keyring.gpg
# 写入树莓派5 ARM64专属GitLab源
echo "deb [signed-by=/usr/share/keyrings/gitlab-runner-archive-keyring.gpg arch=arm64] https://packages.gitlab.com/runner/gitlab-runner/raspbian/ bookworm main" | sudo tee /etc/apt/sources.list.d/gitlab-runner.list
# 更新源 + 一键安装 GitLab Runner 官方完整版
sudo apt update -y
sudo apt install -y gitlab-runner -y
注冊runner
✅ 第一步:获取项目的 Runner 注册令牌(Token)
- 打开你的 GitLab 项目页面。
- 进入 设置(Settings) > CI/CD > Runners。
- 展开 “Project runners” 区域。
- 找到 “Registration token”(形如
glrt-xxxxx),点击复制。
🔒 这个令牌是当前项目专用的,不要泄露。
✅ 第二步:确定你的 Runner 配置需求
根据你提供的选项,建议如下配置:
| 配置项 | 推荐值 | 说明 |
|---|---|---|
| 标签(Tags) | raspberry,shell(可自定义) |
用于指定哪些 CI 任务可以在这个 Runner 上运行 |
| 运行无标签任务 | ✅ 勾选(启用) | 这样即使 .gitlab-ci.yml中没写 tags,任务也能运行 |
| Runner 描述 | 树莓派5 - 项目专用Runner |
方便识别 |
| 暂停(Paused) | ❌ 不勾选 | 否则无法接收任务 |
| 受保护(Protected) | ❌ 不勾选 | 除非你只希望在 main等受保护分支运行 |
| 锁定到当前项目 | ✅ 勾选 | 安全最佳实践,仅本项目可用 |
| 最大作业超时时间 | 3600秒(1小时) |
最小值为 600 秒,建议设为 3600 |
💡 如果你不填标签,但启用了“运行无标签任务”,那么所有 CI 任务都会在这个 Runner 上执行。
✅ 第三步:在树莓派上生成并执行注册命令
在你的 树莓派终端 中运行以下命令(请替换其中的占位符):
sudo gitlab-runner register \
--non-interactive \
--url "http://你的-gitlab地址/" \
--registration-token "YOUR_PROJECT_REGISTRATION_TOKEN" \
--description "树莓派5 - 项目专用Runner" \
--executor "shell" \
--tag-list "raspberry,shell" \
--run-untagged="true" \
--locked="true"
🔧 参数说明:
--url:你的 GitLab 地址,例如:- 如果 GitLab 和 Runner 在同一台树莓派上 → 用
http://localhost - 如果通过局域网访问 → 用
http://192.168.x.x或http://raspberrypi.local
- 如果 GitLab 和 Runner 在同一台树莓派上 → 用
--executor "shell":最轻量的方式,直接在系统 shell 中运行命令,无需 Docker,非常适合树莓派。--tag-list:指定标签,多个用逗号分隔(英文逗号)。--run-untagged="true":允许运行没有标签的任务。--locked="true":此 Runner 仅限当前项目使用。
📌 权限设置(首次运行前执行):
sudo mkdir -p /home/gitlab-runner/{builds,cache} sudo chown -R gitlab-runner:gitlab-runner /home/gitlab-runner
✅ 第四步:验证 Runner 是否成功注册
-
查看状态:
sudo gitlab-runner verify -
检查配置文件:
cat /etc/gitlab-runner/config.toml应能看到你刚注册的 Runner 信息。
-
回到 GitLab 网页,在 CI/CD > Runners 页面,你会看到一个 绿色在线状态 的 Runner。
✅ 示例:.gitlab-ci.yml 如何使用这个 Runner
# .gitlab-ci.yml
test-on-rpi:
script:
- echo "任务正在树莓派上运行!"
- python3 --version
tags:
- raspberry # 可选;如果你禁用了“运行无标签任务”,就必须加 tags
如果你启用了“运行无标签任务”,也可以完全不写
tags,任务依然会分配给这个 Runner。
⚠️ 常见问题排查
| 问题 | 解决方法 |
|---|---|
| Runner 显示灰色(离线) | 运行 sudo systemctl restart gitlab-runner |
| 任务卡在 “pending” | 检查是否标签匹配,或是否启用了“运行无标签任务” |
| 权限错误(如无法写文件) | 将 gitlab-runner用户加入对应用户组:sudo usermod -aG yourgroup gitlab-runner |
| 内存不足导致崩溃 | 确保 --max-builds 1,并在 CI 脚本中避免大内存操作 |
cuo
ERROR: Registering runner... failed correlation_id=01KF06972HSPPXJ0WQSK3TMGQ7 runner=oZ5s-tkgY status=GET https://gitlab.sljjj.top/api/v4/runners: 401 Unauthorized
PANIC: Failed to register the runner.
关键点
| 问题 | 解决方案 |
|---|---|
401 Unauthorized(注册) |
使用项目内网地址 + 正确 Token 注册 Runner |
prepare environment: exit status 1 |
修复 .bash_logout中的 clear_console |
HTTP Basic: Access denied(克隆失败) |
放弃自动 Git,改用 GIT_STRATEGY: none+ 手动 git clone |
| 自定义 SSH 端口(3922) | 通过 ~/.ssh/config配置端口 + 密钥 |
优化 .gitlab-ci.yml(可选)
你可以封装成模板,例如:
.default-ssh-clone:
variables:
GIT_STRATEGY: none
before_script:
- rm -rf ${CI_PROJECT_NAME}
- git clone --depth 1 -b $CI_COMMIT_REF_NAME ssh://[email protected]:xxxx/${CI_PROJECT_PATH}.git
- cd ${CI_PROJECT_NAME}
- git checkout $CI_COMMIT_SHA
build-go:
extends: .default-ssh-clone
tags:
- raspberry
script:
- go build -v .