Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- REPO='git@github.com:Username/repo.git';
- APP_DIR='/var/www/site/path';
- BRANCH=master
- DB_USER=SomeUser
- DB_PASS=SomePassword
- DB_NAME=database
- BUILD="build";
- ACTIVE="active"
- PREVIOUS="previous_`date +%y%m%d-%H%M%S`";
- STEP=1
- function log_step
- {
- echo "Step $STEP: $1..." 1>&2
- STEP=$((STEP+1))
- }
- function error_exit
- {
- echo "ERROR, aborting: $1!" 1>&2
- exit 1
- }
- # Enter app dir (create if not exist)
- [ -d $APP_DIR ] || mkdir $APP_DIR;
- cd $APP_DIR || error_exit "Can not enter application directory";
- # Delete it if already exist
- [ -d $BUILD ] && rm -rf $BUILD && log_step "Deleted existing build directory"
- # Git clone
- log_step "Fetch latest code";
- git clone -b $BRANCH $REPO $BUILD || error_exit "Unable to clone";
- # Enter build dir
- cd $APP_DIR/$BUILD || error_exit "Can not enter build directory";
- # Composer
- log_step "Install Composer packages";
- composer install --prefer-dist --no-scripts --no-dev || error_exit "Composer install failed";
- # npm
- log_step "Install npm packages"
- npm install || error_exit "NPM install failed";
- # Optimize Laravel
- log_step "Clean compiled and optimize"
- php artisan clear-compiled --env=production || error_exit "Laravel clean-compiled failed";
- php artisan optimize --env=production || error_exit "Laravel optimize failed";
- # gulp
- log_step "Build assets"
- gulp --production || error_exit "Gulp failed";
- # Environment file
- log_step "Copying environment file";
- cp $APP_DIR/$ACTIVE/.env .env || error_exit "Copying of environment file failed";
- # DB dump
- log_step "Backup database";
- mysqldump -u $DB_USER -p$DB_PASS $DB_NAME > $APP_DIR/$ACTIVE/db_deploy.sql || error_exit "DB dump failed";
- # Migrate
- log_step "Database migration";
- php artisan migrate --force --env=production || error_exit "Database migration failed";
- # Permissions
- log_step "Settings permissions";
- chgrp www-data -R bootstrap/cache/;
- chgrp www-data -R storage/;
- chmod -R g+w,o-w bootstrap/cache/;
- chmod -R g+w,o-w storage/;
- # Replacing web app
- log_step "Stopping web sever and queue handler";
- sudo service nginx stop;
- sudo service queue stop;
- log_step "Swapping folders";
- mv $APP_DIR/$ACTIVE $APP_DIR/$PREVIOUS || error_exit "Failed to move active folder to previous";
- mv $APP_DIR/$BUILD $APP_DIR/$ACTIVE || error_exit "Failed to move build folder to active";
- log_step "Starting web sever and queue handler";
- sudo service nginx start;
- sudo service queue start;
- log_step "Finishing up, setting cache";
- cd $APP_DIR/$ACTIVE;
- php artisan config:cache --env=production;
- php artisan route:cache --env=production;
- echo "Deploy complete :D"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement