Advertisement
Guest User

Untitled

a guest
May 21st, 2016
803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. REPO='git@github.com:Username/repo.git';
  4. APP_DIR='/var/www/site/path';
  5. BRANCH=master
  6.  
  7. DB_USER=SomeUser
  8. DB_PASS=SomePassword
  9. DB_NAME=database
  10.  
  11. BUILD="build";
  12. ACTIVE="active"
  13. PREVIOUS="previous_`date +%y%m%d-%H%M%S`";
  14.  
  15. STEP=1
  16.  
  17. function log_step
  18. {
  19. echo "Step $STEP: $1..." 1>&2
  20. STEP=$((STEP+1))
  21. }
  22.  
  23. function error_exit
  24. {
  25. echo "ERROR, aborting: $1!" 1>&2
  26. exit 1
  27. }
  28.  
  29. # Enter app dir (create if not exist)
  30. [ -d $APP_DIR ] || mkdir $APP_DIR;
  31. cd $APP_DIR || error_exit "Can not enter application directory";
  32.  
  33. # Delete it if already exist
  34. [ -d $BUILD ] && rm -rf $BUILD && log_step "Deleted existing build directory"
  35.  
  36. # Git clone
  37. log_step "Fetch latest code";
  38. git clone -b $BRANCH $REPO $BUILD || error_exit "Unable to clone";
  39.  
  40. # Enter build dir
  41. cd $APP_DIR/$BUILD || error_exit "Can not enter build directory";
  42.  
  43. # Composer
  44. log_step "Install Composer packages";
  45. composer install --prefer-dist --no-scripts --no-dev || error_exit "Composer install failed";
  46.  
  47. # npm
  48. log_step "Install npm packages"
  49. npm install || error_exit "NPM install failed";
  50.  
  51. # Optimize Laravel
  52. log_step "Clean compiled and optimize"
  53. php artisan clear-compiled --env=production || error_exit "Laravel clean-compiled failed";
  54. php artisan optimize --env=production || error_exit "Laravel optimize failed";
  55.  
  56. # gulp
  57. log_step "Build assets"
  58. gulp --production || error_exit "Gulp failed";
  59.  
  60. # Environment file
  61. log_step "Copying environment file";
  62. cp $APP_DIR/$ACTIVE/.env .env || error_exit "Copying of environment file failed";
  63.  
  64. # DB dump
  65. log_step "Backup database";
  66. mysqldump -u $DB_USER -p$DB_PASS $DB_NAME > $APP_DIR/$ACTIVE/db_deploy.sql || error_exit "DB dump failed";
  67.  
  68. # Migrate
  69. log_step "Database migration";
  70. php artisan migrate --force --env=production || error_exit "Database migration failed";
  71.  
  72. # Permissions
  73. log_step "Settings permissions";
  74. chgrp www-data -R bootstrap/cache/;
  75. chgrp www-data -R storage/;
  76. chmod -R g+w,o-w bootstrap/cache/;
  77. chmod -R g+w,o-w storage/;
  78.  
  79. # Replacing web app
  80. log_step "Stopping web sever and queue handler";
  81. sudo service nginx stop;
  82. sudo service queue stop;
  83.  
  84. log_step "Swapping folders";
  85. mv $APP_DIR/$ACTIVE $APP_DIR/$PREVIOUS || error_exit "Failed to move active folder to previous";
  86. mv $APP_DIR/$BUILD $APP_DIR/$ACTIVE || error_exit "Failed to move build folder to active";
  87.  
  88. log_step "Starting web sever and queue handler";
  89. sudo service nginx start;
  90. sudo service queue start;
  91.  
  92. log_step "Finishing up, setting cache";
  93. cd $APP_DIR/$ACTIVE;
  94. php artisan config:cache --env=production;
  95. php artisan route:cache --env=production;
  96.  
  97. echo "Deploy complete :D"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement