GIT and deployment strategy Magento2 projects
Steps below describe how to set up environment for custom module development, not for production.
Project initialization
Add repo.magento.com credentials and github access token to auth.json in composer home directory
Create project using the following command:
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition .
Take this .gitignore and put into your project root. Almost all core files/directories are already added to the root
.gitignore
, but it is better to add the following 2 as well/update
and/phpserver
(just add these 2 lines to .gitignore)Initialize new git repository in the project root
Add all untracked files to git and commit them
Start development of your modules as usual (put them under
app/code/VendorName/ModuleName
), now you will have only your custom code in your git repository
Magento installation
Make sure all filesystem permissions are set as outlined in the official guide
Install Magento using command line, e.g.:
${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 \ [email protected] \ --admin-user=admin \ --admin-password=123123q \ --cleanup-database \ --use-rewrites=1
Enable indexers cron job, e.g. on Ubuntu:
echo "* * * * * php ${project_root}/bin/magento cron:run &" | crontab -u www-data -
Magento will run in
default
mode and all missing content will be auto-generated upon first request. So no need to run compiler or static content deploy[optional] If using PHP Storm, run the following command in to enable XSD support:
bin/magento dev:urn-catalog:generate .idea/misc.xml
For Initialisation and Installation follow the steps from Alex his answer for most of the steps, only differences I would recommend:
Git configuration
Only store the following files in your Git repository:
- composer.json
- composer.lock
- app/etc/config.php
For your project custom code, also use separate modules that you include thru composer. Managing this thru composer is easier as you can lock a specific version/release that you want to deploy. This also forces you to use the same approach for internal and external modules.
Deployment
During development you update the modules on your environment (dev/test) with the command:
composer update
This will update the composer.lock file with the versions installed on that installation.
On staging/pre-production/production you can create/install the same setup with the command:
git pull
composer install
This will install all the same modules as used in dev/test to ensure that the testing before publishing to production is done with the same module versions as it is developed with.
After the installation to run the following commands:
bin/magento setup:upgrade
bin/magento setup:di:compile (or setup:di:compile-multi-tenant)
bin/magento setup:static-content:deploy
This will update the database (schema and data upgrade), generate the DI configuration and deploy all static view files.
we run a different approach which does not involve a separate build-server/process, we locally develop as if in production
we then commit all files necessary for production. we then simply deploy the changesets to the server and run the upgrade command.
getting to a version which is suitable for development but also runs in production mode was the tricky part and is still not perfect but now we got a recipe which works.
the reason is that we want to have 100% control over what code goes into production. since magento2 generates a ton of code we must run it locally to be able to understand all the effects and being able to debug as if in production.
I'm aware that this not what many people recommend to do but for us it works best.
frontend-setup steps
In order for these scripts to work set your shop to production mode in your env.php and setup your theme in dev/tools/grunt/configs/themes.js
.
(the following steps were put into an ansible playbook)
- delete
var/cache
- delete
var/view_preprocessed
- delete
pub/static/*
(don't delete the .htaccess) - delete
var/composer_home
- run
php bin/magento cache:flush
- run
php bin/magento setup:static-content:deploy %your_languages%
- delete all themes/languages you don't actually use from pub/static/frontend
- remove hard-copies of less files from
pub/static/frontend
- run
php bin/magento dev:source-theme:deploy --locale="%your_language%" --theme="%your_theme%" css/styles-m css/styles-l css/email css/email-inline
- optional: we use a bash-script to change the absolute symlinks, created in step 9, into relative ones, making it possible to run grunt from outside the vm
- run
grunt less:your_theme
backend/di-setup steps
- delete
var/cache
- delete
var/generation
- delete
var/composer_home
- delete
var/di
- run
php bin/magento cache:flush
- run
php bin/magento setup:di:compile