Skip to content

Instantly share code, notes, and snippets.

@ajax13
Last active November 7, 2017 14:38
Show Gist options
  • Save ajax13/18ab02ac3eb887edb924fb246e36a2ac to your computer and use it in GitHub Desktop.
Save ajax13/18ab02ac3eb887edb924fb246e36a2ac to your computer and use it in GitHub Desktop.
Script shell/bash to deploy Symfony
#!/usr/bin/env bash
declare -xr REPOSITORY="https://github.com/xx/yyy.git"
BASE_SSH='ssh'
#export GLOBAL_VAR="value"
#declare -x GLOBAL_VAR="value" #global variable
#readonly CURRENT_SHELL_VAR=value
dev_env() {
#Git config
BRANCH=$1
# local BRANCH=$1
#VM config
SERVER_IP="xx.xx.xx.xx"
SERVER_PORT="22"
SERVER_USER="username"
PROJECT="project_dir"
PROJECTS_ROOT_DIR="/var/www/"
CERTIFICATE=""
#Symfony Config
SYMFONY_ENV="dev"
SKIP_SCHEMA_UPDATE="true"
SKIP_ASSETIC_DUMP="true"
SKIP_ASSETS_INSTALL="true"
}
staging_env() {
#Git config
BRANCH=$1
#VM config
SERVER_IP="xx.xx.xx.x"
SERVER_PORT="22"
SERVER_USER="username"
PROJECT="project_name"
PROJECTS_ROOT_DIR="/var/www/"
CERTIFICATE=""
#Symfony Config
SYMFONY_ENV="dev"
SKIP_SCHEMA_UPDATE="true"
SKIP_ASSETIC_DUMP="true"
SKIP_ASSETS_INSTALL="true"
}
prod_env() {
#Git config
BRANCH="master"
#VM config
SERVER_IP="xx.xx.xx.xx"
SERVER_PORT="22"
SERVER_USER="username"
PROJECT="project_name"
PROJECTS_ROOT_DIR="/var/www/"
CERTIFICATE="/.../.ssh/xxx.pem"
#Symfony Config
SYMFONY_ENV="prod"
SKIP_SCHEMA_UPDATE="true"
SKIP_ASSETIC_DUMP="true"
SKIP_ASSETS_INSTALL="true"
}
#!/usr/bin/env bash
clear
echo "SCRIPT BEGINS"
echo #print empty ligne
echo "include config environment variables"
#Add custom functions available to current shell environment
#The Shell script deploy_sf.sh runs as a parent process and deploy_config.sh as child process of deploy_sf.sh, so we need "export" to make varibales visible to parent child
source config_sf.sh
# check environment config
if [ "$1" == "" ]; then
echo "Error: Environment required: use deploy_sf2 <env> ['dev', 'staging', 'prod']"
exit 1
fi
#Check env function existence
if [ `type -t $1_env` != 'function' ]; then
echo "Error: environment config function $1_env not found"
exit 1
fi
#Check git branch name
if [[ -z "$BRANCH" && "$2" == "" ]]; then
echo "Error: Environment required: use deploy_sf2 <env> <branch>"
exit 1
elif [[ -z "$BRANCH" && "$2" != "" ]]; then
BRANCH="$2"
fi
#Start deployment
START_TIME=$(date +%s)
echo "Start deploying for '$1' environment..."
echo
echo "create default config variables"
#call env function
"$1_env" $BRANCH
# if a different port is required
if [ ! "$SERVER_PORT" == "" ]; then
BASE_SSH="$BASE_SSH -p $SERVER_PORT"
fi
# when a certificate is requied
if [ ! "$CERTIFICATE" == "" ]; then
BASE_SSH="$BASE_SSH -i $CERTIFICATE"
fi
# find out what symfony version we are dealing with
if [ -e 'app/console' ]; then
CONSOLE_PATH="$PROJECTS_ROOT_DIR/app/console"
CACHE_PATH="$PROJECTS_ROOT_DIR/app"
else
if [ -e 'bin/console' ]; then
CONSOLE_PATH="PROJECTS_ROOT_DIR/bin/console"
CACHE_PATH="$PROJECTS_ROOT_DIR/var"
else
echo "Symfony version not found"
exit 1
fi
fi
echo $BRANCH
echo $REPOSITORY
echo $PROJECT
echo $PROJECTS_ROOT_DIR
# SSH connect
$BASE_SSH "$SERVER_USER"@"$SERVER_IP" 'bash -s' < setup_sf.sh $PROJECTS_ROOT_DIR $PROJECT $REPOSITORY $BRANCH $SYMFONY_ENV
echo '
_____
(__) / \
(@o) ( Done! )
/-------\/ --" \_____/
/ | ||
* ||----||
^^ ^^
'
END_TIME=$(date +%s)
echo $'\n'"Deployed in $(($END_TIME-$START_TIME)) seconds."
echo "SCRIPT FINISHED!!"
echo
#!/usr/bin/env bash
PROJECTS_ROOT_DIR=$1
PROJECT=$2
REPOSITORY=$3
BRANCH=$4
SYMFONY_ENV=$5
#Switch Project Root path
cd "$PROJECTS_ROOT_DIR"
#check if project dir exist, if not created
[ -d "$PROJECT" ] || mkdir "$PROJECT"
#Switch Project Dir
cd "$PROJECT"
#Check if we are inside a git work tree
if [ ! `git rev-parse --is-inside-work-tree` ]; then
echo "Cloning git project"
echo $(pwd)
cd ..
git clone --branch "$BRANCH" "$REPOSITORY" "$PROJECT"
cd "$PROJECT"
else
echo "Pulling git project"
#Get all tags and force progress reporting
git fetch --tags --progress "$REPOSITORY" origin "$BRANCH"
#echo "[pull code] update code from bitbucket repository"
git pull --rebase origin "$BRANCH"
fi
[$SYMFONY_ENV -eq "dev"] && COMPOSER_DEV = --no-dev || COMPOSER_DEV=""
echo "[composer install] check package to install"
php composer.phar install $COMPOSER_DEV --optimize-autoloader
##echo "[validate doctrine schema]"
##php app/console doctrine:schema:validate
##php app/console doctrine:ensure-production-settings
#
##echo "[update database]"
##php app/console doctrine:schema:update --dump-sql --force
#
##php app/console innova:dev-fixtures:load
echo "[Symfony assetic: install symlink]"
php app/console assets:install --symlink --relative --env="$SYMFONY_ENV"
echo "[Symfony assetic: lancer un dump]"
php app/console assetic:dump --env="$SYMFONY_ENV" --no-debug
echo "[Symfony Cache clear]"
php app/console cache:clear --env="$SYMFONY_ENV" --no-debug --no-warmup
##echo "[modify permissions] cache and log"
##sudo chmod -R 777 app/cache/
##sudo chmod -R 777 app/logs/
#
#bower install
@ajax13
Copy link
Author

ajax13 commented Nov 7, 2017

To debug the shell script:

chmod u+x setup_sf.sh deploy_sf.sh config_sf.sh
./deploy_sf.sh -x CONFIG_NAME BRANCH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment