Deploy Magento2 using Gitlab pipelines and Deployer PHP
Versione Italiana

Leggi questo articolo in italiano: click here


This guide is for a single frontend node and is not fully optimized. The configuration is for Production and Staging but we use only staging environment.

This is an implementation of Magento Pipeline Guidelines: https://devdocs.magento.com/guides/v2.2/config-guide/deployment/pipeline/

Magento Version: >= 2.2.x

Follow this project to have an example: https://gitlab.com/gm-public/magento2-sample-deploy/pipelines

REQUIREMENTS

  • gitlab account
  • magento project with a local setup (so you have a config.php on your repository)
  • ssh access of deployment server
  • you know how deployer php works https://deployer.org/

Create a deploy ssh-key to access to your server

ssh-keygen -t rsa -b 4096 -N '' -f my_ssh_key

Create an ssh-keyscan of your server

ssh-keyscan -t rsa magento.staging > server_keyscan.txt

Add public deploy ssh key to the server

ssh-copy-id -i my_ssh_key.pub [email protected]

Create the project folder on the server

ssh [email protected]
mkdir /home/admin/magento
mkdir /home/admin/magento/bucket

Add Gitlab Variables for Pipeline

Go to Settings > CI/CD > Variables and add these variables:

For Stage environment

STAGE_HOST -> ip or domain of your web machine (ex. magento.staging)
STAGE_HOST_DEPLOY_PATH -> absolute path where you put your source code (ex. /home/admin/magento)
STAGE_SSH_KEYSCAN -> output of the file server_keyscan.txt
STAGE_SSH_PRIVATE_KEY -> output of the file my_ssh_key
STAGE_USER -> ssh user for server access (in our example admin)

Add gitlab pipeline files in your project

Use this project to add Gitlab Pipeline: https://github.com/gmdotnet/magento2-pipeline-deploy

Go to Settings > CI/CD > General pipelines settings and add pipelines/gitlab/.gitlab-ci.yml under Custom CI config path

IMPORTANT!!!

In this case pipeline start only with develop branch. In any case, we need to have .gitlab-ci.yml on default branch (master) or the pipeline doesn't start Also, first deploy/release will fail because we don't have any setup completed in the server

Create/compile Magento env.php on server

Once fist deploy (with error) is finished you have this folder tree:

/home/admin/magento
/home/admin/magento/bucket
/home/admin/magento/current
/home/admin/magento/pipelines
/home/admin/magento/releases
/home/admin/magento/shared

edit /home/admin/magento/shared/htdocs/app/etc/env.php to make your magento setup.

Try to re-deploy. This time you can have the pipeline completed.

Enjoy!


Improvements

  • create a system for multinode web server
  • managing in a secure way ssh private key

Improve the project

Send me a feedback

Write me a line or open an issue on github.

GitHub
Blog Comments powered by Disqus.