Hooks provide a broad range of opportunities for developers, which allow to automate many processes and get a ready-to-work application without any manual steps required. For example, here are the most common tasks, that could be programmed to be automatically accomplished by hooks:
- Pre deploy (before actually deploying application)
- to check if all of the requirements are met
- to pre-install required software
- to clear or prepare a dedicated folder for application
- to log data
- Post deploy (after deployment is finished)
- to restart your application server after deployment
- to install project dependencies
- to apply any other preferred customization
to log data
Below, we’ve provided a simple example of your own log file creation with the help of hooks.1. Initiate deployment of your project using any preferable way. We’ll use the default HelloWorld.zip archive from a deployment manager.
2. Expand the Hooks section, click on the Pre hook and provide the following code within the opened editor:
echo "$(date) - deployment start" >> ~/mylog
if ! grep -q "$(pwd)/mylog" /etc/jelastic/redeploy.conf; then
echo "$(pwd)/mylog" >> /etc/jelastic/redeploy.conf
This will add a string into the mylog file (will be automatically created in the home directory, if not exists), which will identify deployment start and provide the appropriate time stamp. Also, we check if the redeploy.conf file includes our custom log file and, if not, add the appropriate line - in such a way it will be kept after container redeploy operation.
3. For the Post hook add the next code:
echo "$(date) - deployment end" >> ~/mylog
Tip: If needed, you can use the exit command allows to break your hook and the appropriate deployment / build operation execution at any point. Herewith, the 0 value (i.e. exit 0) is used to indicate success, while any other value assumes an error (e.g. exit 1).
Here, we just log the end of our deployment.4. Finally, deploy your application and check both mylog and redeploy.conf file to verify hooks successful execution.
As you can see, our scripts have worked as intended, providing the deployment start / end time and ensuring it is protected during redeploy operation.
If you face any issues while working with hooks, feel free to appeal for our technical experts’ assistance at Stackoverflow