pexels-photo-335907

Deployer: Nanbando integration

After my first blog post abut Deployer: Deployment made easy I thought about integrating my backup-tool nanbando into the setup which deploys my homepage.

Integration

The idea was to create a backup before the deployment. After a few attempts, I realized that Nanbando and Deployer are predestinated to achieve such a task. 

Prototype

diff --git a/deploy.php b/deploy.php
index 639fb44..6ad1386 100644
--- a/deploy.php
+++ b/deploy.php
@@ -11,6 +11,7 @@ set('git_tty', true); // [Optional] Allocate tty for git on first deployment
 set('bin/php', '/path/to/php');
 set('bin/composer', '{{bin/php}} /path/to/composer');
 set('bin/console', '{{release_path}}/bin/websiteconsole');
+set('bin/nanbando', 'nanbando.phar');

 add('shared_files', ['app/config/parameters.yml']);
 add('shared_dirs', ['var/indexes', 'var/sessions', 'var/sitemaps', 'var/uploads', 'web/uploads', 'var/cache']);
@@ -33,3 +34,21 @@ after('deploy:failed', 'deploy:unlock');
 // Migrate database before symlink new release.

 before('deploy:symlink', 'database:migrate');
+
+// nanbando
+
+task('nanbando:reconfigure', function () {
+    run('cd {{current_path}} && {{bin/php}} {{bin/nanbando}} reconfigure');
+})->desc('Reconfigure nanbando');
+
+task('nanbando:backup', function () {
+    run('cd {{current_path}} && {{bin/php}} {{bin/nanbando}} backup');
+})->desc('Create backup');
+
+task('nanbando:push', function () {
+    run('cd {{current_path}} && {{bin/php}} {{bin/nanbando}} push');
+})->desc('Push backup to remote storage');
+
+before('nanbando:backup', 'nanbando:reconfigure');
+after('nanbando:backup', 'nanbando:push');
+before('deploy:prepare', 'nanbando:backup');

The prototype is built on top the script we developed in the last blog-post. It reconfigures nanbando in the project root directory and creates a new backup (configured in the "nanbando.json") before the "deploy:prepare" task of Deployer will be called.

Deployer recipe

To improve the Developer-Experience I have extracted this few lines of code into an own repository nanbando/deployer-recipe. This can be installed via composer.

Simply add the following lines to your "deploy.php" file in any project and the next release will create a full backup of your project when you trigger the deployment.

diff --git a/deploy.php b/deploy.php
index 639fb44..a6ab3c2 100644
--- a/deploy.php
+++ b/deploy.php
@@ -2,6 +2,7 @@
 namespace Deployer;

 require 'recipe/symfony.php';
+require 'vendor/nanbando/deployer-recipe/recipe.php';

 // Configuration

@@ -11,6 +12,7 @@ set('git_tty', true); // [Optional] Allocate tty for git on first deployment
 set('bin/php', '/path/to/php');
 set('bin/composer', '{{bin/php}} /path/to/composer');
 set('bin/console', '{{release_path}}/bin/websiteconsole');
+set('bin/nanbando', 'nanbando.phar');

 add('shared_files', ['app/config/parameters.yml']);
 add('shared_dirs', ['var/indexes', 'var/sessions', 'var/sitemaps', 'var/uploads', 'web/uploads', 'var/cache']);

Conclusion

This simple extension shows you how easy it is to include third party tools into the deployment process. The resulting recipe is easy to install and use. You cannot forget the backup creation in the future :)