Build and deploy on local

Before pushing your code to your Starter or Pro Staging and Production environments, you should fully build on your local. Fully testing builds and deploys along with full site testing can reduce the risk of issues or delays for your final site deployment, and expose any issues early for debugging.

These tasks walk through:

  • Complete development on your local
  • Complete a full build and deploy process on your local (deploys to the associated active development environment)
  • Test fully before continuing deployment to Staging

For more information on the full five step process, see the Deployment process.

We highly recommend completing your testing in an Integration active environment and the Staging environment. Only complete final tests for going live in the Production environment. Your Staging environment is best for testing with code, data, and services including Fastly, New Relic, and others.

Update composer if you add extensions

If you modified your composer.json file to add modules, we recommend running the composer update command in a terminal. This command updates any dependencies in the composer.lock. During the build phase, we run composer install on a fresh clone of your Git branch of code to retrieve the latest dependencies.

Verify all required files in Git

Your Git branch must have the following files for building and deploying to your local, Integration, Staging, and Production environments:

  • auth.json in the root Magento directory. This file includes the Magento authentication keys entered when creating the project. If you need to verify the file and settings, see Troubleshoot deployment.
  • config.php if you use Configuration Management to manage Magento configuration settings
  • .magento.app.yaml is updated and saved in the root directory
  • services.yaml is updated and saved in magento/
  • routes.yaml is updated and saved in magento/

Test build your code locally before pushing

Sometimes you just want to test your build prior to pushing your code to Git. You can use a specific set of commands to build locally. Do NOT push the generated build files from this test to your remote Git branch. This is a test to ensure no issues occur before pushing to Git. Remember, when you push to the remote Git branch, a full build and deploy process begins automatically.

  1. SSH into your local Magento workspace.
  2. Move to another location to run your build. You should keep this build separate from your usual Git branch.
  3. Run the following command to build locally. The command builds the current project locally strictly to test the build without the full patching and commit process.

    1
    
    magento-cloud local:build
    

    For details, enter magento-cloud local:build --help.

  4. Watch for the results. A series of files will generate for the build. If you do not encounter errors, you can push code to the remote Git branch and continue.

If errors occur during the build, you can investigate and resolve the code issues. You should not commit the files from this build to Git.

To remove these test builds, you can use the magento-cloud local:clean command. For details, enter magento-cloud local:clean --help.

Push code to Git and Integration

Before you continue, push all current code to the remote Cloud server so that, in event of issues, you can recover the state of the Magento application.

To prepare your code and branch:

To get started with environment branches:

  1. On your local workstation, change to your Cloud project directory.

  2. Switch to the Magento file system owner.

  3. Log in to your Magento project.

    1
    
    magento-cloud login
    
  4. List your projects.

    1
    
    magento-cloud project:list
    
  5. List environments in the project. Every environment includes an active Git branch that contains your code, database, environment variables, configurations, and services.

    1
    
    magento-cloud environment:list
    

    It is important to use the magento-cloud environment:list command because it displays environment hierarchies, whereas the git branch command does not.

  6. Fetch origin branches to get the latest code.

    1
    
    git fetch origin
    
  7. Checkout, or switch to, a specific branch and environment.

    1
    
    magento-cloud environment:checkout <environment-ID>
    

    Git commands only checkout the Git branch. The magento-cloud checkout command checks out the branch and switches to the active environment.

    You can create a new environment branch using the magento-cloud environment:branch <environment-name> <parent-environment-ID> command syntax. It may take some additional time to create and activate a new environment branch.

  8. Use the environment ID to pull any updated code to your local. This is not necessary if the environment branch is new.

    1
    
    git pull origin <environment-ID>
    
  9. (Optional) Create a snapshot of the environment as a backup.

    1
    
    magento-cloud snapshot:create -e <environment-ID>
    

