Türchen 02: Automated Deployment with deployer.org

Putting every change to webservers can be an annoying job. To ensure same procedure every time you deploy, you could automate this process. Let me show how to with deployer.org

Bringing new version of your code base may be an annoying job for you and your team if you don't have a specialist DevOps in your team. To ensure you process your deployment every time with same procedure, you could use a specific tool for that job. Especially after some Magento-Core updates it is a good way to deploy your code with one of these tools.

Why using a deployment tool?

There are a lot of deployment tools in the wild. Some automate more or less than others, most use different programming languages, some need a root-server to deploy to other machines. Big benefit of these tools are speed, consistency, rollbacks and sometime parallel deployments. If you are working as team on a projects, you can guarantee that everyone deploys code the same way.

Why deployer.org?

I decided to use deployer as this tool come in our native programming language PHP. It is simple to install, learn and to use. It also brings some out-of-the-box recipes with it. You could also create your own recipes and share them with your team on VCS.

Typical deployment tasks:

Let's think about tasks, that you have/had to do on every deployment:

  • choose server, login
  • go to webserver directory
  • create folder structure for your project
  • separate folder/files that will be used in every deployment
  • collect and build your new code
  • create symlinks for folder and files
  • clear caches folder, flush database cache, invalid cache files

Create your magento deployer recipe

As mentioned earlier, deployer.org brings some recipes with it. One of these recipes is for default Magento1 Shop: View recipe on Github

This gives us a good base to work on.

First we define our servers

server('prod_1', 'domain.com')
    ->user('user')
    ->password('pass')
    ->set('deploy_path', '/home/www')
    ->stage('production');

or in an external file with YAML:

prod:
  host: domain.com
  user: www
  identity_file: ~
  stage: production
  deploy_path: /home/www/`

After that we build our first recipe:
Most of our desired tasks are covered by common.php and magento.php recipe.

Therefore we extend the existing Magento-Recipe:

<?php
 
 require 'recipes/magento.php';
 
 #import serverlist
 serverList('config/servers.yaml');

# Deployer tasks run as root. We'd like to fix rights:
task('fix-rights', function () {
    $httpUser = get('http_user');

    if (null === $httpUser) {
        $httpUser = run("ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1")->toString();
    }
    #runs as root
    run('chown -R ' . $httpUser . ':' . $httpUser . ' {{deploy_path}}');
});

#We'd like to fix rights after deployment/cleanup and after rollback:
after('cleanup', 'fix-rights');
after('rollback', 'fix-rights');


#Now we define deployment of our git-based code:
task('set-shop-repository', function () {
    set('repository', 'https://github.com/firegento/magento.git');
});

task('deploy-store', [
    'set-shop-repository',
    'deploy'
]);

Running it with dep deploy-store should result in something like this:

screenshot-deployer

Deployer will create following folder structure:

/your/deployment/path
- releases/
- - 20161101120000
- - 20161203090000
- shared/
- - media/
- - var/
- - app/etc/local.xml
- current/ => symlink to current release

You could set the number of kept releases.
With dep rollback you can rollback one release within seconds.

Just beginning

With that simple deployment recipe you can automate your process to build new releases and rollback.
That's just a simple beginning. With deployer you could also run shell commands. So you could use modman, composer or magerun.

One thing, our script doesn't cover: your database! Keep caution about changes in tables that are hard to revert.

Alternatives

You don't want to use deployer.org?? No problem:

Try one of these other solutions:



Ein Beitrag von Kevin Krieger
Kevin's avatar

Kevin Krieger is a computer scientist, freelancer and volunteer. He is working as Magento freelancer since 2015, previously as web developer frontend/backend in various projects. His first experience with magento was in 2008. He likes to attend and organize barcamps like SaarCamp and CoderDojo Saar. Find out more about him on his blog, GitHub page, Xing Profile or Twitter

Alle Beiträge von Kevin

Kommentare
Peter Jaap am

Small issue; the top of your deploy.php should be;

Björn am

We also where searching for a deployment tool writen in PHP and we can recommend http://robo.li/

There is also docker support for robo ;-)

Dein Kommentar