deployer/recipe/common.php

210 lines
5.1 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
2021-10-09 17:54:16 +02:00
require __DIR__ . '/provision.php';
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
2021-10-18 18:29:14 +02:00
use Deployer\Exception\ConfigurationException;
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
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
2021-10-19 22:47:13 +02:00
* run('echo $KEY', env: ['KEY' => 'over']);
2020-10-11 16:04:44 +02:00
* ```
*/
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);
2021-10-18 18:29:14 +02:00
/**
* The deploy path.
*
* For example can be set for a bunch of host once as:
* ```php
* set('deploy_path', '~/{{alias}}');
* ```
*/
set('deploy_path', function () {
throw new ConfigurationException('Please, specify `deploy_path`.');
});
/**
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');
2021-10-18 18:29:14 +02:00
// Path to the `php` bin.
2016-11-05 17:50:18 +07:00
set('bin/php', function () {
2021-10-12 22:46:34 +02:00
if (currentHost()->hasOwn('php_version')) {
return '/usr/bin/php{{php_version}}';
}
2021-11-05 15:23:34 +01:00
return which('php');
});
2016-11-19 15:13:32 +07:00
2021-10-18 18:29:14 +02:00
// Path to the `git` bin.
2016-11-05 17:50:18 +07:00
set('bin/git', function () {
2021-11-05 15:23:34 +01:00
return which('git');
});
2016-11-19 15:13:32 +07:00
2021-10-18 18:29:14 +02:00
// Should {{bin/symlink}} use `--relative` option or not. Will detect
// automatically.
set('use_relative_symlink', function () {
return commandSupportsOption('ln', '--relative');
});
// Path to the `ln` bin. With predefined options `-nfs`.
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;
});
2021-10-11 22:06:35 +02:00
desc('Prepare a new release');
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
]);
2021-10-11 22:06:35 +02:00
desc('Publish the release');
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.
*/
2021-10-11 22:06:22 +02:00
desc('Show application logs');
task('logs:app', function () {
2020-04-25 23:00:08 +03:00
if (!has('log_files')) {
warning("Please, specify \"log_files\" option.");
return;
}
2020-10-09 01:35:42 +02:00
cd('{{current_path}}');
2020-04-25 23:00:08 +03:00
run('tail -f {{log_files}}');
2021-10-11 22:06:22 +02:00
})->verbose();