Skip to content

Instantly share code, notes, and snippets.

@d-simon
Created August 12, 2020 12:23
Show Gist options
  • Save d-simon/cf36c8d6c18ba3c0509439e5a09d3ed5 to your computer and use it in GitHub Desktop.
Save d-simon/cf36c8d6c18ba3c0509439e5a09d3ed5 to your computer and use it in GitHub Desktop.
Deployer
{
"name": "organization/project",
"description": "Project",
"require": {
},
"scripts": {
"deploy": [
"@php vendor/bin/dep deploy"
]
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist"
},
"require-dev": {
"deployer/deployer": "^6.4"
}
}
<?php
namespace Deployer;
require 'vendor/symfony/dotenv/Dotenv.php';
require 'recipe/common.php';
// Project name
set('application', 'appname');
set('default_stage', 'production');
set('keep_releases', 7);
set('ssh_multiplexing', true);
set('http_user', 'www-data');
set('writable_mode', 'chmod');
// Project repository
set('repository', 'git@github.com:organisation/reponame.git');
// [Optional] Allocate tty for git clone. Default value is false.
set('git_tty', true);
// Shared files/dirs between deploys
set('shared_files', ['.env', 'config/license.key']);
set('shared_dirs', ['public/assets', 'public/imager', 'public/cpresources', 'storage/logs', 'storage/backups', 'storage/rebrand', 'storage/userphotos', ]);
// Writable dirs by web server
set('writable_dirs', ['public/assets', 'public/imager', 'public/cpresources', 'storage', 'storage/logs', 'storage/backups', 'storage/rebrand', 'storage/userphotos', 'storage/runtime']);
// Do not share anonymous data
set('allow_anonymous_stats', false);
host('myprojecthost.ch')
->user('username')
->forwardAgent(true)
// ->port(2121)
->stage('production')
->set('branch', 'master')
->set('deploy_path', '/www.myprojecthost.ch');
host('staging.myprojecthost.ch')
->user('username')
->forwardAgent(true)
// ->port(2121)
->stage('staging')
->set('branch', 'staging')
->set('deploy_path', '/staging.myprojecthost.ch');
set('mysql', function () {
return array(
'host' => 'localhost',
'port' => 3306,
'schema' => getenv('DB_NAME'),
'username' => getenv('DB_USER'),
'password' => getenv('DB_PASSWORD'),
'dump_file' => 'db_dump_{{release_name}}.sql',
'destination' => '{{deploy_path}}/shared/backups',
'dump_options' => array('--skip-comments'),
);
});
// Craft CMS
task('craft:migrate', 'cd {{release_path}} && {{bin/composer}} run craft-migrate');
task('craft:project_config', 'cd {{release_path}} && {{bin/composer}} run craft-project-config');
task('craft:clear_caches', 'cd {{release_path}} && {{bin/composer}} run craft-clear-caches');
desc('Back up database');
task('db:backup', function () {
$config = get('mysql');
$host = $config['host'];
$port = $config['port'];
$schema = $config['schema'];
$username = $config['username'];
$password = $config['password'];
$destination = $config['destination'];
$file = $config['dump_file'];
$opts = implode(' ', $config['dump_options']);
run("mkdir -p {$destination}");
run("mysqldump -h {$host} -P {$port} -u {$username} -p{$password} {$opts} {$schema} > {$destination}/{$file}");
});
desc('Craft CMS: Install Default');
task('craft:install', './craft install --interactive=0 --email="dev+craft-admin@dezentrum.ch" --username="admin" --password="secret" --siteName="{{application}}"');
// Build NPM
task('build:install', function () {
runLocally('npm install');
}); // "npm ci" is available from npm 5.7.1
task('build:build', function () {
runLocally('npm run production');
});
task('build:upload', function () {
upload('public/build', '{{release_path}}/public');
});
task('testing:run', function () {
run('ls', ['tty' => false]);
});
task('deploy', [
'deploy:info',
'deploy:prepare',
'deploy:lock',
'deploy:release',
'deploy:update_code',
'deploy:shared',
'deploy:writable',
// 'db:backup',
'build:install',
'build:build',
// 'build:cleanup',
'build:upload',
'deploy:vendors',
'deploy:clear_paths',
'deploy:symlink',
'craft:install',
'craft:migrate',
'craft:project_config',
'craft:clear_caches',
'deploy:unlock',
'cleanup',
'success',
]);
task('load:env-vars', function () {
$environment = run('cat {{deploy_path}}/shared/.env');
$dotenv = new \Symfony\Component\Dotenv\Dotenv();
$dotenv->populate($dotenv->parse($environment));
});
before('deploy', 'load:env-vars');
before('rollback', 'load:env-vars');
// [Optional] If deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment