Skip to content

Instantly share code, notes, and snippets.

Last active September 5, 2022 08:40
Show Gist options
  • Save johnfmorton/afddda967583aaa2fc4e40ad52dcea1b to your computer and use it in GitHub Desktop.
Save johnfmorton/afddda967583aaa2fc4e40ad52dcea1b to your computer and use it in GitHub Desktop. - Used on Arcustech to deploy a Craft CMS site
# 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:
# 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
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() {
v=$(printf "%-${num}s" "$str")
echo "${v// /*}"
# Being the process
echo "creating directory: $BUILDDIR"
mkdir -p $BUILDDIR
echo "moving to the new directory"
# 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
# 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'
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