Oct 23, 2020 3 min read
Preventing Useless Deployments with Statamic 3’s Git Integration and Forge’s Auto-Deployments
I've loved every Statamic update … except for one.
Manager, Digital Product Delivery
Statamic 3. It’s a wondrous thing. I love pretty much every change I’ve encountered from v2 to v3.
Except for one thing.
In v2, I would move all content, users, assets, and I think something else(?) into a folder called
gitignore it. Basically all users and their generated content. This kept the repo “clean” (free of user-generated content; commit history only includes meaningful changes to the actual code; other mostly-irrelevant-for-this-post stuff), and I worked with Jim-Jam (James Blair) over several years to develop a nice workflow, complete with simple scripts to make the whole thing really easy and convenient. It’s diametrically opposed to the Spock approach. I still absolutely prefer this method.
When I started working with v3, I discovered that content and configuration can get mixed together in a single file. This prevented me from using my preferred approach, so I decided to try the more-popular approach of committing content et al. to the repo (🤮) using the first-party Git integration. I don’t like it, but I can live with it for now.
One thing that continued to bug me though was that every time a user did anything in the CP, Statamic would auto-commit and push as expected, but then Forge would see this new change and deploy it right back to production.
This meant changes made in production were sent to Forge and Forge sent them right back. Not a huge deal, but it also meant the deploy script would run and we’ve all had our fair share of issues with package managers and build scripts that take hours to debug. Not to mention the irrational fear we have as devs that someone will access the site at the exact moment frontend assets are being rewritten and we’ll be caught with our pants down.
Thankfully, Forge provides environment variables and Statamic empowers devs to customize the Git integration. I configured Statamic to prepend each commit message with
[AUTO] and Forge to check for that string in commit messages. If it finds it, Forge will stop execution with a successful exit status.
Here’s what my
config/statamic/git.php looks like:
…and my deploy script in Forge:
With this in place, you’ll still see Forge’s deploying state (deploy button color muted, text changed to deploying, and a little spinner), but it won’t make it all the way through. It’ll stop nice and early and the latest deployment log will just say
"AUTO-COMMITTED ON PRODUCTION. NOTHING TO DEPLOY."👍