When running tasks on your site such as database copies or code deploys, the hooks located in your hooks/common directory will automatically initiate. While this may be appropriate in most cases, there may be scenarios where you don’t want your hooks to fire automatically for all environments. For example, hooks which require files to be present will always fail on the RA environment which in-turn will prevent the RA update process from completing successfully.
The solution to limiting hooks from running on all environments is to add small code changes which will take into account the environment being worked on, before going ahead and running the hook. There are a couple of different methods you could implement to achieve this.
Firstly you could wrap the entire contents of your hook in an if statement which will check if the target environment is NOT the one specified. If the environment is the one specified the hook will not start, however if the target environment is any other environment then it will. Here is an example of how this code would be written:
/* * Keep you variables here above the if statement */ if [ "$target_env" != "ra" ]; then /* * Main hook code should be here */ fi
A second and perhaps more simplified version of this would be to specify, if the target environment is the environment specified, then exit from the hook entirely. To implement this you should include something similar to the following:
# Do not run on the RA environment if [ "$target_env" == 'ra' ]; then exit fi
Each of these approaches do very similar things so the choice of which one to implement is really down to which one is more appropriate for your team and their coding practices.