首页 文章

Gitlab CI如何将最新部署到特定目录

提问于
浏览
1

我在Gitlab中有两个项目,其中一个是另一个的子模块(让我们称之为repo“frontend-templates”)(让我们称之为repo“main”) . 我已经为“frontend-templates”回购设置了一个Gitlab CI构建 . 问题是我不需要测试或构建 . 我只需要在所需目录中部署此CI . 所以,我为“frontend-templates”项目注册了一个跑步者,并将.gitlab-ci.yml添加到了根目录:

job_main:
   type: deploy
   script: echo "Do nothing"

当我推送到我的存储库时,运行器将最新的提交提取到以下目录:

/home/gitlab-runner/builds/6231b425/0/Gasimzada/frontend-templates

然后运行 echo "Do nothing" .

现在,我希望运行器将“已测试”提交部署到开发服务器,该服务器位于:

/var/www/myapp/submodules/frontend-templates

编辑:我将脚本更改为

script: cd /var/www/myapp/submodules/frontend-templates && git pull

但我得到一个错误说:

无法打开/var/www/myapp/.git/modules/submodules/frontend-templates/FETCH_HEAD:权限被拒绝

这是有道理的,因为gitlab-runner用户无法访问/ var / www / myapp中的任何目录,但这对我来说是个问题,因为我想在部署后运行 gulp ,因此它在从远程存储库中提取后编译必要的脚本 .

我是否应该授予dev环境的根目录权限?或者还有另一种方法吗?

1 回答

  • 5

    您可以使用您所在的目录简单地执行某种形式的部署 . 您可以重命名/删除当前部署的代码的目录并在那里复制结帐代码( rm -rf /var/www/myapp/submodules/frontend-templates && cp -r . /var/www/myapp/submodules/frontend-templates ),也可以使用 rsync 进行同步 .

    但是,这些不是原子操作 - 它们会在执行过程中使您部署的代码处于不确定状态,如果它们失败则会处于混乱状态 . 我建议您的/ var / www / myapp / submodules / frontend-templates只是包含代码的目录的符号链接:

    /var/www/myapp/submodules
      | - 7348110b
      | - a03ed59a
      | - frontend-templates -> ./a03ed59a
    

    您可以根据提交哈希命名代码目录 . 工作本身可能看起来像这样:

    job_main:
       type: deploy
       script:
         - cp -r . /var/www/myapp/submodules/$CI_BUILD_REF
         - ln -s ./$CI_BUILD_REF /var/www/myapp/submodules/templink
         - mv -Tf /var/www/myapp/submodules/templink /var/www/myapp/submodules/frontend-templates
    

    注意:显然,跑步者需要必要的文件权限才能执行任务 .

相关问题