You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel项目GitLab CI/CD搭建遇阻:Runner注册后构建报错求助

GitLab CI/CD SSH执行器Runner构建失败排查方案

1. 修正SSH信任文件的权限与归属

GitLab Runner默认以gitlab-runner系统用户运行,你创建的/root/.ssh/known_hosts对它无效,需为该用户配置正确的SSH信任文件:

# 创建gitlab-runner用户的SSH目录
sudo mkdir -p /home/gitlab-runner/.ssh
# 将服务器IP添加到信任列表
sudo ssh-keyscan <你的服务器IP> >> /home/gitlab-runner/.ssh/known_hosts
# 设置SSH要求的严格权限
sudo chown -R gitlab-runner:gitlab-runner /home/gitlab-runner/.ssh
sudo chmod 600 /home/gitlab-runner/.ssh/known_hosts

2. 核对Runner核心配置

打开/etc/gitlab-runner/config.toml文件,检查SSH执行器的关键参数:

  • host:确认是服务器的正确IP(避免混淆localhost、内网IP与公网IP)
  • user:该用户必须拥有目标项目目录的访问权限
  • identity_file:如果使用SSH密钥认证,不能留空,需填写密钥文件路径(比如/home/gitlab-runner/.ssh/id_rsa);如果用密码认证,确保password字段填写正确

3. 调整.gitlab-ci.yml的路径逻辑

你当前before_script中直接cd /var/www/html/laravelcicd存在逻辑问题:SSH执行器会先将代码克隆到Runner的临时工作目录,直接跳转服务器固定目录会脱离CI的工作上下文。建议修改为:

stages:
  - build
  - deploy
before_script:
  - echo "Before script"
  # CI默认在克隆后的项目工作目录执行命令,无需手动跳转服务器目录
building:
  stage: build
  script:
    - echo "In the script section"
deploying:
  stage: deploy
  script:
    # 若要部署到目标目录,通过SSH同步代码
    - scp -r . <用户名>@<服务器IP>:/var/www/html/laravelcicd/
    - echo "Deployed!!"

4. 验证权限与SSH连通性

  • 测试gitlab-runner用户对目标目录的访问权限:
    sudo -u gitlab-runner ls -l /var/www/html/laravelcicd
    
    若无权限,将用户加入Apache用户组并调整目录权限:
    sudo usermod -aG www-data gitlab-runner
    sudo chmod -R g+w /var/www/html/laravelcicd
    
  • 手动测试SSH连接,排查基础连通问题:
    sudo su - gitlab-runner
    ssh <用户名>@<服务器IP>
    
    根据连接报错(如密码错误、密钥未授权)针对性修复

内容的提问来源于stack exchange,提问作者Shashank Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 09:35:22