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

使用ubuntu:latest Docker镜像时文件未复制到指定目录问题求助

问题根源

你当前的流水线是在GitLab Runner启动的Ubuntu容器内部完成所有操作——包括安装Apache、复制文件到容器的/var/www/html。容器执行完流水线任务就会销毁,除非你的Runner配置里把/var/www/html挂载成了持久卷,否则这些内容根本不会落到你实际要部署的Apache服务器上。

解决方案

正确的思路是:通过SSH连接到你的目标Apache服务器,在目标服务器上执行部署操作,而非在Runner的临时容器里折腾。

修改后的流水线配置如下:

image: ubuntu:latest

stages:
  - deploy

deploy_to_apache:
  stage: deploy
  script:
    - apt-get update -y
    - apt-get install -y git ssh-client  # 仅需ssh客户端,无需安装Apache
    - mkdir -p /root/.ssh
    - echo "$SSH_Key" > /root/.ssh/id_rsa
    - chmod 600 /root/.ssh/id_rsa
    # 将GitLab和目标服务器的公钥加入已知主机,避免连接时的确认提示
    - ssh-keyscan gitlab.com >> /root/.ssh/known_hosts
    - ssh-keyscan 你的Apache服务器IP或域名 >> /root/.ssh/known_hosts
    # 远程登录目标服务器,执行部署命令
    - ssh 目标服务器用户名@你的Apache服务器IP "
        rm -rf /var/www/html/* &&
        git clone git@gitlab.com:User_name/project_name.git /tmp/project &&
        cp -r /tmp/project/* /var/www/html/ &&
        service apache2 restart
      "
  only:
    - main
  tags:
    - CommonRunner
注意事项
  • 确保$SSH_Key对应的私钥,同时拥有目标Apache服务器的登录权限和GitLab仓库的拉取权限。如果不想共用密钥,可以在目标服务器单独配置Git的SSH密钥。
  • 目标服务器必须已安装git和apache2,无需在Runner容器重复安装。
  • 若觉得SSH密钥管理麻烦,可改用HTTPS拉取代码,用GitLab个人访问令牌替代:
    git clone https://oauth2:你的个人访问令牌@gitlab.com/User_name/project_name.git /tmp/project
    

内容的提问来源于stack exchange,提问作者Umair Saeed

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 20:41:11