本文介绍了Gitlab CI如何将最新部署到特定目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Gitlab中有两个项目,其中一个是子模块(让我们称之为repofrontend-templates)另一个(我们称之为repomain)。我为前端模板回购建立了一个Gitlab CI构建。事情是,我不需要测试或建设。我只需要在需要的目录中部署此CI。所以,我注册了一个frontend-templates项目的跑步者,并将.gitlab-ci.yml添加到根目录:

  job_main:
类型:deploy
script:echo什么也不做

我推送到我的存储库,跑步者将最新的提交获取到以下目录:

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

只需运行 echoDo没有



现在,我希望跑步者将测试提交部署到位于以下位置的开发服务器: p>

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

编辑:我将脚本更改为

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

但是我收到错误消息:

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



我应该向dev环境的根目录授权吗?或者是有另一种方法来做到这一点?

解决方案

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


$ b但是,这些并不是原子操作 - 它们会在执行代码时处于不确定的状态,如果它们失败,它们会变得很乱。我建议您的 / var / www / myapp / submodules / frontend-templates 只是包含代码的目录的符号链接:

  / var / www / myapp / submodules 
| - 7348110b
| - a03ed59a
| - 前端模板 - > ./a03ed59a

您可以根据提交散列来命名代码目录。工作本身可能如下所示:

  job_main:
类型: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

注:显然,跑步者需要必要的文件权限来执行任务。


I have two projects in Gitlab where one is a submodule (let's call repo "frontend-templates") of the other (let's call this repo "main"). I have set up a Gitlab CI build for the "frontend-templates" repo. The thing is that I don't need testing or building. I only need deploying for this CI in the needed directory. So, I registered a runner for "frontend-templates" project and added .gitlab-ci.yml to the root directory:

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

When I push to my repository, the runner fetches the latest commit to the following directory:

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

and just runs echo "Do nothing".

Now, I want the runner to deploy the "tested" commit to the dev server, which is located in:

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

EDIT: I changed the script to

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

but I got an error saying:

This makes sense because gitlab-runner user does not have access to any directory in /var/www/myapp but it a problem for me because I want to run gulp after deploy, so it compiles necessary scripts after it pulls from the remote repository.

Should I give permission to the root directory of dev environment? Or is there another way to do this?

解决方案

You can simply perform some form of deployment using the directory you are in. You can rename/delete the directory of the currently deployed code and copy the checkout code there (rm -rf /var/www/myapp/submodules/frontend-templates && cp -r . /var/www/myapp/submodules/frontend-templates) or you can use rsync to do the synchronization.

However, these are not atomic operations - they will leave your deployed code in an uncertain state while they are being performed and in a mess if they fail. I'd suggest your /var/www/myapp/submodules/frontend-templates is just a symlink to a directory containing the code:

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

You can name the code directories according to the commit hash. The job itself could then look something like this:

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

NOTE: Obviously, the runner will need necessary file permissions to perform the tasks.

这篇关于Gitlab CI如何将最新部署到特定目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 10:04
查看更多