Repository deployment and Composer : what workflow? - php

As a PHP developer I find myself working with Composer a lot. 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.
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.
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.
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 ? 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.
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.
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. 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).

Capistrano (or Capifony if you are using Symfony2) is very useful for deployments with 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. 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 (i.e. in this case the git checkout and the composer install) except one single mv would happen in isolation, outside of 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).

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
#!/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. 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.
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. The composer.lock keeps track of exactly what packages (and versions) you have installed. When you deploy, send your composer.lock file up to the production server as well, and simply do a 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.

Related

Git: how to unignore all Symfony vendor files?

unfortunately I can't use composer, due to low memory on the webspace. That's why I'm trying to push vendor/* to the git repository, to be able to pull the full project, including the dependencies.
In my .gitignore I'm forcing git to unignore the vendor files by !/vendor/*.
It works for most files, but not for /vendor/friendsofsymfony/jsrouting-bundle.
Content of /vendor/friendsofsymfony/jsrouting-bundle/.gitignore:
/phpunit.xml
/composer.lock
/composer.phar
/vendor/
/node_modules/
My repository is hosted at BitBucket. I don't know yet the meaning of the different folder-icon and the hashs next to it. If anyone knows, please comment.
Does anyone know how to force git to handle all the vendor files?
Thanks in advance!
Solutions
As the jsrouting-bundle is a git submodule, I chose this solution:
git submodule add git://github.com/FriendsOfSymfony/FOSJsRoutingBundle.git vendor/friendsofsymfony/jsrouting-bundle
Another way would be to use the deps file (not tested), source:
https://github.com/XKEYGmbH/ifresco-client/tree/master/vendor/friendsofsymfony/jsrouting-bundle/FOS/JsRoutingBundle/Resources/doc
The jsrouting-bundle folder is a Git submodule. A Git submodule is actually a reference to another Git repository. This is why you cannot add changes from it to your original Git repository.
I had to do this in the past, here is what I did:
php composer.phar selfupdate
php composer.phar update
In the vendor directory run sudo find . -type d -name .git | xargs rm -rf
Commit all modifications: git add -A .
With this, your vendor will be commit like the src directory, so no need to run composer install when deploying in your production environment. When wanting to update just repeat the process. But of course it isn't a good practice and you should do this only if you can't run composer on your production server.

How to include Composer dependencies in a Git repo

When creating sites using a framework like Silverstripe I often want to use helper modules like gridfieldextensions and lumberjack.
I therefore use composer require to add the dependencies.
However when I follow my regular development work flow and use git add -A to add the module to the repo rather than the code being added to the repo I get a reference to it.
This causes problems when I then try to clone the site elsewhere (using Jenkins or another developer). The git clone or git pull leaves an empty directory.
I solve this by deleting the .git dir of the module and adding all the files.
Is there a better way to do this? Is using git submodule an option?
Somewhere i found a good .gitignore file that ignores everything and i have to tell it to include the custom modules for my project. It's like:
# ignore everything...
/*
# ...but
!/.htaccess
!/.gitignore
!/composer.json
!/composer.lock
!/Capfile
!/Gemfile
!/favicon.ico
!/touch-icon-*
!/mysite
!/some-module
#...other modules
# theme stuff
!/themes/
**/.sass-cache
**/node_modules
!**/node_modules/_manifest_exclude
#no assets in general, but /assets/.htaccess
!/assets
/assets/*
!assets/.htaccess
As FinBoWa already said you need the composer.json and composer.lock file in your project and running
composer install
on another machine it'll install the packages in the versions saved in the composer.lock file on that machine
composer install --no-dev
will only install the "normal" requirements, no dev-requirements like phpunit or other stuff you only need for developing or testing but not live
composer install --no-dev -o
will also optimize (-o) the auto loader, so it'll be a bit faster.
composer update
will update your packages, which might have funny side effects and break your site. So use it carefully and test afterwards.
composer update silverstripe/framework
will just update that package and finally
composer update silverstripe/*
will update all packages by the vendor silverstripe (e.g. framework and cms package)
See also:
gitignore documentation
composer documentation

Is there a place to get the laravel `vendor` folder in one spot?

I realize that this is what composer is used for, but I don't particularly like it. It makes sense, but it annoys me that laravel5's github doesn't work out of the box because it's vendor directory is somewhat large and isn't necessarily laravel5, although laravel does require it.
It also puzzles me why composer doesn't get it itself.
I would expect to be able to
git clone the_laravel5_github_url
composer install
and be able to run as it seemed like you were able to do at one time, but for some reason now, the vendor directory isn't there. I feel like this makes laravel harder to set up as it isn't so obvious.
Is there a place where someone can get an recent version of this? I find it disappointing that there is no mention of it on their readme and that you were able to do it before and still can't despite using composer install.
Install Composer on your OS using this command
curl -sS https://getcomposer.org/installer | php.
Move the composer.phar file to /usr/local/bin/ with this command
mv composer.phar /usr/local/bin/composer. This will enable you to access composer globally.
git clone your project.
Make a vendors folder in the root of your project.
cd to root of your project and run composer update. This command will look for vendors folder in the root and will install all the packages required by your project in it.
Happy Coding
Thank you :)
Use composer install in the same folder of your app. Try composer update. And install composer globally in your OS.

Can composer.phar be deployed with the code?

I'm using Composer for a small project. I've pushed composer.json and composer.lock to Git, and put the vendor/ folder into .gitignore, so I can install the dependencies at the server on deploy time.
Can I push composer.phar to the Git repo or should I install a new copy for the server? Not sure if the installation process is machine-dependant.
It's a lot easier to install composer.phar manually on each server where you need it, as it will prompt to be updated every 30 days, and you don't want to be forever updating the content of your repo for a composer update.
Composer is a tool to help you with your deployments, so it should not be a part of your deployments
I personally store composer.phar in git for some PHP projects, usually in the project root directory.
This has the advantage that you can easily enforce the same composer version across multiple team members / computers, which can reduce the differences in composer.lock files, especially after running composer update. This may not be very standard, but I often document this in the project README.

Install FOSUserBundle after cloning from Git

I've created my own site on my local computer and it works wonderfully. I'm having tons of fun developing with it. But when it came time to move it to my server and test I ran into issue.
I'm using the FOSUserBundle. Obviously being a Git of it's own, when I did a git commit and push, git ignored everything in vendor/friendsofsymfony/user-bundle. So when I cloned to my server, the folder came empty, and of course now my site doesn't work.
I want to do this the right way. So if it means destroying the git repo and doing it a different way, I'm ok with that.
So far I've tried.
Following the original install instructions for FOSUserBundle
Running the following:
composer update friendsofsymfony/user-bundle
composer install friendsofsymfony/user-bundle
Both return "nothing to update/install"
I've also tried clearing composer's cache between each command attempt.
rm -rf ~/.composer/cache
Answer is in the comments. Ignore vendor/ use composer to install the appropriate files.
also this Symfony project cloned with git vendors not installed
What it sounds like your trying to do is create a mywebsite parent repo within which you have a third party child repo that has the FOSUserBundle code. This uses git submodules and requires related submodule commands like git submodule init.
The ... directory is there, but empty. You must run two commands: git submodule init to initialize your local configuration file, and git submodule update to fetch all the data from that project and check out the appropriate commit listed in your superproject...
I don't know anything about composer. Here is how you could do it with git.
cd mywebsite
git init
git submodule add <url-to-FOSUserBundle-repo>
You should now have a directory structure like this:
mywebsite
.git
.gitmodules
FOSUserBunder
.git
See also http://git-scm.com/book/en/v2/Git-Tools-Submodules

Categories