Magento 2 Git and Deployment
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
2. Now, it’s time to create a new project. Utilize the command below:
1 |
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition . |
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
2. Now, use the command line to install Magento. You can utilize the following code, but don’t forget to set your parameters:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
${project_root}/bin/magento setup:install \ --db-host=localhost \ --db-name=magento \ --db-user=root \ --backend-frontname=admin \ --base-url=http://base.url.goes.here/ \ --language=en_US \ --timezone=America/Chicago \ --currency=USD \ --admin-lastname=Admin \ --admin-firstname=Admin \ --admin-email=admin@example.com \ --admin-user=admin \ --admin-password=123123q \ --cleanup-database \ --use-rewrites=1 |
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:
1 |
echo "* * * * * php ${project_root}/bin/magento cron:run &" | crontab -u www-data - |
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:
1 |
bin/magento dev:urn-catalog:generate .idea/misc.xml |
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:
1 |
composer update |
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:
1 |
git pull composer install |
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:
1 |
bin/magento setup:upgrade bin/magento setup:di:compile (or setup:di:compile-multi-tenant) bin/magento setup:static-content:deploy |
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