简体   繁体   中英

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

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.

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 )

The best way to install dependencies is from an action hook, ie 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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