Skip to content

Commit 5a2325c

Browse files
authored
Merge pull request #164 from lorisleiva/reload-php-fpm-after-rollback
Add rollback hook
2 parents 57a453f + fbe1293 commit 5a2325c

20 files changed

+52
-25
lines changed

docs/how-to-reload-fpm.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# How to reload php-fpm?
22

3-
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.
3+
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.
44

55
```php
66
// config/deploy.php
@@ -10,6 +10,7 @@ In order to reload php-fpm after each deployment, you will need to add the `fpm:
1010
],
1111
'hooks' => [
1212
'done' => ['fpm:reload'],
13+
'rollback' => ['fpm:reload'],
1314
],
1415
```
1516

@@ -21,4 +22,4 @@ If you wish to customize the command executed by `fpm:reload`, you can override
2122
'options' => [
2223
'php_fpm_command' => 'echo "" | sudo -S /usr/sbin/service {{php_fpm_service}} reload',
2324
],
24-
```
25+
```

docs/overview-getting-started.md

+5
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ Before starting you first deployment, you should go check your `config/deploy.ph
109109

110110
// Deployment failed. This can happen at any point of the deployment.
111111
'fail' => [],
112+
113+
// After a deployment has been rolled back.
114+
'rollback' => [
115+
'fpm:reload',
116+
],
112117
],
113118
```
114119

src/LaravelDeployer/ConfigFile.php

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class ConfigFile implements Arrayable
2020
'hooks.done',
2121
'hooks.success',
2222
'hooks.fail',
23+
'hooks.rollback',
2324
'options',
2425
'hosts',
2526
'localhost',

src/LaravelDeployer/ConfigFileBuilder.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ class ConfigFileBuilder
2424
'default' => 'basic',
2525
'strategies' => [],
2626
'hooks' => [
27-
'start' => [],
28-
'build' => [],
29-
'ready' => [],
30-
'done' => [],
27+
'start' => [],
28+
'build' => [],
29+
'ready' => [],
30+
'done' => [],
3131
'success' => [],
32-
'fail' => [],
32+
'fail' => [],
33+
'rollback' => [],
3334
],
3435
'options' => [
3536
'application' => "env('APP_NAME', 'Laravel')",
@@ -163,6 +164,7 @@ public function useForge($phpVersion = self::DEFAULT_PHP_VERSION)
163164
public function reloadFpm($phpVersion = self::DEFAULT_PHP_VERSION)
164165
{
165166
$this->add('hooks.done', 'fpm:reload');
167+
$this->add('hooks.rollback', 'fpm:reload');
166168
$this->set('options.php_fpm_service', "php$phpVersion-fpm");
167169

168170
return $this;

src/LaravelDeployer/stubs/config.stub

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ return [
5959

6060
// Deployment failed.
6161
'fail' => {{hooks.fail}},
62+
63+
// After a deployment has been rolled back.
64+
'rollback' => {{hooks.rollback}},
6265
],
6366

6467
/*

src/recipe/laravel-deployer.php

+3
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@
6868
// Unlock when deployment fails.
6969
fail('deploy', 'deploy:failed');
7070
after('deploy:failed', 'deploy:unlock');
71+
72+
// Add rollback hook.
73+
after('rollback', 'hook:rollback');

src/task/common.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,4 @@
8888
writeln('<comment>To speed up composer installation setup "unzip" command with PHP zip extension https://goo.gl/sxzFcD</comment>');
8989
}
9090
run('cd {{release_path}} && {{bin/composer}} {{composer_options}}');
91-
});
91+
});

src/task/defaults.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@
3737
preg_match_all('/(\d+\.?)+/', $result, $matches);
3838
$version = $matches[0][0] ?? 5.5;
3939
return $version;
40-
});
40+
});

src/task/fpm.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
desc('Reload the php-fpm service');
99
task('fpm:reload', function () {
1010
run('{{php_fpm_command}}');
11-
});
11+
});

src/task/helpers.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@ function copyShared($from, $to)
7171
run("rsync --ignore-existing $from/$file $to/$file");
7272
}
7373
}
74-
}
74+
}

src/task/hook.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
task('hook:start', function() {})->shallow()->setPrivate();
66
task('hook:build', function() {})->shallow()->setPrivate();
77
task('hook:ready', function() {})->shallow()->setPrivate();
8-
task('hook:done', function() {})->shallow()->setPrivate();
8+
task('hook:done', function() {})->shallow()->setPrivate();
9+
task('hook:rollback', function() {})->shallow()->setPrivate();

src/task/logs.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
desc('Read logs from a given host');
99
task('logs', function() {
1010
writeln(run('cd {{deploy_path}}/current && {{log_command}}'));
11-
})->shallow();
11+
})->shallow();

src/task/npm.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
task('npm:development', '{{bin/npm}} run development');
99

1010
desc('Execute npm run production');
11-
task('npm:production', '{{bin/npm}} run production');
11+
task('npm:production', '{{bin/npm}} run production');

tests/Features/DeployInitTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function it_generates_a_minimal_config_file()
3636
"done" => [],
3737
"fail" => [],
3838
"success" => [],
39+
"rollback" => [],
3940
],
4041
"options" => [
4142
"application" => "Laravel",
@@ -86,6 +87,9 @@ function it_generates_a_full_config_file_with_forge_defaults()
8687
],
8788
"fail" => [],
8889
"success" => [],
90+
"rollback" => [
91+
'fpm:reload',
92+
],
8993
],
9094
"options" => [
9195
"application" => "Laravel",

tests/Features/DeployInitWithLumenTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function it_generates_a_config_file_without_artisan_tasks_not_supported_by_lumen
3232
"done" => [],
3333
"fail" => [],
3434
"success" => [],
35+
"rollback" => [],
3536
],
3637
"options" => [
3738
"application" => "Laravel",

tests/Features/DeployRollbackTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class DeployRollbackTest extends DeploymentTestCase
99
protected $configs = 'basic';
1010

1111
/** @test */
12-
function a_rollback_with_no_previous_release_should_do_nothing_but_warn_user()
12+
function a_rollback_with_no_previous_release_should_do_nothing_but_warn_the_user()
1313
{
1414
$output = $this->artisan('deploy:rollback');
1515

@@ -42,9 +42,10 @@ function a_rollback_should_symlink_to_the_previous_release()
4242

4343
/* Rollback */
4444

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

4747
$this->assertSuccessfulDeployment();
4848
$this->assertServerHas('unicorn.txt');
49+
$this->assertStringContainsString('Executing task fpm:reload', $output);
4950
}
5051
}

tests/Unit/ConfigFileTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function it_can_render_a_config_file_that_is_equivalent_to_its_content()
2828
'done' => [],
2929
'fail' => [],
3030
'success' => [],
31+
'rollback' => [],
3132
],
3233
'options' => [
3334
'application' => "env('APP_NAME', 'Laravel')",

tests/Unit/DeployFileTest.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,13 @@ function it_adds_hooks()
226226
{
227227
$deployFile = (string) new DeployFile([
228228
'hooks' => [
229-
'start' => ['slack:notify'],
230-
'build' => ['npm:install', 'npm:production'],
231-
'ready' => ['artisan:cache:clear', 'artisan:migrate'],
232-
'done' => ['fpm:reload'],
233-
'fail' => ['slack:notify:failure'],
234-
'success' => ['slack:notify:success'],
229+
'start' => ['slack:notify'],
230+
'build' => ['npm:install', 'npm:production'],
231+
'ready' => ['artisan:cache:clear', 'artisan:migrate'],
232+
'done' => ['fpm:reload'],
233+
'fail' => ['slack:notify:failure'],
234+
'success' => ['slack:notify:success'],
235+
'rollback' => ['fpm:reload'],
235236
]
236237
]);
237238

@@ -245,6 +246,7 @@ function it_adds_hooks()
245246
after('hook:done', 'fpm:reload');
246247
after('deploy:failed', 'slack:notify:failure');
247248
after('success', 'slack:notify:success');
249+
after('hook:rollback', 'fpm:reload');
248250
EOD
249251
, $deployFile);
250252
}

tests/fixtures/configs/basic.php

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
'artisan:config:cache',
3131
'artisan:optimize',
3232
],
33+
'rollback' => [
34+
'fpm:reload',
35+
],
3336
],
3437

3538
'include' => [

tests/fixtures/recipes/mocks.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
run('echo "compiled app.js" > {{release_path}}/public/js/app.js');
1414
});
1515

16-
// Mock vendors
16+
// Mock other tasks.
1717
task('deploy:vendors', function() {});
18-
19-
// Mock artisan commands
18+
task('fpm:reload', function() {});
2019
task('artisan:storage:link', function() {});
2120
task('artisan:view:clear', function() {});
2221
task('artisan:view:cache', function() {});

0 commit comments

Comments
 (0)