Magento Cloud Command-Line: Basic Usage

One of the many purposes of Magento Cloud is to save valuable DevOps time and to reduce deployment issues. Although is very convenient to utilize the UI provided by Magento Cloud Web, it is even more time-efficient to do the same tasks via Command-Line (CLI).

First, the Magento Cloud CLI must be downloaded and installed into the computer client:

  1. Make sure you are logged in as the OS user that will access the cloud resources (not necessarily the root user).
  1. Install the magento-cloud CLI.

$ curl -sS https://accounts.magento.cloud/cli/installer | php


Then, add magento-cloud to the bash profile. 

$ export PATH=$PATH:$HOME/.magento-cloud/bin

After that, reload the bash profile

$ ~/.bash_profile

Now you can execute Magento clouds commands by typing magento-cloud or mgc.  Please note that when using the console for the first time, you need to login by providing your username and password. 

$ mgc


After this, when typing mgc list you will be able to check the available CLI commands; where most of them provide a basic CLI menu for guiding you into the executed procedure.

$ mgc list

After downloading it is recommended that you generate a public SSH key upload it to the project. (Generating an SSH key is out of the scope of this article)

$ mgc ssh-key:add ~/.ssh/id_rsa.pub

Note: When generating any SSH key please keep the default name as “id_rsa.pub”, because by default Magento cloud will look for this specif file name.

WEB UI actions on CLI

For the purpose of imitating the most common Web UI actions you might be interested in check the following commands (In alphabetical order):

db:dump:  Create a dump file from the environment selected and downloads it into your computer. No need to mention how convenient is this command that reduces many steps and data transfer between the host and client.

db:sql: Opens the Mysql CLI interface for the specific environment.

environment:active:  Activates the selected environment.

environment:branch: Branches an environment. Allowing you to select the parent branch.

environment:list: Lists the current environments for the project(s).

environment:merge: Merges two environments

environment:redeploy: Redeploys an environment without pushing any code. Very convenient when you just need to restart the environment.

environment:ssh: SSH to an environment.

environment:sync: Syncs an environment from its parent.

mount:download: Allows to download files from the environment.

mount:upload: Allows to upload files from the environment.

tunnel:open & tunnel:info: A combination of these commands allows you the get ip addresses, usernames, and passwords of the internal services of any specific environment, such as Elastics Search and Mysql DB, without the need to access the environment directly.

It is worth mentioning that if you would like to explore what param combinations are possible when using the above commands you can always use the “–help” param.

$ mgc –help command

In definitive, using the magento-cloud CLI is not a must when using Magento Cloud, however, when getting used to it will speed up, even more, the clouding experience.

Reducing Magento Cloud Git Repository

Magento Cloud customers and operators might not know, but Magento Cloud Git repository size on the Cloud is not limitless; and because of this, a junior Engineer might not pay attention to details when pushing new commits into this Git repository.

A common mistake would be to commit and push “media” resources or big static files to the repository, causing an exponential growth when merging between branches; leading to being unable to connect via SSH or browser to any of the environments. Also, the Git repository might not be able to allow pushing new commits.

Usually, the first attempt to check and remove any media resource or unnecessary data is to remove, make a new commit and push back to the repository. Unfortunately, this workflow never really works. 

Deleting files in a commit doesn’t actually reduce the size of the repo since the earlier commits and blobs (files) are still around in previous commits and included in the git database, so the repository will never decrease. 

With that being said, the Magento Cloud repository can be reduced by following these steps:

  1. Navigate to the repository:

cd cloud_repository/

  1. Change to the branch that contains big files:

git checkout cloud-branch

  1. Execute filter-branch to remove the bigs files and save the re-written Git history in HEAD

