Deployment

Made the right way

...more or less

Why talk about deployment?

A presentation made by Kirill Kalachev / @dhampik

Deployment problems

  • code replacement
  • shared files
  • build process
  • application restart
  • deployment permissions
  • cronjobs
  • environments
  • tests

Code replacement

  • Symlink current folder
    
    |-- var
      |-- www
        |-- facebook
          |-- current => /var/www/facebook/releases/20130721000000
          |-- releases
          |  |-- 20130721000000
          |  |-- 20130602000000
          |-- shared
                            
  • There are some issues

Shared files

  • Logs
  • Uploads
  • Cache, etc

Build proccess

  • Composer
    • composer.lock
    • composer update
    • require-dev
  • Migrations
  • Assets

Application restart

  • Do we need to restart webserver?
  • PHP OPcache
  • Symlink switching

Deployment permissions

  • What permissions deployed files/folders should have?
  • Who can deploy and how to restrict access?

Cronjobs

Environments

  • Yii2 environments are bad
  • .env https://github.com/vlucas/phpdotenv
    • Never store secure information in repository
    • No need to modify server configs
    • Easy to manage configuration in one place, simple portability

Tests

  • Run tests before deployment
  • Don't deploy if tests fail

Deployment tools

  • manual
  • git pull
  • bash script
  • chef/puppet/ansible
  • CI
  • Tools from other languages
  • phing
  • magallanes / atlax / deployer / rocketeer

Manual deployment

It wastes your time!!!

Git pull

It wastes your time anyway!!!

Bash script

Better, but you have to code all by yourself

Chef / puppet / ansible

These are actually tools for automating server configuration, but they could also be used for deployment

CI is important!

  • Continuous integration server is NOT actually a deployment tool
  • Continuous deployment is when your code is automatically deployed to production/staging
  • Continuous delivery is when your code is always ready for deployment

Tools from other languages

Phing

www.phing.info

  • old
  • xml

Magallanes

magephp.com

Atlax

kohkimakimoto.github.io/altax

Deployer

deployer.org

Rocketeer

rocketeer.autopergamene.eu

Rocketeer: a closer look

Releases


// config.php
'connections'      => [
    'staging' => [
        'host'      => '188.226.251.17',
        'username'  => 'phpist',
        'password'  => '',
        'key'       => '',
        'db_role'   => true,
        /*...*/
    ],
    'production' => [/*...*/]
],
                

// config.php
'on' => [
    'connections' => [
        'staging' => [
            'scm' => ['branch' => 'staging'],
            'remote' => [
                'root_directory' => '/var/www/',
                'app_directory' => 'staging.site.com'
            ]
        ],
        'production' => [/*...*/]
    ]
]
                

Before symlink


// hooks.php
'before-symlink' => [
    'deploy' => [
        function ($task) {
            $task->runForCurrentRelease([
                $task->binary('npm')->install(),
                $task->binary('bower')->install('--config.interactive=false'),
                $task->binary('grunt')->build(),
            ]);
            $task->runForCurrentRelease([
                $task->binary('php')->yii('migrate --interactive=0')
            ]);
        }
    ]
],
                

php5-fpm opcache

We should use real path instead of symlinked, so that php5-fpm would understand that code has changed


fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
                

Vagrant and deployment keys

  • Deploy from vagrant box, coz PHP is there.
  • Use different comman instead of default one on Windows:
    
                                php vendor/anahkiasen/rocketeer/bin/rocketeer deploy
                            
  • Deploy keys should have proper permissions
  • Deploy keys should not be under version control
  • Deploy keys should be shared with vagrant box

Thank you!

Questions?