Import Magento Commerce code into a Cloud project

Use the following instructions to import your Magento Commerce code into a current Magento Commerce Cloud environment.

When you force push code from an existing Git branch to the branch for your Magento Commerce Cloud environment, you overwrite the project code in the master branch which removes all data, sites, stores, and other development work you have done in the Cloud environment. If you want to keep any files or directories, copy them to a directory outside of your project.

Required information

You need the following information to import code into your project:

  • Encryption key from your Magento Commerce system

  • SSH or HTTPS URL for your Magento Commerce installation Git repository.

Create a remote Git reference

Create a remote Git reference from your Cloud Git repository to the repository containing your Magento Commerce installation so you can pull the Magento Commerce code into your project.

  1. Log in to your local Magento Commerce Cloud development machine as, or switch to, the Magento file system owner.

  2. Copy composer.json to a non-tracked directory so it does not get overwritten.

    1
    
    cp composer.json ../composer.json.cloud
    
  3. Rename your Cloud Git remote from origin to cloud-project to make it clear which repository is which:

    1
    
    git remote rename origin cloud-project
    
  4. Add a remote upstream for your existing Magento Commerce installation:

    1
    
    git remote add prev-project <git url>
    
  5. Review the remote branch configuration.

    1
    
    git remote -v
    

    Verify that the remote branch configuration matches the following sample configuration, with your project name instead of ikyyrqvlgnrai.

    1
    2
    3
    4
    5
    6
    
    cloud-project   ikyyrqvlgnrai@git.us.magento.cloud:ikyykimjgnrao.git (fetch)
    cloud-project   ikyyrqvlgnrai@git.us.magento.cloud:ikyykimjgnrao.git (push)
    magento ikyyrqvlgnrai@git.us.magento.cloud:ikyykimjgnrao.git (fetch)
    magento ikyyrqvlgnrai@git.us.magento.cloud:ikyykimjgnrao.git (push)
    prev-project    git@github.com:mygitusername/myeereponame.git (fetch)
    prev-project    git@github.com:mygitusername/myeereponame.git (push)
    
  6. Checkout the Cloud project master branch.

    1
    
    magento-cloud environment:checkout master
    
  7. Set the cloud-project branch as an upstream tracking branch for master.

    1
    
    git fetch cloud-project
    
    1
    
    git branch -u cloud-project/master
    

Import your Magento Commerce code to your Cloud project

Before you begin the import process, make sure that you have completed the steps to prepare your existing Magento Commerce system.

After you have completed the git reference configuration, you can import the Magento Commerce code.

  1. Fetch the Magento Commerce branch.

    1
    
    git fetch prev-project
    
  2. Reset your Cloud master branch to contain the code and the commit history of your Magento Commerce branch.

    1
    
    git reset --hard prev-project/<branch name>
    
  3. Push code from your Magento Commerce project to your Magento Commerce Cloud project, overwriting the previous contents and commit history with that of your project.

    1
    
    git push -f cloud-project master
    

    If the import succeeds, the Magento Commerce Cloud environment redeploys.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    Re-deploying environment 43biovskhelhy-master-l5ut8gq.
       Environment configuration:
         mymagento (type: php:7.0, size: S, disk: 2048)
         mysql (type: mysql:10.0, size: S, disk: 2048)
         redis (type: redis:3.0, size: S)
         solr (type: solr:4.10, size: S, disk: 1024)
    Environment routes:
       http://master-o9gv6gq-43biovskhelhy.us.magentosite.cloud/ is served by application `mymagento`
       https://master-o9gv6gq-43biovskhelhy.us.magentosite.cloud/ is served by application `mymagento`
    

Import the Magento database

Database import and export operations can take a long time, which can affect site performance and availability. Schedule import and export operations during off-peak hours to prevent slow performance or outages on Magento Production sites.

Before you can use your existing Magento Commerce code in Magento Commerce Cloud, you must import the database.

You need the following information to complete this task:

  • SSH URL for the Magento Commerce Cloud environment
  • The database name, username, and password for the Cloud database

When importing data, you need to drop and create a new database. If you have data you want to keep, create a backup of the database.

This topic discusses how to import the Integration environment database. The database connection information is different for Staging and Production environments.

To drop and re-create the Cloud database:

  1. Use SSH to log in to the Integration environment.

    1
    
    magento-cloud ssh
    
  2. Connect to the database.

    1
    
    mysql -h <db-host> -P <db-port> -p -u <db-user> <db-name>
    
  3. Drop the database. At the MariaDB [main]> prompt, enter:

    1
    
    drop database main;
    
  4. Re-create the database:

    1
    
    create database main;
    
  5. At the MariaDB [main]> prompt, enter exit.

  6. At the shell command prompt, enter the following command to re-create the database.

    1
    
    zcat var/db.sql.tgz | sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | mysql -h <db-host> -P <db-port> -p -u   <db-user> <db-name>
    

    For example,

    1
    
    zcat var/db.sql.tgz | sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | mysql -h database.internal -p -u user main
    

Update base URLs

Before you can access Magento from your local Cloud development system, you must change the Base URLs in the Magento database. Base URLs are stored in the core_config_data table.

The following example shows how to change only the insecure URL but you can use the same procedure to change secure URLs as well.

To update the unsecure base URL:

  1. If you have not already done so, use SSH to connect to the Cloud integration server.

    1
    
    magento-cloud ssh
    
  2. Connect to the database.

    1
    
    mysql -h <db-host> -P <db-port> -p -u <db-user> <db-name>
    
  3. Show the contents of the core_config_data table.

    1
    
    SELECT * from core_config_data;
    

    Note the path of web/unsecure/base_url; this is the value to change.

  4. Enter the following command to change the value of path to your integration server’s unsecure base URL:

    1
    
    UPDATE core_config_data SET value='<Cloud unsecure base URL>' WHERE path='web/unsecure/base_url';
    

    The base URL must end with a / character.

  5. Confirm the change by entering the following command:

    1
    
    SELECT * from core_config_data;
    
  6. If the change was successful, enter exit to exit the [Maria DB] prompt.
  7. Continue with the next section.

For your system to be fully functional, you must also set unsecure and secure URLs for the default scope as well as for all websites, stores, and store views.

Add the Magento encryption key

The Magento Commerce encryption key is required as an environment variable in env.php for Integration, Staging, and Production environments. If you deployed Magento when first creating a project across all environments, the encryption key should have been saved to env.php. If you have not deployed previously, you should verify and add the encryption key if needed in every environment. Without this key, the store encounters authentication and authorization errors such as payments and shipping.

You copied the key in a previous step.

To add your Magento Commerce encryption key using the CRYPT_KEY environment variable:

  1. Log in to your Cloud project as an Admin user from the Project Web UI, or from the command line.

  2. Set the CRYPT_KEY environment variable to the encryption key value that you copied from the remote environment:

To add your Magento Commerce encryption key to the env.php file for each environment:

  1. If you have not done so already, use SSH to connect to the Cloud environment.

    1
    
    magento-cloud environment:ssh
    
  2. Open the app/etc/env.php file in a text editor.

  3. Replace the existing value of key with your Magento Commerce key.

    1
    2
    3
    4
    5
    6
    
    return array (
      'crypt' =>
      array (
        'key' => '<your encryption key>',
      ),
    );
    
  4. Save your changes to env.php and exit the text editor.

    You must add this key to the env.php file for all environments: Integration, Staging, and Production.

Import media

To import media files into your Cloud environment:

  1. If you have not done so already, use SSH to connect to the Cloud environment.

    1
    
    magento-cloud ssh -p <project ID> -e <environment ID>
    
  2. Enter the following command to clear existing media files:

    1
    
    rm -rf pub/media/*
    
  3. Enter the following command to extract the media files to the pub/media directory:

    1
    
    tar -xzf var/media.tgz pub/media
    

Clear the cache

On the Cloud environment, enter the following commands in the order shown:

1
bin/magento setup:upgrade
1
bin/magento setup:static-content:deploy
1
bin/magento cache:clean

After the cache flushes, enter exit to close the SSH tunnel.

Verify the import

To verify everything imported properly, perform the following tasks in your local Cloud development environment:

  1. On your Cloud environment, enter the following commands to find the information to log in to the Magento Admin and to view the storefront:

    1
    
    magento-cloud environment:url
    
  2. Log in to the Magento Admin using the username and password of your Magento Commerce system.
  3. Verify that the settings in the Admin are the same as your Magento Commerce system.
  4. Access the storefront.
  5. Confirm that categories, products, and other content display as expected.
  6. Test everything thoroughly.