How to run (or should I run) Composer on Openshift

2019-04-10 01:28发布

问题:

I am trying to create ZendFramework's Skeleton application on Openshift. I have created a PHP5 application using these instructions and cloned it locally. I cloned the ZendFramework skeleton application in the repository, then ran Composer to install dependencies locally.

I then pushed my repository to Openshift, but I get the following error message:

Fatal error: Uncaught exception 'RuntimeException' with message
'Unable to load ZF2. Run `php composer.phar install`
or define a ZF2_PATH environment variable.' in
/var/lib/openshift/559d4d8f500446844700002b/app-
root/runtime/repo/init_autoloader.php:51 Stack trace: #0
/var/lib/openshift/559d4d8f500446844700002b/app-
root/runtime/repo/public/index.php(18): require() #1 {main} thrown in
/var/lib/openshift/559d4d8f500446844700002b/app-
root/runtime/repo/init_autoloader.php on line 51

which is indicative that I need to run Composer on Openshift. How do I achieve this?

In my local repository, the /vendor directory is filled with dependency directories. However, /vendor is ignored in the commit. I could try to commit and push it, but is this the right way to proceed? It does not look clean.

回答1:

Basically, you need to execute composer install with each build automatically on OpenShift.

You might do this by adding a marker file named use_composer in the folder .openshift/markers.

.openshift/markers/use_composer

Referencing: https://developers.openshift.com/en/php-markers.html


If you need to do more than just composer install, like installing Composer, using action_hooks is the better choice. They allow to work with bash scripts.

See, https://developers.openshift.com/en/managing-action-hooks.html

.openshift/action_hooks/build:

#!/bin/bash

export COMPOSER_HOME="$OPENSHIFT_DATA_DIR/.composer"

if [ ! -f "$OPENSHIFT_DATA_DIR/composer.phar" ]; then
    curl -s https://getcomposer.org/installer | php -- --install-dir=$OPENSHIFT_DATA_DIR
else
    php $OPENSHIFT_DATA_DIR/composer.phar self-update
fi

( unset GIT_DIR ; cd $OPENSHIFT_REPO_DIR ; php $OPENSHIFT_DATA_DIR/composer.phar install )


回答2:

The best way to install dependencies is from an action hook, i.e. a script run by the cartridge during the deployment. I would use the deploy hook:

#!/bin/bash
# @file
#   .openshift/action_hooks/deploy

COMPOSER_DIR="$OPENSHIFT_DATA_DIR/bin"

function install_composer() {
    echo 'Installing Composer'

    if [ ! -d "$COMPOSER_DIR" ]
    then
        mkdir -p $COMPOSER_DIR
    fi

    curl -s https://getcomposer.org/installer | php -- --install-dir=$COMPOSER_DIR
}

if [ ! -x "$COMPOSER_DIR/composer" ]
then
    install_composer
fi

$COMPOSER_DIR/composer self-update
cd $OPENSHIFT_REPO_DIR
$COMPOSER_DIR/composer install

Also remember to make this script executable: chmod +x .openshift/action_hooks/deploy.