Created
June 2, 2018 18:59
-
-
Save jjcodes78/67712f2c43b98ad45057388a9eb7b71b to your computer and use it in GitHub Desktop.
Codeship deploy script (/home/user/.deploy)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
#--------------------------------------------------------------- | |
# Continuous Integration (CI) - Deployment Shell Script | |
# Build for deploy Laravel applications after CI check | |
# Author: Jorge -JJSQUADY- Junior | |
# July, 2017 | |
# | |
# Transfer this file to server | |
# and run it: bash <name_of_this_script> | |
#--------------------------------------------------------------- | |
# DEPLOYMENT VARS | |
# Git repository host | |
ORIGIN_HOST=${REPOSITORY_HOST} | |
# Repository ssh link to deploy | |
GIT_REPOSITORY=${REPOSITORY_USER}@${REPOSITORY_HOST}:${REPOSITORY}.git | |
# Branch to deploy it | |
BRANCH=${DEPLOY_BRANCH} | |
# Database configuration (SQLITE only) | |
DATABASE=database.sqlite | |
# Application folder | |
APP_DIR=bustracker | |
# Application relative path (~/webapps default) | |
APP_ROOT_DIR=~/webapps | |
# Alias to application full path | |
DIRECTORY=${APP_ROOT_DIR}/${APP_DIR} | |
# Class to seed database | |
DB_SEED= | |
# User | |
HOME_USER=jjsquady | |
# Set the post actions command to run | |
EXTRA_ARTISAN_COMMAND=${COMMAND} | |
UPDATE_MODULES=${MODULES} | |
# Storage link | |
STORAGE_LINK= | |
#DEPLOY CONFIGURATION BASE FOLDER | |
DEPLOY_CFG_FOLDER=~/.deploy | |
# ------------------------------------------------------ | |
# Install all dependencies and future deps | |
# PS: a composer running on server its required | |
# ------------------------------------------------------ | |
installDependencies() { | |
echo "$1 dependencies..." | |
rm composer.lock | |
composer install --no-interaction | |
npm install | |
} | |
# ------------------------------------------------------ | |
# Works only with sqlite, and creates a new sqlite file | |
# in database directory | |
# ------------------------------------------------------ | |
createDatabase() { | |
echo "Creating database..." | |
touch ${DIRECTORY}/database/${DATABASE} | |
} | |
# ------------------------------------------------------ | |
# If DB_SEED variable is set | |
# A seeder class are called to feed data into database | |
# ------------------------------------------------------ | |
seedDatabase() { | |
echo "Seeding database" | |
if [ "$DB_SEED" != '' ]; then | |
php artisan db:seed --force --class=$1 | |
else | |
echo "No data seeded." | |
fi | |
} | |
# ------------------------------------------------------ | |
# Executes the first application deployment on server | |
# ------------------------------------------------------ | |
firstDeploy() { | |
echo "Checking for applications root directory..." | |
if [ ! -d "$APP_ROOT_DIR" ]; then | |
mkdir ${APP_ROOT_DIR} | |
fi | |
echo "Change to webapps directory" | |
cd ${APP_ROOT_DIR} | |
# ------------------------------------------------------ | |
# Pull branch to deploy | |
# ------------------------------------------------------ | |
echo "Cloning from $BRANCH..." | |
git clone -b ${BRANCH} ${GIT_REPOSITORY} ${APP_DIR} || exit; | |
cd ${APP_DIR} | |
installDependencies "Installing" | |
if [ "$DATABASE" != '' ]; then | |
createDatabase | |
fi | |
# ------------------------------------------------------ | |
# Copy default application enviroment file | |
# and rename it to .env (required by Laravel) | |
# Set the application KEY (required for hashing) | |
# ------------------------------------------------------ | |
echo "Configuring the application..." | |
cp ${DEPLOY_CFG_FOLDER}/env.${APP_DIR}.${DEPLOY_BRANCH} ${DIRECTORY}/.env | |
php artisan key:generate | |
# ------------------------------------------------------ | |
# Configure the database | |
# ------------------------------------------------------ | |
echo "Migrating tables" | |
php artisan migrate --force | |
seedDatabase | |
# ------------------------------------------------------ | |
# Adjusts the storage folder | |
# ------------------------------------------------------ | |
echo "Creating link..." | |
ln -s /home/${HOME_USER}/${DIRECTORY}/storage/app/${STORAGE_LINK}/ public/${STORAGE_LINK} | |
echo "" | |
echo "Done." | |
} | |
# ------------------------------------------------------ | |
# Executes the application update | |
# ------------------------------------------------------ | |
applyUpdate() { | |
echo "Change to webapps directory" | |
cd ${DIRECTORY} | |
# ------------------------------------------------------ | |
# Pull updates from repository | |
# Updates composer dependencies | |
# and install new ones if have it | |
# ------------------------------------------------------ | |
echo "Pulling updates..." | |
git reset --hard | |
git pull origin ${BRANCH} | |
installDependencies "Updating" | |
# ------------------------------------------------------ | |
# Update Laravel Modules | |
# ------------------------------------------------------ | |
echo "Updating Laravel Modules...." | |
if [ "$UPDATE_MODULES" != '' ]; then | |
composer require ${UPDATE_MODULES} | |
fi | |
# ------------------------------------------------------ | |
# Copy default application environment file | |
# and rename it to .env (required by Laravel) | |
# Set the application KEY (required for hashing) | |
# ------------------------------------------------------ | |
echo "Updating the application environment..." | |
cp ${DEPLOY_CFG_FOLDER}/env.${APP_DIR}.${DEPLOY_BRANCH} ${DIRECTORY}/.env | |
# ------------------------------------------------------ | |
# Update the migrations and new feed data (if has) | |
# ------------------------------------------------------ | |
echo "Updating migrations..." | |
php artisan migrate --force | |
echo "Clearing cache..." | |
php artisan cache:clear | |
seedDatabase | |
echo "" | |
echo "Done." | |
} | |
# ------------------------------------------------------ | |
# Try to check the git repository host | |
# | |
# 1) checks if the known_hosts ssh file exists | |
# 2) if not, create a new and continues... | |
# 3) check for host key, if host exists in | |
# hosts file, returns and continues... | |
# 4) if not, scans the host for a ssh key | |
# store the key in hosts file, and continues... | |
# ------------------------------------------------------ | |
checkOriginHost() { | |
if [ ! -f "~/.ssh/known_hosts" ]; then | |
echo "Creating a new Known Hosts file." | |
touch ~/.ssh/known_hosts | |
fi | |
echo "Checking host..." | |
ssh-keygen -F ${ORIGIN_HOST} | |
if [ "$?" == "0" ]; then | |
echo "${ORIGIN_HOST} exists and verified!" | |
return | |
fi | |
ssh-keyscan -t rsa -H ${ORIGIN_HOST} >> ~/.ssh/known_hosts | |
echo "A new host found, verified and added" | |
} | |
# ------------------------------------------------------ | |
# Check for application DIRECTORY | |
# start the correctly deployment setup | |
# If application directory not exists then | |
# calls the first deployment... | |
# otherwise applies the update | |
# ------------------------------------------------------ | |
startDeploy() { | |
if [ ! -d "$DIRECTORY" ]; then | |
echo "Initializing first deploy..." | |
firstDeploy | |
fixPermissions | |
else | |
echo "Applying updates..." | |
applyUpdate | |
chmod 775 database/${DATABASE} | |
fi | |
if [ "$EXTRA_ARTISAN_COMMAND" ]; then | |
php artisan ${EXTRA_ARTISAN_COMMAND} | |
fi | |
} | |
#------------------------------------------------------- | |
# Fix directories and files permissions | |
#------------------------------------------------------- | |
fixPermissions() { | |
echo "Fixing permissions..." | |
cd .. | |
chown -R ${HOME_USER}:www-data ${APP_DIR}/ | |
cd ${APP_DIR} | |
chmod 775 database/${DATABASE} | |
chmod 775 -R storage/ | |
chmod 775 -R bootstrap/cache/ | |
} | |
# ------------------------------------------------------ | |
# Script initialization... | |
checkOriginHost | |
startDeploy | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment