Deploy code and migrate static files and data

Previous step

Prepare to deploy to Staging and Production

To migrate your database and static files to Staging and Production:

If you encounter errors or need to change project configuration, complete the required updates in your local environment. Then, push the code changes to the Integration environment to deploy and test before pushing to Staging and Production environments.

Deploy code to Staging and Production

You can use the Project Web Interface or SSH and CLI commands to deploy your code to Staging and Production.

Deploy code with the Project Web Interface

The Project Web Interface provides features to create, manage, and deploy code in Integration, Staging, and Production environments for Starter and Pro plans.

For Pro projects, deploy the Integration branch you created to Staging and Production:

  1. Log in to your project.
  2. Select the Integration branch.
  3. Select the Merge option to deploy to Staging. Complete all testing.
  4. Select the Staging branch.
  5. Select the Merge option to deploy to Production.

For Starter, deploy the development branch you created to Staging and Production:

  1. Log in to your project.
  2. Select the prepared code branch.
  3. Select the Merge option to deploy to Staging. Complete all testing.
  4. Select the Staging branch.
  5. Select the Merge option to deploy to Production.

Use the merge option to deploy

Deploy code with SSH and CLI

You can use the Magento Cloud CLI commands to deploy code to Starter and Pro environments. You need SSH and Git access to your project. See prepare for deployment.

Prerequisites:

Step 1: Deploy and test the Integration environment:

  1. After logging into the project, check out the Integration environment:

    1
    
    magento-cloud environment:checkout <environment-ID>
    
  2. Synchronize your local Integration environment with the remote environment:

    1
    
    magento-cloud environment:synchronize <environment-ID>
    
  3. Create a snapshot of the environment as a backup:

    1
    
    magento-cloud snapshot: create -e <environment-ID>
    
  4. Update code in your local branch as needed.

  5. Add, commit, and push changes to the environment.

    1
    
    git add -A && git commit -m "Commit message" && git push magento <environment-ID>
    
  6. Complete site testing.

Step 2: Merge changes to Staging and deploy

  1. Check out the Staging environment:

    1
    
    magento-cloud environment:checkout <environment-ID>
    
  2. Synchronize your local Staging environment with the remote environment:

    1
    
    magento-cloud environment:synchronize <environment-ID>
    
  3. Create a snapshot of the environment as a backup:

    1
    
    magento-cloud snapshot: create -e <environment-ID>
    
  4. Merge the Integration environment to Staging to deploy:

    1
    
    magento-cloud environment:merge <Integration-ID>
    
  5. Complete site testing.

Step 3: Deploy to Production:

  1. Check out, synchronize, and create a snapshot of your local Production environment.

  2. Merge the Staging environment to Production to deploy:

    1
    
    magento-cloud environment:merge <Staging-ID>
    
  3. Complete site testing.

Migrate static files

You migrate static files from your pub/media directory to Staging or Production.

We recommend using the Linux remote synchronization and file transfer command rsync. The rsync utility uses an algorithm that minimizes the amount of data by moving only the portions of files that have changed. Rsync also supports compression.

Use the following command to migrate files:

1
rsync -azvP <source> <destination>

This command uses the following options:

  • a–archive
  • z–compress
  • v–verbose
  • P–partial progress

For additional options, see the rsync man page.

To migrate static files from your local machine, use the rsync command to copy the pub/media directory from your local Magento server to staging or production:

1
rsync -azvP local_machine/pub/media/ <environment_ssh_link@ssh.region.magento.cloud>:pub/media/

To migrate static files from remote-to-remote environments directly (fast approach):

To transfer media from remote-to-remote environments directly, you must enable ssh agent forwarding, see GitHub guidance.

  1. Open an SSH connection to the source environment.

    To find the SSH access link in your Project Web Interface, select the environment and click Access Site. The syntax for the SSH command is as follows:

    1
    
    ssh -A <environment_ssh_link@ssh.region.magento.cloud>
    
  2. Use the rsync command to copy the pub/media directory from your current environment to another remote environment:

    1
    
    rsync -azvP pub/media/ <destination_environment_ssh_link@ssh.region.magento.cloud>:pub/media/
    

Migrate the 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.

Prerequisite: A database dump (see Step 3) should include database triggers. For dumping them, confirm you have the TRIGGER privilege.

Important: The Integration environment database is strictly for development testing and can include data that you do not want to migrate into Staging and Production.

For continuous integration deployments, we do not recommend migrating data from Integration to Staging and Production. You could pass testing data or overwrite important data. Any vital configurations will be passed using the configuration file and setup:upgrade command during build and deploy.

We do recommend migrating data from Production into Staging to fully test your site and store(s) in a near-production environment with all services and settings.

To transfer media from remote-to-remote environments directly you must enable ssh agent forwarding, see GitHub guidance

To migrate a database:

  1. SSH into the environment you want to create a database dump from:

    1
    
    ssh -A <environment_ssh_link@ssh.region.magento.cloud>
    
  2. List the environment relationships to find the database login information:

    1
    
    php -r 'print_r(json_decode(base64_decode($_ENV["MAGENTO_CLOUD_RELATIONSHIPS"]))->database);'
    
  3. Create a database dump file in gzip format:

    For Starter environments and Pro Integration environments:

    1
    
    mysqldump -h <database host> --user=<database username> --password=<password> --single-transaction --triggers main | gzip - > /tmp/database.sql.gz
    

    For Pro Staging and Production environments, the name of the database is in the MAGENTO_CLOUD_RELATIONSHIPS variable (typically the same as the application name and username):

    1
    
    mysqldump -h <database host> --user=<database username> --password=<password> --single-transaction --triggers <database name> | gzip - > /tmp/database.sql.gz
    
  4. Transfer the database dump to another remote environment with the rsync command:

    1
    
    rsync -azvP /tmp/database.sql.gz <destination_environment_ssh_link@ssh.region.magento.cloud>:/tmp
    
  5. Type logout to terminate the SSH connection.

  6. Open an SSH connection to the environment you want to migrate the database into:

    1
    
    ssh -A <destination_environment_ssh_link@ssh.region.magento.cloud>
    
  7. Import the database dump:

    1
    
    zcat /tmp/database.sql.gz | mysql -h <database_host> -u <username> -p<password> <database name>
    

    The following example references the gzip file created by the database dump operation:

    1
    
    zcat /tmp/database.sql.gz | mysql -h database.internal -u user main
    

Troubleshooting the database migration

If you encounter the following error, you can try to create a database dump with the DEFINER replaced:

1
ERROR 1277 (42000) at line <number>: Access denied; you need (at least one of) the SUPER privilege(s) for this operation

This error occurs because the DEFINER for the triggers in the SQL dump is the production user. This user requires administrative permissions.

To solve this problem, you can generate a new database dump changing or removing the DEFINER clause as shown in the following example:

1
mysqldump -h <database host> --user=<database username> --password=<password> --single-transaction <database name>  | sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | gzip > /tmp/database_no-definer.sql.gz

Use the database dump file to migrate the database.

After migrating the database, you can set up your stored procedures or views in Staging or Production the same way you did in your Integration environment.

Related topics

Test deployment