CloudWays has a super basic introduction to using Envoyer with CloudWays – but disapointingly it doesn’t actually cover any of the things specific to CloudWays. It’s just a generic introduction to a super basic deployment over SSH.
I’ve spent the best part of today getting things running, and wanted to share what I’ve done – to hopefully to help someone else, and also to request feedback from others.
I like automation, I don’t want to have to be doing things manually. That’s the purpose of Envoyer too. When I trigger a deploy in Envoyer, I expect my production site to be fully updated – composer, compiled assets, caches cleared, database migrated.
A little disclaimer
This process has been my attempt to set things up according to best practice. For me, best practice means automating as much as possible to eliminate human error (i.e. me forgetting to run something).
I’m setting this up in August 2019 – if anything doesn’t work well I’ll come back and update this blog (hopefully!). Otherwise please treat this as a suggestion, rather than something tried and tested. Likewise – I’d love your feedback or experiences in the comments below.
Deployment Hook Positions
Here are the Deployment Hooks I have setup, the content for each is in the table after. This one just shows where I’ve positioned each.
Action | Before | After |
Clone New Release | Backup | – |
Install Composer Dependencies | Install NPM Dependencies, Build Assets | – |
Activate New Release | Rebuild Cache, Migrate Database | Reset App Permissions on CloudWays, Restart FPM on CloudWays |
Purge Old Releases | – | – |
Deployment Hook Content
Name | Run As | Script |
Backup | master | # Backup the current site, before doing anything. # Uses package: backpack/backupmanager cd {{ project }}/current php artisan backup:run |
Install NPM Dependencies | master | cd {{release}} npm install |
Build Assets | master | cd {{release}} npm run production |
Rebuild Cache | master | cd {{release}} php artisan clear-compiled php artisan cache:clear php artisan view:clear php artisan config:cache |
Migrate Database | master | cd {{release}} # Migrate the database without asking us # if we’re sure. php artisan migrate -n –force |
Reset App Permissions in CloudWays | master | CURL command (see blow). It was very long and messy to paste in this table. |
Restart FPM on CloudWays | master | # Envoyer can’t restart FPM with the CloudWays SSH user # but we can easily do it using the API. CURL command (see blow). It was very long and messy to paste in this table. |
Don’t forget to tick the “On Servers” box for each one, too.
Which user to deploy as?
One of the issues I’m trying to overcome is the occasional permissions issue I get in cache (especially template) files on CloudWays. Because I’m compiling my assets as the “master” user, but Apache is running them as the “application” user.
I started of trying to run everything as the application user. But that user cannot run npm properly (permissions issues creating ~/.npm).
I also tried to get the application user to build the assets and do the cache clearing – but again I was getting permissions issues and errors. I presume that’s something to do with the locked down users CloudWays configures.
So in the end – I’m running everything as the master user.
Using the CloudWays API
The CloudWays API is very simple to use, I used the Playground (don’t forget to Authourize with the button in the very top right) to generate the CURL call needed. Heads up: the Bearer header is not your API token, but is created when you authourise on the API Playground.
Grab the Server ID and Application ID by loading the CloudWays platform page and getting the numbers from the ID.
https://developers.cloudways.com/play/
CloudWays API Command to reset file permissions
Since I’m trying to avoid file permissions issues (caused by compiling things or clearing caches as the master user, but serving them as the application user) I opted to run CloudWays’s File Permission Re-setter tool after every deploy.
curl -X POST –header ‘Content-Type: application/x-www-form-urlencoded’ –header ‘Accept: application/json’ –header ‘Authorization: Bearer YOUR_API_TOKEN’ -d ‘server_id=YOUR_BEARER_ID&app_id=YOUR_APP_ID’ ‘https://api.cloudways.com/api/v1/app/manage/reset_permissions’
CloudWays API Command to restart FPM
Envoyer can’t restart FPM over SSH because CloudWays doens’t let us sudo. But we can do it with the API instead. Make sure you’ve got the correct PHP FPM version. Use the /service/get method to check what it should be.
curl -X POST –header ‘Content-Type: application/x-www-form-urlencoded’ –header ‘Accept: application/json’ –header ‘Authorization: Bearer YOUR_BEARER_TOKEN’ -d ‘server_id=YOUR_SERVER_ID&service=php7.3-fpm&state=restart’ ‘https://api.cloudways.com/api/v1/service/state’
Change Log
2019-10-08
- Changed page title to include Laravel 6 – which I’m now using.
- Moved “Build Assets” from “Before Activate New Release” to ” Before Install Composer Dependencies” to resolve “Mix Manifest not found” error.
- Moved “Install NPM Dependencies ” from “After Install Composer Dependencies” to “Before Install Composer Dependencies” because it has to come before “Build Assets”
Comments, Suggestions, Feedback
As mentioned above, this is a voyage of discovery for me. Hopefully it’ll run smoothly going forward. I’ll definitely be adding to it (I intend to setup the Laravel Job Queue at some point).
Please share below any experience or suggestions you have. I struggled to find any detailed articles addressing this so hope to create one.