git filter-branch –force –tree-filter ‘rm -rf pub/media/*’ HEAD

  1. Instruct Git to purge and prune the unwanted data:

git reflog expire –expire=now –all && git gc –prune=now –aggressive

  1. Lastly, force push to the repository:

git push –force origin master

After this, the repository size will be reduced; helping to prevent any of the issues caused by the lack of space in the Git repository; however, before performing these steps make sure to make a copy of the repository in case some info needs to be restored in case of errors

It is also good to take into account that after re-writing and/or purging the Git repository all the collaborators need to rebase or re-clone the repository, in order to avoid re-pushing the old commits and blobs.

Magento Cloud: Create a New Integration Environment

What if you need to create a new Integration environment in Magento Cloud? In this article, I will detail the process of creating a new Integration environment from the Staging branch.

You already noted that the Magento Cloud UI interface is very easy to use; making you feel confident that creating a new Integration Environment is just as performing simple “delete” and “branch from Staging” operations; however you will find out soon that this is not the case.

After deleting the Integration environment you will realize that there are no available options for creating a new branch from the Staging site. If this is your case, don’t panic. Magento Cloud CLI comes to the rescue.

For creating a new Integration Environment from Staging, just follow these steps:

  1. Clone or checkout to the latest Staging git repository

$ git checkout staging -f

  1. Create a new branch based on Staging, better to call it Integration for keeping the same naming convention

$ git checkout -b integration

  1. Push to Magento Cloud for creating the new environment. 

$ git push magento integration

  1. After this, the new environment will be displayed as a child of Master, however, we can change this environment parent by using the Magento Cloud CLI and editing its parent info like this:

$ magento cloud environment:info -e integration parent staging 

  1. By using the command above we are telling Magento Cloud that we want the Staging server to be the parent of our new Integration environment. After this, if you refresh the Magento Cloud Web you will see that Integration is correctly begin displayed as a direct child of Staging, meaning that you are able to continue using the integrated CI/CD provided by Magento Cloud. You just need to activate the new environment.

$ magento cloud environment:activate integration 

  1. Also the CLI will show the correct info as well:

Deploying PHP extensions to Magento Cloud

By design, Magento Cloud clouds provide an “out of the box” environment structure, containing all the libraries and settings necessary to run Magento, without needing to spend time in finding the correct configuration and specifications. However from time to time, due to technology changes, extra PHP extensions might be required to be installed.

The most common extensions required to be added are those related to security; just like new encryption algorithms that enhance the security of cloud environments. Also, there might be a chance that a new module has been added and requires one of these new PHP extensions. 

In this article, it is assumed that a new module was installed and require to use PHP Libsodium extension; and because the Libsodium extension doesn’t exist in the environment Magento might face exceptions, with errors similar like the following appearing in logs:

Since a Magento Cloud environment cannot be changed manually on CLI, the only accepted procedure is to add the library in the .magento.app.yml  file, included in every Magento Cloud environment.

Adding Libsodium is as simple as adding in the “extensions” section as follows:

# Enable extensions required by Magento 2

runtime:

    extensions:

        – redis

        – xsl

        – json

        – blackfire

        – newrelic

        – sodium

After pushing the changes into the environment and SSH into it, the new extension will be visible and fully configured.

Although Magento Cloud does not allow direct changes to the environment by CLI, it still provides a very flexible mechanism for customizing any environment by utilizing the embedded CI/CD, providing a robust flow for avoiding misconfigurations and human errors; and the same procedure is safe to be applied to any other PHP extensions required over the time.

Rerouting Magento Cloud Request to Another CMS

Magento Cloud, as the name suggests, runs on cloud. Means that, they already have all the network and infrastructure configured for you and they work great. You don’t need to worry about those things, and you as a Magento developer, can just focus on developing your site.

Since it runs on the “cloud”, then you may not have all the flexibility to customize your nginx, firewall, and other infrastructure aspects. There might be cases where you need to integrate some third party apps to your Magento. One example is you usually need to have a wordpress blog running under /blog of your Magento installation.

In order to do this on Magento Cloud, you need to utilize the Fastly module. Fastly comes by default with your Magento Cloud, so you don’t need to pay anything extra for that.

In order to route specific path to an external service, you need to do the following on your Magento admin:

  1. Enable Fastly CDN
  2. Enable Fastly Edge Modules
  3. Create Edge Dictionary
  4. Add new Fastly Backend Service
  5. Configure Other CMS / backend integration Edge Module

Magento cloud documentation and it’s Github has already covered most of the aspects I mentioned above. However, there are some details that haven’t been explained clearly for the Edge Dictionary. For example, if you want to route /blog to your wordpress site, then you will need to add a dictionary item with key blog and value 1. This is very important to highlight otherwise your custom route won’t work. Overall, to enable this function, you will only need less than 15 minutes to configure everything. But if you miss a step (like us!), then it might get you two days!