To push code to your remote environment:

  1. Change to your project root directory.
  2. Complete code commits in a terminal.

    1
    
    git add -A && git commit -m "<comment>"
    
    1
    
    git push origin <branch name>
    
  3. The build and deploy phases begin. Wait for the deployment to complete.

Build phase

During the build phase, we perform the following tasks:

  • Apply patches distributed to all Magento Commerce Cloud accounts
  • Apply patches we provided specifically to you
  • Enable modules to build
  • Compile code and the dependency injection configuration

The build also checks for a configuration file. If the file exists, the static file deployment is also completed during the build stage. If not, it is completed in the deployment stage.

Before you continue, you must know the file system path to any patch we provided specifically to you. Typically, hotfixes are in the <Magento root dir>/m2-hotfixes directory.

To build your site:

  1. Apply patches distributed to all Magento Commerce Cloud accounts.

    Enter the following command from the project root directory:

    1
    
    php vendor/magento/magento-cloud-configuration/patch.php
    

    Output includes the following:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    [2016-11-30 15:05:15] Copying static.php to front-static.php
    [2016-11-30 15:05:15] Command:git apply /var/www/html/magento2/vendor/magento/magento-cloud-configuration/patches/000-MAGETWO-57719-2.1.2.patch
    [2016-11-30 15:05:15] Status:0
    [2016-11-30 15:05:15] Output:array (
    )
    [2016-11-30 15:05:15] Command:git apply /var/www/html/magento2/vendor/magento/magento-cloud-configuration/patches/MAGETWO-52660-scd-improvement.patch
    [2016-11-30 15:05:15] Status:0
    [2016-11-30 15:05:15] Output:array (
    )
    
      ... more ...
    )
    
  2. Apply hotfixes and other patches provided to you:

    1
    
    git apply <path to patch>
    

    For example, to apply hotfixes:

    1
    
    git apply m2-hotfixes/<patch file name>
    

    If the m2-hotfixes directory is empty, skip this step.

    If patches are present, output from this command is similar to the patches command.

  3. Enable all missing modules.

    1
    
    ./vendor/bin/ece-tools module:refresh
    
  4. Compile code and the dependency injection configuration:

    1
    
    php bin/magento  setup:di:compile
    

    This command can take several minutes to complete and produces messages similar to the following:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    Compilation was started.
    0% 1 sec 54.0 MiB%message% 0/7 [>---------------------------]
    0% 1 sec 54.0 MiBProxies code generation...
    0/7 [>---------------------------]
    0% 1 sec 54.0 MiB
    Proxies code generation... 1/7 [====>-----------------------]  14% 1 sec 58.0 MiB
    Repositories code generation... 1/7 [====>-----------------------]  14% 1 sec 58.0 MiB
    Repositories code generation... 2/7 [========>-------------------]  28% 30 secs 176.0 MiB
    ...
    ...
    Interception cache generation... 7/7 [============================] 100% 5 mins 324.0 MiB
    

If you receive errors, debug them when possible and open a support ticket for further assistance.

We strongly recommend you complete your testing in an Integration or Staging environment only, and not in a Production environment.

Deploy phase

We highly recommend having Magento already installed prior to deployment. During the deployment phase, we perform the following tasks:

  • Install the Magento application if needed
  • If the Magento application is installed, upgrade components
  • Clear the cache
  • Set the Magento application for production mode

To deploy your site:

  1. If you have not already, log in as or switch to the Magento file system owner.
  2. Change to your project root directory.
  3. Enter the following command:

    1
    
    php bin/magento setup:upgrade
    

    We highly recommend having Magento already installed if you followed the First time deployment. If you have not installed the Magento application yet, use the magento setup:install command instead. Be advised, you may encounter issues with enabled modules on a fresh installation.

  4. Clean the Magento cache:

    1
    
    php bin/magento cache:clean
    
  5. Set the Magento application for production mode:

    1
    
    php bin/magento deploy:mode:set production
    

Debug errors by reviewing logs. Open a support ticket for additional assistance.

Next step

Prepare to deploy to Staging and Production