Magento2 - local / staging / production deployment & gitignore

In fact, every day I am less convinced about the utility of Magento 2 production mode, unless you are not going to change anything in the project. Can you change my mind?

I'm not sure if I understand you correct, but that's exactly what the production mode is for: production systems where you do not change anything (code wise). Until the next deployment, that is.

I find the Git based deployment that you are using less suitable for Magento 2 than it was for Magento 1, because of all the preprocessing. The build and deployment is more complex and IMHO there is no way around an automated build process

What I would recommend:

  • Have repeatable deployments, i.e. you should be sure that the exact same code ends up in production that was in staging, including generated files.
  • To achieve that, separate build from deployment and do the following in the build process:

    • composer install (adding vendor to the repository instead is possible too, but if you did that just to avoid running composer on the server during deployment, rather do it in the build step and only keep composer.lock in the repo)
    • Code generation (YMMV):

      bin/magento setup:di:compile
      bin/magento setup:static-content:deploy
      
    • create an archive (the build artifact) from the full Magento directory, excluding media and var, but including vendor, pub, var/generated and var/di. Starting with magento-2.2, var/generated and var/di are moved to generated/code and generated/metadata, which makes it easier to separate them from the rest of var which should be ignored for deployments.

  • In the deployment, copy the build artifact to the target server, extract it to a new directory and:

    • link persistent directories into it (media, var/session, var/log, ...)
    • enable maintenance mode
    • switch document root (usually the docroot is a symlink to the last release, change it to the new release)
    • flush cache
    • run setup:upgrade
    • disable maintenance mode
  • This deployment process can be easily implemented with Deployer, which is like Capistrano but in PHP. A full deployment solution for Magento 2 based on deployer can be found here: https://github.com/mwr/magedeploy2 (thanks to netz98!) and here is another one that we use: https://github.com/staempfli/magento2-deployment-tool

  • Keeping app/etc/config.php in the repository is good to keep track of enabled and disabled modules.

This is not a step by step instruction but it should give you an overview for a more robust alternative to your current process. Take a look at the linked tools to see how a full solution may look like.


To my mind, wait Magento 2.2 or try to implement a similar approach.

Magento 2.2 introduces pipeline deployment by for example separating build server with production server.

Here is the official documentation : http://devdocs.magento.com/guides/v2.2/config-guide/deployment/pipeline/

Moreover, currently I am using Ansible to manage to automated deployment with configuration templates and multiple environment setup.