Skip to content

Instantly share code, notes, and snippets.

@mtwalsh
Last active December 8, 2022 16:31
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtwalsh/fce3c4aa416996e5900e8ac9f471dd6c to your computer and use it in GitHub Desktop.
Save mtwalsh/fce3c4aa416996e5900e8ac9f471dd6c to your computer and use it in GitHub Desktop.
Deployer recipe for Craft CMS 3 projects.
<?php
namespace Deployer;
require 'recipe/common.php';
// Project name
set('application', 'enovate.co.uk');
// Project repository
set('repository', 'git@githosting.com:enovatedesign/project.git');
// Shared files/dirs between deploys
set('shared_files', [
'.env'
]);
set('shared_dirs', [
'storage'
]);
// Writable dirs by web server
set('writable_dirs', [
'storage',
'storage/runtime',
'storage/logs',
'storage/rebrand',
'public/cpresources'
]);
// Set the worker process user
set('http_user', 'worker');
// Set the default deploy environment to production
set('default_stage', 'production');
// Disable multiplexing
set('ssh_multiplexing', false);
// Tasks
// Upload build assets
task('upload', function () {
upload(__DIR__ . "/public/assets/", '{{release_path}}/public/assets/');
//upload(__DIR__ . "/public/service-worker.js", '{{release_path}}/public/service-worker.js');
});
desc('Execute migrations');
task('craft:migrate', function () {
run('{{release_path}}/craft migrate/up');
})->once();
// Hosts
// Production Server(s)
host('110.164.16.59', '110.164.16.34', '110.164.16.50')
->set('deploy_path', '/websites/{{application}}')
->set('branch', 'master')
->stage('production')
->user('someuser');
// Staging Server
host('192.168.16.59')
->set('deploy_path', '/websites/{{application}}')
->set('branch', 'develop')
->stage('staging')
->user('someuser');
// Group tasks
desc('Deploy your project');
task('deploy', [
'deploy:info',
'deploy:prepare',
'deploy:lock',
'deploy:release',
'deploy:update_code',
'upload', // Custom task to upload build assets
'deploy:shared',
'deploy:writable',
'deploy:vendors',
'deploy:clear_paths',
'deploy:symlink',
'deploy:unlock',
'cleanup',
'success'
]);
// [Optional] Run migrations
after('deploy:vendors', 'craft:migrate');
// [Optional] If deploy fails automatically unlock
after('deploy:failed', 'deploy:unlock');
// Run with '--parallel'
// dep deploy --parallel
@mtwalsh
Copy link
Author

mtwalsh commented Feb 4, 2021

@duikb00t You're welcome, glad it's useful.

@mandrasch
Copy link

mandrasch commented Jul 31, 2022

Thanks for providing this! 👏 👏

Just fiy:

Shortened it down to the following, but I'm not yet a task expert for deployer. Not sure if 100% correct ;-)

desc('Deploy your project');
task('deploy', [
    'deploy:prepare',
    'upload', // Custom task to upload build assets
    'deploy:vendors',
    'deploy:clear_paths',
    'deploy:publish',
 ]);

Currently trying this https://dev.to/mandrasch/statamic-meets-hetzner-cloud-ploi-and-deployer-2ko5 approach with DDEV + Craft + Deployer on ploi + Hetzner Cloud. I'm currently stuck with the following for the first time deployment? Changed php craft migrate/up to php craft up, but no success. 🤔

[production]  error  in deploy.php on line 59:
[production] run ~/train-to-lake.mandrasch.dev/releases/9/craft up
[production] err Error: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'traintolake.migrations' doesn't exist

(Repo: https://github.com/mandrasch/train-to-lake-craftcms, deploy.php here: https://github.com/mandrasch/train-to-lake-craftcms/blob/main/deploy.php)

Update: Needed to run php craft install/craft in release/xx/ folder after first (failed) deployment (via SSH on target server). Deployment works fine after installation. 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment