Skip to content

Instantly share code, notes, and snippets.

@francisbesset
Forked from marcw/deploy.sh
Created Feb 2, 2017
Embed
What would you like to do?
Symfony deployment bash script
#!/bin/bash
# some configurations values
host="foobar-web00" # the name of the host in my ssh config file
project_path="/var/www/foobar"
user="web"
key_filename="/path/to/.ssh/web-deploy-key"
dry_run="--dry-run"
assets_differ=""
export PATH="$PATH:./node_modules/grunt-cli/bin"
export PATH="$PATH:./node_modules/grunt-init/bin"
export PATH="$PATH:./node_modules/less/bin"
export PATH="$PATH:./node_modules/react-tools/bin"
export PATH="$PATH:./node_modules/uglify-js/bin"
export PATH="$PATH:./node_modules/clean-css/bin"
export PATH="$PATH:./node_modules/webpack/bin"
export PATH="$PATH:./node_modules/babel/bin"
if [ "$1" == "force" ]
then
dry_run=""
fi
function say {
echo -e ""
}
function say_red {
echo -e "\033[31m>>> $@\033[0m"
}
function say_green {
echo -e "\033[32m>>> $@\033[0m"
}
function say_yellow {
echo -e "\033[33m>>> $@\033[0m"
}
function rsync_files {
say_yellow "Rsyncing to remote"
exclude="--exclude-from=app/rsync-exclude.txt"
options="--no-owner --no-group --progress -crDpLt --force --delete"
rsync ${dry_run} ${exclude} ${options} ./ ${user}@${host}:${project_path}
}
function scp_file {
protect scp $1 ${user}@${host}:$2
}
function run_local {
say_yellow "[local] $@"
$@
}
function run {
say_yellow "[remote] $@"
ssh ${user}@${host} "cd ${project_path} && $@"
}
function pre_deploy {
run_local npm install
run_local composer install
run_local composer dump-autoload -o
protect create_version_file
protect run_only_if_assets_differ make assets
protect run_local ./bin/console cache:clear --env=prod --no-debug --no-warmup
protect run_local ./bin/console cache:clear --env=dev --no-debug --no-warmup
protect run_local ./bin/console cache:clear --env=test --no-debug --no-warmup
}
function create_version_file {
git log --pretty=%H --max-count=1 > web/version.txt
}
function run_only_if_assets_differ {
if [ -z "$assets_differ" ]
then
remote_version=`curl https://www.foobar.io/version.txt`
local_version=`cat web/version.txt`
files="app/Resources/js app/Resources/img/ app/Resources/style bootstrap.config.js bootstrap.config.less npm-shrinkwrap.json package.json webpack.config.js"
code=`git diff --exit-code --name-only ${remote_version} ${local_version} -- ${files}`
if [ "0" -eq "$?" ]
then
assets_differ="no"
else
assets_differ="yes"
fi
fi
if [ "yes" == "$assets_differ" ]
then
$@
else
say_green "[Skip (Assets)] $@"
fi
}
function deploy {
protect run_only_if_assets_differ deploy_assets
protect deploy_admin_assets
rsync_files
}
function deploy_admin_assets {
aws s3 sync web/bundles s3://foobar-prod-assets/bundles --acl="public-read"
}
function deploy_assets {
DATE=`php -r 'echo (new DateTime("+1 year"))->format(\DateTime::RFC1123);'`
aws s3 cp web/assets/*.js s3://foobar-prod-assets/assets/ --content-type="application/javascript" --acl="public-read" --expires="${DATE}"
aws s3 cp web/assets/*.css s3://foobar-prod-assets/assets/ --content-type="text/css" --acl="public-read" --expires="${DATE}"
aws s3 cp web/assets/*.svg s3://foobar-prod-assets/assets/ --content-type="image/svg+xml" --acl="public-read" --expires="${DATE}"
aws s3 cp web/*.ico s3://foobar-prod-assets/ --acl="public-read" --expires="${DATE}"
aws s3 cp web/*.png s3://foobar-prod-assets/ --acl="public-read" --expires="${DATE}"
aws s3 sync web/assets s3://foobar-prod-assets/assets --acl="public-read" --expires="${DATE}"
aws s3 sync web/img s3://foobar-prod-assets/img --acl="public-read" --expires="${DATE}"
}
function protect {
if [ -z "$dry_run" ]
then
$@
else
say_red "[dry run] $@"
fi
}
function post_deploy {
say_yellow "Executing post deploy tasks"
protect run ./rabbitmq.sh
protect run sudo supervisorctl stop foobar_swarrot:*
protect run sudo supervisorctl reload
protect run mkdir -p var/cache
protect run mkdir -p var/logs
protect run chmod -R 0777 var/logs
protect run php bin/console cache:clear --no-debug --env=prod
protect run php bin/console doctrine:migrations:status --env=prod
protect run php bin/console doctrine:migrations:migrate --no-interaction --env=prod
protect run sudo /usr/local/bin/web-reload
protect run chmod -R 0777 var/cache
protect run sudo supervisorctl start foobar_swarrot:*
}
pre_deploy
deploy
post_deploy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment