简体   繁体   English

存储库部署和Composer:什么工作流程?

[英]Repository deployment and Composer : what workflow?

As a PHP developer I find myself working with Composer a lot. 作为一名PHP开发人员,我发现自己经常使用Composer。 In the past it was on personal projects and such so I didn't have much problems with it, but now with Laravel 4 it's on project that require deploying and I'm in kind of a struggle to adapt my workflow. 在过去,这是个人项目,所以我没有太多的问题,但现在与Laravel 4项目需要部署,我很难适应我的工作流程。

All my projects are git repositories, thus per convention and because it's still quite buggy, like most developers I put the vendor directory in my .gitignore . 我的所有项目都是git存储库,因此按照惯例,因为它仍然非常错误,像大多数开发人员一样,我将vendor目录放在我的.gitignore Now the problem is : I also use Git to deploy to the server, and by all logic the vendor directory is not uploaded as it's not tracked by the repository. 现在的问题是:我还使用Git部署到服务器,并且所有逻辑都没有上传供应商目录,因为它没有被存储库跟踪。

So my question is towards people that have worked with Composer and Git for longer than me : what is the best workflow to keep the server in sync ? 所以我的问题是那些与Composer和Git合作的人比我更长的时间:保持服务器同步的最佳工作流程是什么? How to track the vendor folder without really tracking it ? 如何跟踪供应商文件夹而不真正跟踪它? I tried uploading it every time I update with Composer but some of my vendor folders are quite big and I can't manually upload 30Mb of files every time something updates. 我每次使用Composer更新时都尝试上传它,但我的一些供应商文件夹非常大,每次更新时我都无法手动上传30Mb的文件。

I don't really know, how do you guys work it out ? 我真的不知道,你们是怎么解决这个问题的? I tried not ignoring the vendor folder but Git just messes it up, half are recognized as cloned repos and are just ignored anyway, etc. 我试图不忽略vendor文件夹,但Git只是搞砸了,一半被认为是克隆的回购,而且无论如何都被忽略了等等。

UPDATE : Note that I'm on a shared host so I don't have access to the server's terminal. 更新:请注意,我在共享主机上,因此我无法访问服务器的终端。

The best way is to run composer install on the server after updating to the latest code. 最好的方法是在更新到最新代码后在服务器上运行composer install You should also make sure you commit your composer.lock file, which the server will then use to install (you should not run composer update on the server). 您还应该确保提交composer.lock文件,然后服务器将使用该文件进行安装(您不应该在服务器上运行composer update )。

Capistrano (or Capifony if you are using Symfony2) is very useful for deployments with composer. Capistrano(或Capifony,如果您使用的是Symfony2)对于使用composer的部署非常有用。 You can trigger a deployment remotely and it will run a composer install in isolation so the site remains online until it has been deployed successfully. 您可以远程触发部署,它将独立运行作曲家安装,以便站点保持联机状态,直到成功部署。 There are many other benefits such as retaining previous deployments and rolling back, copying old vendors before deployments, compiling assets etc. etc. 还有许多其他好处,例如保留以前的部署和回滚,在部署之前复制旧供应商,编译资产等。

I'm working on something like this in the git post-receive hook on the server. 我正在服务器上的git post-receive hook中处理这样的事情。 This isn't tested and may be buggy, but you should get the idea. 这没有经过测试,也可能是错误的,但你应该明白这个想法。

#!/bin/bash
# get the updated composer.json
git checkout master -- composer.json

# only do this stuff if composer.json is different
# you could check this manually, or with git or cmp
cp composer.json tmp/composer.json

# may take a minute, but won't take the site down
(cd tmp; composer install --prefer-dist)

# this doesn't seem to be atomic
git checkout -f

# switch vendors over
# this isn't quite an atomic operation, but is very close
# you could probably do it with symlinks and "mv -Tf" to make it atomic
mv vendor vendor.old
mv tmp/vendor vendor

rm -r tmp vendor.old

Ideally all of the deploy (ie in this case the git checkout and the composer install ) except one single mv would happen in isolation, outside of www . 理想情况下,除了一个单独的mv之外的所有部署(即在这种情况下是git checkoutcomposer install )将在www之外单独发生。 This doesn't work if you have untracked files (eg CMS uploads) in your working tree and rely on PHP's __FILE__ not resolving symlinks (due to this PHP bug ). 如果您在工作树中有未跟踪的文件(例如CMS上传)并且依赖PHP的__FILE__而不解析符号链接(由于此PHP错误 ),则这不起作用。

This is an old question but in case anybody is looking a solution: 这是一个老问题,但万一有人在寻找解决方案:

I slightly modify the @dave1010 answer to use git pull instead of git checkout--force 我稍微修改了@ dave1010的答案,使用git pull而不是git checkout--force

#!/bin/bash
# get only composer files 
git fetch
git checkout origin/master -- composer.json
git checkout origin/master -- composer.lock

# make sure tmp is empty
rm -rf tmp
mkdir tmp

# copy the composer files to tmp
cp -r vendor tmp/vendor
cp composer.json tmp/composer.json
cp composer.lock tmp/composer.lock

# may take a minute, but won't take the site down
(cd tmp; composer install --no-scripts --verbose; cd ..)

# switch vendors over
rm -rf vendor_old
mv vendor vendor_old
mv tmp/vendor vendor

# update your code
git pull

# run again composer install. This time will print 'Nothing to install or update'
# but will execute pre/post scripts & generate autoload files
composer install --optimize-autoloader

There is maybe a better solution using capistrano/composer . 使用capistrano / composer可能有更好的解决方案。 But I like mine better. 但我更喜欢我的。

You can use something like jenkins to ftp your files over this way you can direct jenkins to run composer install on the jenkins server and then ftp the files over. 您可以使用像jenkins这样的东西来ftp你的文件,你可以指导jenkins在jenkins服务器上运行composer install然后ftp文件。

This also allows you to ignore the vendor folder. 这也允许您忽略供应商文件夹。

It does require a build server to be made and you would need to be able to execute commands vs the build server 它确实需要构建服务器,您需要能够执行命令与构建服务器

The key is your composer.lock file. 关键是你的composer.lock文件。 The composer.lock keeps track of exactly what packages (and versions) you have installed. composer.lock会准确跟踪您安装的软件包(和版本)。 When you deploy, send your composer.lock file up to the production server as well, and simply do a composer update . 部署时,也将composer.lock文件发送到生产服务器,然后只进行编辑器composer update All the exact same package versions will be installed. 将安装所有完全相同的软件包版本。 With deployment software like Capistrano or Flightplan you can make the composer update step part of the process so it happens automatically. 使用像Capistrano或Flightplan这样的部署软件,您可以使composer update步骤成为流程的一部分,以便自动进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM