Skip to content

Instantly share code, notes, and snippets.

@deizel
Created July 31, 2015 20:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save deizel/836ca0c7c276b1a04834 to your computer and use it in GitHub Desktop.
Save deizel/836ca0c7c276b1a04834 to your computer and use it in GitHub Desktop.
Multiple global Composer installations

Multiple global Composer installations

When setting up CI servers, it can sometimes be necessary to have different versions of Composer dependencies installed globally.

For example, when your CI server mainly deals with CakePHP 2.x and 3.x applications, where the coding standards vary.

This guide aims to walk you through how one might acheive this.

Composer home

The trick to this whole guide is really just to take advantage of the COMPOSER_HOME environment variable.

This allows you to change the default global path from ~/.composer to something else.

Example

Here we set up a dedicated global directory for CakePHP 3.x composer dependencies.

$ mkdir ~/.composer-cake3
$ COMPOSER_HOME=~/.composer-cake3 composer global require "cakephp/cakephp-codesniffer:2.*" "phpunit/phpunit:*"
$ crontab -e

And some simple cronjobs to handle routine tasks:

# Composer maintenance
@daily /usr/bin/composer self-update
@daily /usr/bin/composer global update
@daily COMPOSER_HOME=~/.composer-cake3 /usr/bin/composer global update

Now, in the case of CakePHP, we can set the install path of the custom PHPCS standard:

$ ~/.composer-cake3/vendor/bin/phpcs --config-set installed_paths ~/.composer-cake3/vendor/cakephp/cakephp-codesniffer/

As you can see here, the PHPCS binary and config for CakePHP 3.x is completely separate from the (old) existing one:

$ phpcs --version
PHP_CodeSniffer version 1.5.4 (stable) by Squiz (http://www.squiz.net)
$ phpcs --config-show
$ ~/.composer-cake3/vendor/bin/phpcs --version
PHP_CodeSniffer version 2.3.3 (stable) by Squiz (http://www.squiz.net)
$ ~/.composer-cake3/vendor/bin/phpcs --config-show
installed_paths: /home/deploy/.composer-cake3/vendor/cakephp/cakephp-codesniffer/

Lastly, we just need to update any CI tasks to use this version of PHPCS.

diff --git a/scripts/jenkins-ant-tasks/build.xml b/scripts/jenkins-ant-tasks/build.xml
index b365b20..0079ba1 100644
--- a/scripts/jenkins-ant-tasks/build.xml
+++ b/scripts/jenkins-ant-tasks/build.xml
@@ -145,10 +145,10 @@

        <target name="phpcs-ci-3" description="Find coding standard violations using PHP_CodeSniffer creating a log file for the continuous integration server">
                <echo message="Require phpcs pass to deploy is set to: ${phpcsRequired}" />
-               <exec executable="phpcs" output="/dev/null" failonerror="${phpcsRequired}">
+               <exec executable="/home/deploy/.composer-cake3/vendor/bin/phpcs" output="/dev/null" failonerror="${phpcsRequired}">
                        <arg value="--report=full" />
                        <arg value="--report-file=${env.WORKSPACE}/build/logs/phpcs.txt" />
-                       <arg value="--standard=PSR2" />
+                       <arg value="--standard=CakePHP" />
                        <arg value="--extensions=php" />
                        <arg path="${env.WORKSPACE}/build/changes" />
                </exec>

And that's it! Just rinse and repeat for each project type you need to support on your CI server.

This also works for other Composer dependencies such as PHPUnit, PHPMD, and so forth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment