Skip to content

Instantly share code, notes, and snippets.

@vfsoraki
Created October 26, 2019 21:51
Show Gist options
  • Save vfsoraki/be645fee69bb43b67986c28ed4bad0b1 to your computer and use it in GitHub Desktop.
Save vfsoraki/be645fee69bb43b67986c28ed4bad0b1 to your computer and use it in GitHub Desktop.
A simple bash script to deploy a simple app to server using just SSH. Tested with Lumen/Laravel
#!/usr/bin/env bash
# This script is used to simply copy some files using `rsync`
# and then running some commands on server.
# Command can be run synchronously, or in background.
# For example, your can restart your `supervisor` workers
# and PHP-FPM after copying new files.
# NOTE: This script does not require any kind of version control, e.g. `git`
# Server name. Define the connection parameters in `~/.ssh/config` so that a
# simple `ssh my-server` works in your shell.
SERVER=my-server
# Deployment path on server. Should be an already-made directory
# Webserver configuration should probably point to `$DEPLOY_PATH/public`.
DEPLOY_PATH=/var/www/html/deploy
set -e
# Utility functions
function run {
if [ "$2" == 'bg' ]
then
echo "==> CMD(background): $1"
ssh $SERVER "$1 >/dev/null 2>&1 || echo '===!!! Command failed: $1'" >/dev/null 2>&1 &
RUN_PID=$!
else
echo "==> CMD: $1"
ssh $SERVER "$1 || echo '===!!! Command failed: $1'"
echo '==============Done=============='
fi
}
function runDeploy {
echo "==> Running in deploy destination"
run "cd $DEPLOY_PATH && $1" $2
}
function runLocal {
echo "==> CMD: $1"
$1 || echo "===!!! Command failed: $1"
echo '==============Done=============='
}
# Deployment tasks
# For a Lavel application, make sure to add `--exclue=node_modules` too
runLocal "rsync \
-az \
--exclude=.git \
--exclude=vendor \
--exclude=.env \
. \
$SERVER:$DEPLOY_PATH"
# Simple check, if deploy script is called like `./deploy.sh files` don't
# do anything with services, just copy files. Makes faster deployment when you
# don't need to restart services for a quick change. Feel free to modify for your needs.
if [ "$1" != 'files' ]
then
# For this to work, make sure to also have `composer.phar` in your project root
runDeploy 'php composer.phar install --no-dev --no-progress --no-suggest --no-interaction'
runDeploy 'php composer.phar dump'
# You can probably add your app installation commands here, like `bundle install` or `npm install`
# Restart supervisor, running in background, and capture its PID
run 'supervisorctl restart worker-name:\*' 'bg'
SUPERVISOR_PID=$RUN_PID
# Restart HTTP service, in background, and capture its PID
# This can mean PHP-FPM or whatever else
run 'systemctl restart http.service' 'bg'
HTTP_PID=$RUN_PID
# Wait for tasks to complete
echo "==> Waiting for supervisorctl(${SUPERVISOR_PID}) to finish"
wait "$SUPERVISOR_PID"
echo "==> Waiting for http.service(${HTTP_PID}) to finish"
wait "$HTTP_PID"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment