Skip to content

Commit

Permalink
Merge pull request #164 from lorisleiva/reload-php-fpm-after-rollback
Browse files Browse the repository at this point in the history
Add rollback hook
  • Loading branch information
lorisleiva committed Feb 29, 2020
2 parents 57a453f + fbe1293 commit 5a2325c
Show file tree
Hide file tree
Showing 20 changed files with 52 additions and 25 deletions.
5 changes: 3 additions & 2 deletions docs/how-to-reload-fpm.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# How to reload php-fpm?

In order to reload php-fpm after each deployment, you will need to add the `fpm:reload` task at the end of your deployment flow. You also have to set up which version of php-fpm you are using.
In order to reload php-fpm after each deployment, you will need to add the `fpm:reload` task at the end of your deployment flow (including, if necessary, rolling back to a previous version). You also have to set up which version of php-fpm you are using.

```php
// config/deploy.php
Expand All @@ -10,6 +10,7 @@ In order to reload php-fpm after each deployment, you will need to add the `fpm:
],
'hooks' => [
'done' => ['fpm:reload'],
'rollback' => ['fpm:reload'],
],
```

Expand All @@ -21,4 +22,4 @@ If you wish to customize the command executed by `fpm:reload`, you can override
'options' => [
'php_fpm_command' => 'echo "" | sudo -S /usr/sbin/service {{php_fpm_service}} reload',
],
```
```
5 changes: 5 additions & 0 deletions docs/overview-getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ Before starting you first deployment, you should go check your `config/deploy.ph

// Deployment failed. This can happen at any point of the deployment.
'fail' => [],

// After a deployment has been rolled back.
'rollback' => [
'fpm:reload',
],
],
```

Expand Down
1 change: 1 addition & 0 deletions src/LaravelDeployer/ConfigFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ConfigFile implements Arrayable
'hooks.done',
'hooks.success',
'hooks.fail',
'hooks.rollback',
'options',
'hosts',
'localhost',
Expand Down
12 changes: 7 additions & 5 deletions src/LaravelDeployer/ConfigFileBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ class ConfigFileBuilder
'default' => 'basic',
'strategies' => [],
'hooks' => [
'start' => [],
'build' => [],
'ready' => [],
'done' => [],
'start' => [],
'build' => [],
'ready' => [],
'done' => [],
'success' => [],
'fail' => [],
'fail' => [],
'rollback' => [],
],
'options' => [
'application' => "env('APP_NAME', 'Laravel')",
Expand Down Expand Up @@ -163,6 +164,7 @@ public function useForge($phpVersion = self::DEFAULT_PHP_VERSION)
public function reloadFpm($phpVersion = self::DEFAULT_PHP_VERSION)
{
$this->add('hooks.done', 'fpm:reload');
$this->add('hooks.rollback', 'fpm:reload');
$this->set('options.php_fpm_service', "php$phpVersion-fpm");

return $this;
Expand Down
3 changes: 3 additions & 0 deletions src/LaravelDeployer/stubs/config.stub
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ return [

// Deployment failed.
'fail' => {{hooks.fail}},

// After a deployment has been rolled back.
'rollback' => {{hooks.rollback}},
],

/*
Expand Down
3 changes: 3 additions & 0 deletions src/recipe/laravel-deployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,6 @@
// Unlock when deployment fails.
fail('deploy', 'deploy:failed');
after('deploy:failed', 'deploy:unlock');

// Add rollback hook.
after('rollback', 'hook:rollback');
2 changes: 1 addition & 1 deletion src/task/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@
writeln('<comment>To speed up composer installation setup "unzip" command with PHP zip extension https://goo.gl/sxzFcD</comment>');
}
run('cd {{release_path}} && {{bin/composer}} {{composer_options}}');
});
});
2 changes: 1 addition & 1 deletion src/task/defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@
preg_match_all('/(\d+\.?)+/', $result, $matches);
$version = $matches[0][0] ?? 5.5;
return $version;
});
});
2 changes: 1 addition & 1 deletion src/task/fpm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
desc('Reload the php-fpm service');
task('fpm:reload', function () {
run('{{php_fpm_command}}');
});
});
2 changes: 1 addition & 1 deletion src/task/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ function copyShared($from, $to)
run("rsync --ignore-existing $from/$file $to/$file");
}
}
}
}
3 changes: 2 additions & 1 deletion src/task/hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
task('hook:start', function() {})->shallow()->setPrivate();
task('hook:build', function() {})->shallow()->setPrivate();
task('hook:ready', function() {})->shallow()->setPrivate();
task('hook:done', function() {})->shallow()->setPrivate();
task('hook:done', function() {})->shallow()->setPrivate();
task('hook:rollback', function() {})->shallow()->setPrivate();
2 changes: 1 addition & 1 deletion src/task/logs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
desc('Read logs from a given host');
task('logs', function() {
writeln(run('cd {{deploy_path}}/current && {{log_command}}'));
})->shallow();
})->shallow();
2 changes: 1 addition & 1 deletion src/task/npm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
task('npm:development', '{{bin/npm}} run development');

desc('Execute npm run production');
task('npm:production', '{{bin/npm}} run production');
task('npm:production', '{{bin/npm}} run production');
4 changes: 4 additions & 0 deletions tests/Features/DeployInitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function it_generates_a_minimal_config_file()
"done" => [],
"fail" => [],
"success" => [],
"rollback" => [],
],
"options" => [
"application" => "Laravel",
Expand Down Expand Up @@ -86,6 +87,9 @@ function it_generates_a_full_config_file_with_forge_defaults()
],
"fail" => [],
"success" => [],
"rollback" => [
'fpm:reload',
],
],
"options" => [
"application" => "Laravel",
Expand Down
1 change: 1 addition & 0 deletions tests/Features/DeployInitWithLumenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function it_generates_a_config_file_without_artisan_tasks_not_supported_by_lumen
"done" => [],
"fail" => [],
"success" => [],
"rollback" => [],
],
"options" => [
"application" => "Laravel",
Expand Down
5 changes: 3 additions & 2 deletions tests/Features/DeployRollbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class DeployRollbackTest extends DeploymentTestCase
protected $configs = 'basic';

/** @test */
function a_rollback_with_no_previous_release_should_do_nothing_but_warn_user()
function a_rollback_with_no_previous_release_should_do_nothing_but_warn_the_user()
{
$output = $this->artisan('deploy:rollback');

Expand Down Expand Up @@ -42,9 +42,10 @@ function a_rollback_should_symlink_to_the_previous_release()

/* Rollback */

$this->artisan('deploy:rollback');
$output = $this->artisan('deploy:rollback');

$this->assertSuccessfulDeployment();
$this->assertServerHas('unicorn.txt');
$this->assertStringContainsString('Executing task fpm:reload', $output);
}
}
1 change: 1 addition & 0 deletions tests/Unit/ConfigFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function it_can_render_a_config_file_that_is_equivalent_to_its_content()
'done' => [],
'fail' => [],
'success' => [],
'rollback' => [],
],
'options' => [
'application' => "env('APP_NAME', 'Laravel')",
Expand Down
14 changes: 8 additions & 6 deletions tests/Unit/DeployFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,13 @@ function it_adds_hooks()
{
$deployFile = (string) new DeployFile([
'hooks' => [
'start' => ['slack:notify'],
'build' => ['npm:install', 'npm:production'],
'ready' => ['artisan:cache:clear', 'artisan:migrate'],
'done' => ['fpm:reload'],
'fail' => ['slack:notify:failure'],
'success' => ['slack:notify:success'],
'start' => ['slack:notify'],
'build' => ['npm:install', 'npm:production'],
'ready' => ['artisan:cache:clear', 'artisan:migrate'],
'done' => ['fpm:reload'],
'fail' => ['slack:notify:failure'],
'success' => ['slack:notify:success'],
'rollback' => ['fpm:reload'],
]
]);

Expand All @@ -245,6 +246,7 @@ function it_adds_hooks()
after('hook:done', 'fpm:reload');
after('deploy:failed', 'slack:notify:failure');
after('success', 'slack:notify:success');
after('hook:rollback', 'fpm:reload');
EOD
, $deployFile);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/configs/basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
'artisan:config:cache',
'artisan:optimize',
],
'rollback' => [
'fpm:reload',
],
],

'include' => [
Expand Down
5 changes: 2 additions & 3 deletions tests/fixtures/recipes/mocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
run('echo "compiled app.js" > {{release_path}}/public/js/app.js');
});

// Mock vendors
// Mock other tasks.
task('deploy:vendors', function() {});

// Mock artisan commands
task('fpm:reload', function() {});
task('artisan:storage:link', function() {});
task('artisan:view:clear', function() {});
task('artisan:view:cache', function() {});
Expand Down

0 comments on commit 5a2325c

Please sign in to comment.