Magento 2 Git and Deployment

- Fire development, Magento 2

Magento 2 Git strategy

If you had a coding experience with Magento 1, you might have utilized a deploy tool designed to pull in the GIT repository, ran such commands as modman deploy-all, and check if a var directory is writable. But how did all these things change with the appearance of Magento 2? Below, you will get the answer. The following Magento 2 Git and deployment guide describes such important aspects as the best gitignore, project deployment, pre- and post deploy commands, etc.

Let’s start with custom module development. The tutorial below shows how to set up a special environment for your Magento 2 projects.

Initialization of your Magento 2 project

1. Go to composer home directory; find the auth.json file there; add Magento 2 credentials (repo.magento.com) as well as an access token for GitHub.

2. Now, it’s time to create a new project. Utilize the command below:

3. Then, you should perform some operations with .gitignore. Take this one – Magento 2 .gitignore, put it into your Magento 2 project root, and add /update and /phpserver lines to .gitignore. Although, almost all core directories and files are already included, but it is vital to add two more to the root .gitignore.

4. Go to the root of your Magento 2 project and initialize a new git repository there.

5. Don’t forget about untracked files related to your Magento 2 project. Besides adding them to Git, you should commit them as well.

6. Now, you can run the development of your Magento 2 modules as usual putting them under app/code/VendorName/ModuleName. But your Git repository will contain their custom code.

Installing Magento 2

1. To install Magento 2 correctly, set all your filesystem permissions as described in the official documentation.

2. Now, use the command line to install Magento. You can utilize the following code, but don’t forget to set your parameters:

3. The next step requires enabling indexers cron job. You can read more about Magento 2 cron here: Magento 2 Cron Configuration.To enable indexers cron job on Ubuntu, run this command:

4. Now, Magento 2 will run in the default mode. It means that the auto-generation procedure will take place for all missing content upon the first request. Consequently, there is no need to run static content deploy or use compiler.

5. this step is optional and it is aimed at PHP Storm users. If you are among them, enable XSD support by running the following command:

Additional Magento 2 Git configurations

Please note that the Git repository should contain only the following files:

  • composer.lock
  • composer.json
  • app/etc/config.php

As for the custom code of your Magento 2 projects, use separate modules included via Composer. Thus, you will be easily able to lock and deploy a specific version. The same approach should be utilized in a case  of both for internal and external modules.

Magento 2 Deployment

To update your Magento 2 modules on your dev or test environment during the deployment, run the following command:

As a result, your composer.lock file will be updated with the latest versions available on your installation.

As for staging, pre-production, and production, the below command is used for creating or installing the same setup:

Magento 2 modules from your dev/test environment will be installed. Hence, you will be able to verify if the same module versions are under tests before publishing everything to production.

Finally, after everything is installed, it’s necessary to run this commands:

It updates the database, generates the DI configuration and fixtures, as well as deploys  static view files.

______

That’s everything you should know about the Magento 2 Git and deployment. Although, there always be some nuances related to both processes, use StackExchange to find answers to your questions. For instance, this post is based on a question by Sander Mangel and answers by Alex Paliarush and Vladimir Kerkhoff.