How do you run your migrations and clear your template cache on every deploy to Fortrabbit?
We’ve been pretty happy with Fortrabbit as a hosting service for CraftCMS. It has a nice git based workflow, where you can push to a remote and deploy. This is great for automated builds (we use CircleCI) so that developers can focus on development, not deploying.
Often, you want to run a command after deploying. It may be that you need migrations to run. Or you might need to clear the cache. If you add an entry to your fortrabbit..yml (full docs) you can run an arbitrary command after a deploy is successful. If that command fails (or times out, a deployment can only take 10 minutes or the process is killed) the deploy doesn’t occur.
So, to run our migrations, we had a fortrabbit.yml that looked like this:
post: craft migrate/up --interactive 0
However, we wanted to clear the view cache, which you can do via the command line. It looks like this:
craft cache/flush-all
You cannot run two command as a post command in fortrabbit, unfortunately. I tried two different methods, combining them on one line:
post: craft migrate/up --interactive 0 &&
craft cache/flush-all
and having two separate post commands.
post: craft migrate/up --interactive 0
post:
craft cache/flush-all
Neither worked. What did work, however, was to write a small php script:
<?php
echo shell_exec('php craft migrate/up --interactive 0');
echo shell_exec('php craft cache/flush-all');
?>
and then have that script called. The fortrabbit.yml file then looked like:
post: post.php
Echoing the results lets us see any issues that the php command. (More about the details of calling php from php.) This same strategy would work if you had to run multiple commands before deployment as well.