Using Magerun with deploy hooks to prepare environments

For a brief introduction of Magerun or how to install it, please see this Confluence page: Magerun.

The purpose of this article

The purpose of this article is to show you how to use Magerun (or Magerun 2 for Magento 2) scripts in your projects in conjunction with Aja hooks to automatically set Magento config during deployment. We will be using different config values for each environment while keeping with the DRY (Don’t Repeat Yourself) principle.

Prerequisites

Before you add Magerun hooks you must understand how to add Magerun scripts to your project’s local repository. See this Confluence page: Using Magerun modules in your projects.

Summary of what happens during deployment

  1. Aja hook file executes correct Magerun script
  2. Correct config for environment is set
  3. Cache is cleared (you can add any Magerun command to your scripts)
  4. Output of Magerun is logged to $webRoot/var/log/n98-magerun.log.

The hook file

The hook file contains the script that actually triggers the Magerun scripts to be executed based on which environment is being deployed.

Example hook file

Magento 2

You can see an example of a working Magerun hook file for Aja deployment in this project: project_dalema:hooks/n98-magerun2.sh.

hooks/n98-magerun2.sh

1

2

3

4

5

6

7

8

9

10

#!/bin/bash

 

# Execute environment configuration adjustments

echo `date` “#” ${AJA_TASK_USER} “#” ${AJA_BUILD_NUMBER} “#” ${AJA_BUILD_REVISION} >> ${AJA_WWWROOT_PATH}/var/log/n98-magerun.log

echo ‘Magerun EXECUTE: /usr/local/bin/n98-magerun2.phar –skip-root-check –root-dir=”‘${AJA_WWWROOT_PATH}‘” script:repo:run set-‘${AJA_ENVIRONMENT_NAME}‘-config’

 

export HOME=/tmp

n98-magerun2 –root-dir=“${AJA_WWWROOT_PATH}” script:repo:run set-${AJA_ENVIRONMENT_NAME}-config >> ${AJA_WWWROOT_PATH}/var/log/n98-magerun.log

 

echo ‘Magerun DONE’

Explanation of hook file

In line number 4 we simply echo a log entry to $webRoot/var/log/n98-magerun.log. The log entry contains:

  1. The time (`date`)
  2. Aja task user (${AJA_TASK_USER})
  3. Aja build number (${AJA_BUILD_NUMBER})
  4. Aja build revision (${AJA_BUILD_REVISION})

In line number 7 we ensure that the deploy environment has the HOME environment variable. Otherwise Magerun will exit with this exception:

[RuntimeException]

Unable to get home-directory to obtain user-config-file.

In line 8 we execute a Magerun script with a couple features:

  1. Notice that we execute only “n98-magerun2“. This is not a binary but rather an alias to the Magerun binary that hosting should have added to all servers. It ensures that Magerun is run with the correct user.
  2. The script name is dynamic and based on Aja environment name. Eg: script:repo:run set-dev-config. (script:repo:run set-${AJA_ENVIRONMENT_NAME}-config)
  3. We log the output of Magerun command to $webRoot/var/log/n98-magerun.log.

Since the hook dynamically calls Magerun scripts named after the current environment, you need to make sure that each environment has a script.

Magento 1

hooks/n98-magerun.sh

1

2

3

4

5

6

7

8

9

10

11

12

#!/bin/bash

# Execute environment configuration adjustments

#n98-magerun –root-dir=”${AJA_WWWROOT_PATH}” script:repo:run set-${AJA_ENVIRONMENT_NAME}-config

mkdir -p ${AJA_WWWROOT_PATH}/var/log

magerun_binary=`which n98-magerun.phar`

if [[ -x “$magerun_binary” ]]

then

    echo “Magerun command: $magerun_binary –skip-root-check –root-dir=${AJA_WWWROOT_PATH} script:repo:run set-${AJA_ENVIRONMENT_NAME}-config >> ${AJA_WWWROOT_PATH}/var/log/n98-magerun.log 2>&1”

    $magerun_binary –skip-root-check –root-dir=“${AJA_WWWROOT_PATH}” script:repo:run set-${AJA_ENVIRONMENT_NAME}-config >> ${AJA_WWWROOT_PATH}/var/log/n98-magerun.log 2>&1

else

    echo “Could not run Magerun”

fi

 

The Magerun scripts

Example environment script

lib/n98-magerun2/modules/dalema/scripts/set-dev-config.magerun

 Expand source

Notice that the top and bottom lines  references external scripts. This is to avoid repeating ourselves when setting similar config across environments.

Also notice that the script name is set-dev-config.magerun. This script will automatically be run when deploying to the Aja environment named “dev”. See the hook file above.

Example of external Magerun script that is being reused

Notice that in the set-dev-config.magerun file above there is a call to script:repo:run common-test-config. Below you see the content of that file.

lib/n98-magerun2/modules/dalema/scripts/common/common-test-config.magerun

 Expand source

Again, see examples of all files in the project_dalema repository.

Preparing environments manually

You can also prepare environments manually by logging on to you server and running Magerun manually. Here is an example for setting up the dev environment:

Running Magerun script manually on server

$ cd /mnt/www/$instanceNumber

$ n98-magerun2 script:repo:run set-dev-config

This is particularly useful if you use Server Portal to sync live database into your test environment.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s