Build and Publish Magento 2 with Shipit-Deploy

Magento 2.2 with config dump

Magento 2 is still a new platform for many. The fact of how many new functionalities are added in subsequent versions shows how much Magento is developing. Along with version 2.2 we were given a system allowing to dump the configuration of the project to the file config.php. This, in turn, gives you the opportunity to build a project before we publish it. This allows you to shorten downtime almost to zero.



Shipit for Magento 2

Today I would like to present to you the possibility of easy and fast building of server-side applications and publishing it on the server. For this purpose, we will use:

  • https://github.com/Alekseon/clean_composer_magento_2
    Repository prepared by Alekseon, in which we will find the file composer.json needed to download all dependencies. There is also an app/etc/config.php file that contains the configuration dump from the pure Magento 2 version
  • https://github.com/Alekseon/shipit-magento2
    Package prepared by Alekseon thanks to which, after providing some information, we should be able to send a new code to the server using one application, build an application on the server side and replace it with the current version
  • https://cloud.digitalocean.com/
    Small remote server to run magento on it (you can use your own server of course). We will use clean Ubuntu 16.04 and configure it quickly by ourselfs.

Server preparation

At the beginning, we will create a machine for which we will be able to send our code. For this purpose, we can use our own server, which can be logged in via ssh. We can also create a virtual machine at https://cloud.digitalocean.com

When creating a machine, we can add our public key to immediately have the option of logging in without entering a password.

When the machine is ready we can log in. In my case:

ssh root@159.89.181.57

We will need a composer here, so let's install it:

apt-get update
apt-get install composer

We also need to install a few missing php packages which are required by Magento

apt-get install php-curl php-intl php-mbstring php-dompdf php-xml php-mcrypt php-mysql php-soap php-zip

It will also have to create an auth.json file containing access data to the Magento repository. It should be located in your home directory under .composer/auth.json to be sure it will be automatically read by composer. You should include into this file your private and public api key of magento

{
    "http-basic": {
        "repo.magento.com": {
            "username": "your public key",
            "password": "your private key"
        }
    }
}

If we are able to log in, it should be enough for now. We'll be back to the server later.


Shipit preparation for Magento2

Let's clone now the shipment repository for Magento 2

git clone https://github.com/Alekseon/shipit-magento2.git
cd shipit-magento2 /
npm install --global shipit-cli
npm install --global shipit-deploy
npm install

Let's now look at the stages/index.js file.
We have information about the repository that shipit-deploy will use to download the code on the server. This is a pure version of magento 2.2, which we can use for our test. You can enter your repository here.

repositoryUrl: 'https://github.com/Alekseon/clean_composer_magento_2.git',

We can also set up here files that will not be published, and which are needed for the operation of magento on the server.

linkedFiles: [
        'app / etc / env.php' // It's advised that env.php for stages would not
                            // be stored in repository due to security concerns.
    ]
linkedDirs: [
        'Pub / media'
    ]

In the projectPath attribute, you can define the path to the directory in which your project is located. For example 'src' or 'html'. In our case, the application is in the main directory, so leave the field blank.

ProjectPath: ''

In the part containing module.exports, we can define our environments, such as preprod, prod, etc. In our example, we will only use preprod.
Here, we paste information about our server

preprod: {
        servers: 'root@159.89.181.57',
        branch: 'master',
        deployTo: 'testApp',
        isProtected: false,
    }

If everything is already set, save the file and execute the command:

shipit prepod deploy

The first launch should download all dependencies by the composer. The script will also try to symlink the app/etc/env.php file and the pub/media directory that do not yet exist on the server. We will have to create them by hand.
For this purpose, log on to the server and see what we have there. We should see the newly created 'testApp' directory and in the middle of 4 directories:

  • current
  • releases
  • remote-cache
  • shared

You can locate your env.php file in shared/app/etc/env.php and all your media in shared/media/pub. This will always be linked to the newly built app

All that remains is to install nginx and connect to the appropriate directories and install the database on the server

Summary

This article does not cover everything you need to run Magento2 on the server. However, it should allow to automatically publish the Magento 2 application with almost zero downtime. I hope it will help you in any degree. Leave comments. I will do my best to answer