Deploy Statamic with Git and rsync

Published

Updated

Deploy Statamic on fortrabbit using Git for code and rsync for user-generated content, keeping your version control clean.

Get ready

Statamic runs locally, see install Statamic, and Git and rsync are familiar.

Setup GitHub at least once

To enable git deployments, init git and add GitHub as remote repo, with the Statamic CLI, a GUI or the GitHub CLI:

Configure Statamic for Git with rsync

To separate code from content, configure .gitignore at the project root to exclude user-generated files. Add the following folders to ensure only application code is version controlled:

…
# Exclude stuff you are creating from Git
/content
/users
/resources/blueprints
/resources/fieldsets
/resources/forms
/resources/users
/storage/forms
/public/assets
shell

This separates code from content, the approach we recommend. You'll need to run dedicated rsync commands to deploy and update user-generated content (see below). To deploy content with git instead, see content in git below.

Connect GitHub with fortrabbit

Push the code to the GitHub repository at least once. To connect your GitHub repo with an app on fortrabbit, you first need to install the fortrabbit GitHub app. In many cases, this done during boarding. See your account settings if GitHub is connected:

Create an app or connect an existing app to a Git repo.

Once connected, every git push deploys the new code.

Software template

Choosing Statamic as the software applies the software template, which sets the root path, the build and post-deploy commands and the ENV vars an app needs to reach its database.

Statamic 6, 5, 4, 3

DeploymentValue
Root pathpublic
Deploy strategyreplace
Excluded paths/storage/node_modules/content/users/resources/blueprints/resources/fieldsets/resources/forms/resources/users/public/assets
Build commandscomposer install --no-dev --prefer-dist --no-interaction --no-ansi --no-progressnpm installnpm run build
Post-deploy commandsmkdir -p storage/framework/{sessions,views,cache}php artisan migrate --force
Environment variablesValue
APP_DEBUGfalse
APP_ENVproduction
APP_KEY!RANDOM_KEY!
APP_URLhttps://${FORTRABBIT_MAIN_DOMAIN}
LOG_CHANNELstack
LOG_STACKstderr
Database environment variablesValue
DB_CONNECTION${FORTRABBIT_DB_TYPE}
DB_DATABASE${FORTRABBIT_DB_NAME}
DB_HOST${FORTRABBIT_DB_HOST}
DB_PASSWORD${FORTRABBIT_DB_PASSWORD}
DB_PORT${FORTRABBIT_DB_PORT}
DB_USERNAME${FORTRABBIT_DB_USER}

Exclude the content folders

The template deploys with the replace strategy: every deployment deletes the files on the environment that are not in the new deploy package. Folders listed as exclude patterns stay untouched. The template excludes the content folders from the .gitignore above:

/storage
/node_modules
/content
/users
/resources/blueprints
/resources/fieldsets
/resources/forms
/resources/users
/public/assets
raw

A git deployment keeps content uploaded with rsync, and entries, users and assets created in the control panel. Git does not update an excluded folder, so its content goes up by rsync only.

Environments created with an older template exclude only /storage and /node_modules, and a git deployment deletes the content folders there. Check the exclude patterns and add the missing folders before the first content upload:

Content in git

To deploy content with git as well, keep the content folders in the repository and remove them from the exclude patterns. Each deployment then replaces the content on the environment with the state in git: entries, users and assets created in the control panel are deleted. Edit content locally only, or commit the changes made online before the next deployment. The merge strategy keeps files that are not in the repository, but also keeps files deleted from it.

Synchronize content with rsync

As mentioned above, deployment of your code base (templates and configuration) and dependencies (Statamic and Composer) is done via Git deployment. Deploying the content is a separate step. We recommend using rsync to upload or download new content to and from your remote fortrabbit app (see also our rsync article). On your local computer in the terminal in the Statamic project folder execute:

# SYNC UP: from local to remote
$ rsync -avR ./content ./users ./resources/blueprints ./resources/fieldsets ./resources/forms ./resources/users ./storage/forms ./public/assets {{app-env-id}}@ssh.{{region}}.frbit.app:./
shell

(You may not have all these folders on your local system: Only include folders which you do have.)

Statamic watches content files for changes only in local environments. On fortrabbit, refresh its content cache after each upload, or new entries do not show up:

$ ssh {{app-env-id}}@ssh.{{region}}.frbit.app 'php please stache:refresh'
shell

It works also the other way around. For example, if you have some edits done online and want them to be reflected in your local development environment:

# SYNC DOWN: from remote to local
rsync -av '{{app-env-id}}@ssh.{{region}}.frbit.app:./content' ./
rsync -av '{{app-env-id}}@ssh.{{region}}.frbit.app:./users' ./
rsync -av '{{app-env-id}}@ssh.{{region}}.frbit.app:./resources/blueprints' ./resources/
rsync -av '{{app-env-id}}@ssh.{{region}}.frbit.app:./resources/fieldsets' ./resources/
rsync -av '{{app-env-id}}@ssh.{{region}}.frbit.app:./resources/forms' ./resources/
rsync -av '{{app-env-id}}@ssh.{{region}}.frbit.app:./resources/users' ./resources/
rsync -av '{{app-env-id}}@ssh.{{region}}.frbit.app:./storage/forms' ./storage/
rsync -av '{{app-env-id}}@ssh.{{region}}.frbit.app:./public/assets' ./public/
shell

Written by humans and AI in collaboration.

AI use & editorial processEdit on GitHub ↗