Last active
September 5, 2022 08:40
-
-
Save johnfmorton/afddda967583aaa2fc4e40ad52dcea1b to your computer and use it in GitHub Desktop.
deploy-from-git.sh - Used on Arcustech to deploy a Craft CMS site
This file contains 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
# This script is used to deploy a Craft CMS site on an Arcustech server | |
# | |
# It will clone the git main branch from a private repo into | |
# a 'deployments' directory and them create symlinks for the | |
# static assets: .env, and 3 directories of assets. | |
# It then does a composer install of the Craft site. | |
# The scripts in the composer file are like this: https://github.com/nystudio107/devmode/blob/f2b231e772026860f75e255c9e22722dac983de8/cms/composer.json#L55 | |
# These scripts update Craft, clear caches, etc. | |
# Finally, it will symlink the web directory in the newly downloaded files | |
# to the public folder which is the one used by Arcustech | |
# Define the directories we'll use | |
BASEDIR=$(pwd) | |
DEPLOYMENTSDIR=$BASEDIR'/deployments' | |
BUILDDIR=$DEPLOYMENTSDIR'/build-'$(date '+%Y-%m-%d-%H-%M-%S') | |
# helper function make repeating characters | |
# Used at the end of the script | |
# example: | |
# MESSAGE="* Hello World *" | |
# printf_new "*" ${#MESSAGE} | |
# echo "* $MESSAGE *" | |
# printf_new "*" ${#MESSAGE} | |
printf_new() { | |
str=$1 | |
num=$2 | |
v=$(printf "%-${num}s" "$str") | |
echo "${v// /*}" | |
} | |
# Being the process | |
echo "creating directory: $BUILDDIR" | |
mkdir -p $BUILDDIR | |
echo "moving to the new directory" | |
cd $BUILDDIR | |
# NOTE: The server this site is on has a "my_project_name" | |
# in it's ~/.ssh/config file; for example: | |
# | |
# Host my_project_name | |
# Hostname github.com | |
# User git | |
# IdentityFile ~/.ssh/my_project_name_rsa | |
# | |
# this key is in the Deployment key area in the project on Github also | |
# | |
# This allows me to clone a private repo like this | |
# git clone my_project_name:<user>/<repo>.git | |
echo "cloning the master git repo" | |
git clone project_name:johnfmorton/gitreponame | |
echo "cd gitreponame/cms" | |
cd gitreponame/cms/ | |
# CUSTOMIZE the specific things you need linked | |
echo "Symlink the .env file in /static_assets" | |
ln -s $BASEDIR/static_assets/.env | |
echo "Symlink projects folder from static_assets" | |
mkdir $BUILDDIR/gitreponame/cms/web/assets | |
ln -s $BASEDIR/static_assets/projects $BUILDDIR/gitreponame/cms/web/assets/projects | |
echo "Symlink general folder from static_assets" | |
ln -s $BASEDIR/static_assets/general $BUILDDIR/gitreponame/cms/web/assets/general | |
echo "Symlink userphotos folder from static_assets" | |
ln -s $BASEDIR/static_assets/userphotos $BUILDDIR/gitreponame/cms/web/assets/userphotos | |
echo "composer install" | |
composer install | |
MESSAGE="* Symlink the public folder to $BUILDDIR/gitreponame/cms/web *" | |
printf_new "*" ${#MESSAGE}; | |
echo "$MESSAGE"; | |
printf_new "*" ${#MESSAGE}; | |
# First, remove the old symlink | |
rm /storage/myusernameonserver/www/public_html/public | |
# Now that it's gone, create the new one | |
ln -sf $BUILDDIR/gitreponame/cms/web /storage/myusernameonserver/www/public_html/public | |
echo 'Clean up deployment directory to keep only the last 3 builds' | |
cd $DEPLOYMENTSDIR | |
echo $(pwd) | |
# the -n +4 means "starting at the 4th item in the list", we'll remove what is listed, i.e. removing older directories | |
ls -tl | grep "^d" | tail -n +4 | xargs rm -rf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment