Fetching contributors…
Cannot retrieve contributors at this time
151 lines (100 sloc) 4.84 KB
Title: Türchen 02: Automated Deployment with
Date: 2016-12-02
Tags: adventskalender
Author: kevinkrieger
Intro: 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
Text: 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
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, 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](
(code lang: php)
server('prod_1', '')
->set('deploy_path', '/home/www')
or in an external file with YAML:
(code lang: yaml)
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:
(code lang: php)
require 'recipes/magento.php';
#import serverlist
# 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', '');
task('deploy-store', [
Running it with `dep deploy-store` should result in something like this:
(image: screenshot-deployer.png)
Deployer will create following folder structure:
- 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 No problem:
Try one of these other solutions:
* [Ansible](
* [Salt](
* [Capistrano](
* [Fabric](