deployer/recipe/common.php

220 lines
5.4 KiB
PHP
Raw Normal View History

<?php
2016-01-11 14:00:45 +07:00
namespace Deployer;
2016-11-13 21:56:50 +07:00
2020-04-25 23:00:08 +03:00
require __DIR__ . '/deploy/check_remote.php';
require __DIR__ . '/deploy/cleanup.php';
require __DIR__ . '/deploy/clear_paths.php';
require __DIR__ . '/deploy/copy_dirs.php';
require __DIR__ . '/deploy/info.php';
2016-11-19 15:16:25 +07:00
require __DIR__ . '/deploy/lock.php';
2020-10-11 00:08:50 +02:00
require __DIR__ . '/deploy/push.php';
2016-11-19 15:16:25 +07:00
require __DIR__ . '/deploy/release.php';
2020-04-25 23:00:08 +03:00
require __DIR__ . '/deploy/rollback.php';
require __DIR__ . '/deploy/setup.php';
2016-11-19 15:16:25 +07:00
require __DIR__ . '/deploy/shared.php';
require __DIR__ . '/deploy/symlink.php';
2020-04-25 23:00:08 +03:00
require __DIR__ . '/deploy/update_code.php';
require __DIR__ . '/deploy/vendors.php';
require __DIR__ . '/deploy/writable.php';
2016-01-11 14:00:45 +07:00
2020-04-25 23:00:08 +03:00
use Deployer\Exception\RunException;
2016-11-05 12:22:56 +07:00
use Symfony\Component\Console\Input\InputOption;
2020-04-25 23:00:08 +03:00
use Symfony\Component\Console\Output\Output;
2016-11-05 12:22:56 +07:00
2020-10-25 16:00:05 +01:00
add('recipes', ['common']);
2020-10-11 16:35:59 +02:00
// Name of current user who is running deploy.
// If not set will try automatically get git user name,
// otherwise output of `whoami` command.
2017-08-16 11:46:05 +03:00
set('user', function () {
2020-04-25 23:00:08 +03:00
if (getenv('CI') !== false) {
return 'ci';
}
2017-08-16 11:46:05 +03:00
try {
return runLocally('git config --get user.name');
2020-04-25 23:00:08 +03:00
} catch (RunException $exception) {
try {
return runLocally('whoami');
} catch (RunException $exception) {
return 'no_user';
}
2017-08-16 11:46:05 +03:00
}
});
2017-03-14 20:16:54 +07:00
2020-10-11 16:35:59 +02:00
// Number of releases to preserve in releases folder.
2021-09-23 02:09:55 +02:00
set('keep_releases', 10);
2016-11-05 17:50:18 +07:00
2020-10-11 16:35:59 +02:00
// Repository to deploy.
set('repository', '');
2016-11-05 17:50:18 +07:00
2020-10-11 16:35:59 +02:00
// List of dirs what will be shared between releases.
// Each release will have symlink to those dirs stored in {{deploy_path}}/shared dir.
// ```php
// set('shared_dirs', ['storage']);
// ```
2015-02-15 15:28:44 +03:00
set('shared_dirs', []);
2020-10-11 16:35:59 +02:00
// List of files what will be shared between releases.
// Each release will have symlink to those files stored in {{deploy_path}}/shared dir.
// ```php
// set('shared_files', ['.env']);
// ```
2015-02-15 15:28:44 +03:00
set('shared_files', []);
2016-11-05 17:50:18 +07:00
2020-10-11 16:35:59 +02:00
// List of dirs to copy between releases.
// For example you can copy `node_modules` to speedup npm install.
2015-07-05 14:53:49 +02:00
set('copy_dirs', []);
2016-11-05 17:50:18 +07:00
2020-10-11 16:35:59 +02:00
// List of paths to remove from {{release_path}}.
set('clear_paths', []);
2020-10-11 16:35:59 +02:00
// Use sudo for deploy:clear_path task?
set('clear_use_sudo', false);
2017-03-26 13:16:12 +07:00
2017-03-13 15:13:34 +07:00
set('use_relative_symlink', function () {
return commandSupportsOption('ln', '--relative');
2017-03-13 15:13:34 +07:00
});
set('use_atomic_symlink', function () {
return commandSupportsOption('mv', '--no-target-directory');
2017-03-13 15:13:34 +07:00
});
2021-08-31 20:15:06 +02:00
// Default timeout for `run()` and `runLocally()` functions.
//
2020-11-05 19:43:04 +01:00
// Set to `null` to disable timeout.
set('default_timeout', 300);
2020-10-11 16:04:44 +02:00
/**
* Remote environment variables.
* ```php
* set('env', [
* 'KEY' => 'something',
* ]);
* ```
*
* It is possible to override it per `run()` call.
*
* ```php
* run('echo $KEY', ['env' => ['KEY' => 'over']]
* ```
*/
set('env', []);
2020-10-11 16:23:40 +02:00
/**
* Path to `.env` file which will be used as environment variables for each command per `run()`.
*
* ```php
* set('dotenv', '{{current_path}}/.env');
* ```
*/
set('dotenv', false);
/**
2020-10-11 16:35:59 +02:00
* Return current release path. Default to {{deploy_path}}/`current`.
* ```php
* set('current_path', '/var/public_html');
* ```
*/
2020-10-09 01:35:42 +02:00
set('current_path', '{{deploy_path}}/current');
2020-10-11 16:35:59 +02:00
// Custom php bin of remote host.
2016-11-05 17:50:18 +07:00
set('bin/php', function () {
return locateBinaryPath('php');
});
2016-11-19 15:13:32 +07:00
2020-10-11 16:35:59 +02:00
// Custom git bin of remote host.
2016-11-05 17:50:18 +07:00
set('bin/git', function () {
return locateBinaryPath('git');
});
2016-11-19 15:13:32 +07:00
2020-10-11 16:35:59 +02:00
// Custom ln bin of remote host.
2016-11-05 17:50:18 +07:00
set('bin/symlink', function () {
2017-03-13 15:13:34 +07:00
return get('use_relative_symlink') ? 'ln -nfs --relative' : 'ln -nfs';
2016-11-05 13:40:12 +07:00
});
2020-10-02 00:11:13 +02:00
// Path to a file which will store temp script with sudo password.
// Defaults to `.dep/sudo_pass`. This script is only temporary and will be deleted after
// sudo command executed.
2020-04-25 23:00:08 +03:00
set('sudo_askpass', function () {
if (test('[ -d {{deploy_path}}/.dep ]')) {
return '{{deploy_path}}/.dep/sudo_pass';
} else {
return '/tmp/dep_sudo_pass';
}
});
2017-03-14 20:16:54 +07:00
2017-07-03 16:16:54 +09:00
option('tag', null, InputOption::VALUE_REQUIRED, 'Tag to deploy');
option('revision', null, InputOption::VALUE_REQUIRED, 'Revision to deploy');
option('branch', null, InputOption::VALUE_REQUIRED, 'Branch to deploy');
2015-01-03 00:04:00 +03:00
2021-09-24 00:11:45 +02:00
// The deploy target: a branch, a tag or a revision.
set('target', function () {
$target = '';
$branch = get('branch');
if (!empty($branch)) {
$target = $branch;
}
if (input()->hasOption('tag') && !empty(input()->getOption('tag'))) {
$target = input()->getOption('tag');
}
if (input()->hasOption('revision') && !empty(input()->getOption('revision'))) {
$target = input()->getOption('revision');
}
if (empty($target)) {
$target = "HEAD";
}
return $target;
});
2020-04-25 23:00:08 +03:00
task('deploy:prepare', [
'deploy:info',
'deploy:setup',
'deploy:lock',
'deploy:release',
'deploy:update_code',
2020-10-07 23:55:41 +02:00
'deploy:shared',
'deploy:writable',
2020-04-25 23:00:08 +03:00
]);
task('deploy:publish', [
'deploy:symlink',
'deploy:unlock',
'deploy:cleanup',
'deploy:success',
]);
2015-02-25 12:55:56 +03:00
/**
2020-10-11 16:35:59 +02:00
* Prints success message
2015-02-25 12:55:56 +03:00
*/
2020-04-25 23:00:08 +03:00
task('deploy:success', function () {
2020-07-26 22:35:08 +03:00
info('successfully deployed!');
2017-08-11 17:39:36 +03:00
})
->shallow()
2020-04-25 23:00:08 +03:00
->hidden();
2016-11-13 18:21:18 +07:00
/**
2020-10-11 16:35:59 +02:00
* Hook on deploy failure.
2016-11-13 18:21:18 +07:00
*/
task('deploy:failed', function () {
2020-04-25 23:00:08 +03:00
})->hidden();
2021-03-14 08:44:14 +01:00
fail('deploy', 'deploy:failed');
2020-04-25 23:00:08 +03:00
/**
* Follow latest application logs.
*/
desc('Follow latest application logs.');
task('logs', function () {
if (!has('log_files')) {
warning("Please, specify \"log_files\" option.");
return;
}
if (output()->getVerbosity() === Output::VERBOSITY_NORMAL) {
output()->setVerbosity(Output::VERBOSITY_VERBOSE);
}
2020-10-09 01:35:42 +02:00
cd('{{current_path}}');
2020-04-25 23:00:08 +03:00
run('tail -f {{log_files}}');
});