Linking to a shared folder across deployments

Oftentimes, you may have resources, such as images and videos, that don’t change much and aren’t necessary to deploy whenever you push out changes to an app.

To keep your deployments slim, you can add media to a folder on your projects root directory and then create a symbolic link to your folder during deployments by creating a deployment hook.

Step 1 - create new folder in project’s root directory

SSH into your server and navigate to your projects root directory, which should look something like:

/home/cleavr/mysite.com

In this directory, create a new folder. In my example, I’ll create a new folder for images.

mkdir images

Step 2 - populate your directory

A good way for this would be to use an SFTP client to connect to your server, and then you can copy your assets into it.

Step 3 - create a deployment hook to connect to directory

In the web app section > deployment hooks, create a new hook.

I titled mine connect shared resources and then added the following script:

SHARED_DIR=images
echo 'Checking the shared directory...'
if [ -d "{{ projectPath }}/$SHARED_DIR" ]; then
  echo 'Found an existing shared directory.'
else
  mkdir -p "{{ projectPath }}/$SHARED_DIR"
  chown -R {{ username }}:www-data "{{ projectPath }}/$SHARED_DIR"
  chmod -R 775 "{{ projectPath }}/$SHARED_DIR"
fi

# remove the storage folder if exists
if [ -d "{{ releasePath }}/$SHARED_DIR" ]; then
    echo "Shared directory {{ releasePath }}/$SHARED_DIR is pushed. To make data persists across deployments, removing this directory and linking it to {{ projectPath }}/$SHARED_DIR instead."
    rm -rf "{{ releasePath }}/$SHARED_DIR"
fi

echo 'Linking the shared folder...'
ln -s "{{ projectPath }}/$SHARED_DIR" "{{ releasePath }}/$SHARED_DIR"

Swap out “images” in SHARED_DIR=images with whatever you named your new directory in step 1.

After you create your new deployment hook, be sure to add it in the order you want it to run. I just added mine directory before the Active New Deployment step.

Step 4 - deploy!

Once deployed, the new deployment hook will link up the shared assets folder and you should be good to go! :rocket:

3 Likes