Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add composer install check #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions post-receive
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,42 @@ ACCOUNT="" # the account name your site is associated to
PASSWORD="" # the account password your site is associated to
SITE_ID="000000" # the reference ID of your site you can find in your *sites* section
NPM_INSTALL_CHECK=false # a boolean to enable if your site is a _Node.js_ site, and if you want to automatically run `npm install` if your package.json changed since the last push (default to `false`)
COMPOSER_INSTALL_CHECK=false # enable automatic composer install if composer.json or composer.lock changed

# Function checking if the "package.json" file has been changed since last push
# MIT © Sindre Sorhus - sindresorhus.com / Adapted by J.M. Cléry for Alwaysdata
npm_install_check() {
# git hook to run a command after `git push` if a specified file was changed
changed_files="$(git diff --name-only HEAD^ HEAD)"

check_run() {
if [ $(echo "$changed_files" | grep "$1") ]; then
cd $TARGET
echo "'package.json' has been updated! Running 'npm install' automatically for you..."
echo "'$1' has been updated! Running '$2' automatically for you..."
eval "$2"
cd $GIT_DIR
fi
}

# Run `npm install` ONLY if "package.json" has changed
# check_run package.json "npm install --silent --no-audit --no-progress > /dev/null"
check_run package.json "npm install > /dev/null"
}

# Function checking if "composer.json" or "composer.lock" has been changed since last push
composer_install_check() {
changed_files="$(git diff --name-only HEAD^ HEAD)"

check_run() {
if [ $(echo "$changed_files" | grep -E "$1") ]; then
cd $TARGET
echo "'$1' has been updated! Running 'composer install' automatically for you..."
eval "$2"
cd $GIT_DIR
fi
}

# Run `composer install` if "composer.json" or "composer.lock" has changed
check_run "composer.(json|lock)" "composer install --no-dev --optimize-autoloader"
}

###
# DO NOT EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING
###
Expand All @@ -49,11 +64,16 @@ while read oldrev newrev ref; do
echo "Deploying '${BRANCH}' branch to production"
git --work-tree=$TARGET --git-dir=$GIT_DIR checkout --force $BRANCH

# If it's a Node.js website and `npm install` needs to be trigerred
# If it's a Node.js website and `npm install` needs to be triggered
if [ $NPM_INSTALL_CHECK = true ]; then
npm_install_check
fi

# If it's a Symfony project and `composer install` needs to be triggered
if [ $COMPOSER_INSTALL_CHECK = true ]; then
composer_install_check
fi

if [ "$RESTART" = true ]; then
echo "Restarting your web server ..."

Expand All @@ -63,7 +83,7 @@ while read oldrev newrev ref; do
if [ "$status" = 204 ]; then
echo "✅ Site successfully restarted!"
else
echo "❌ An error occured when restarting your site, try to restart it manually"
echo "❌ An error occurred when restarting your site, try to restart it manually"
exit 1
fi
fi
Expand All @@ -77,3 +97,4 @@ while read oldrev newrev ref; do
exit 0
fi
done