Hook (or webhook) is a procedure of code insertion into some standard operation to apply some customization. In confines of the platform, this functionality allows you to execute your custom scripts before and/or after the application deployment operation. Herewith, for Maven build node and Golang application server, the pre- and post- project build hooks can be additionally set up.
So below we’ll examine how to operate with hooks at the platform and will overview several common use cases with step-by-step instructions this functionality can come in handy for:
Hooks Use Cases
Being a part of the deployment process, Hooks are available within an expandable section of the appropriate dashboard form. So, in order to manage hooks, access the application deployment dialog using one of the following options:
Here, click on either Pre or Post button to provide your code, which will be executed respectively just before/immediately after the deployment (according to the chosen option).
Tip: Within the top pane, you have an access to the following tools to help you during code editing:
- Wrap lines - brakes text to continue it in the line below if it reaches the frame border
- Search - allows to easily find the needed information; is supplied with the additional Match case and Regex search options
- Help - redirects to the current document to get the details on proper hooks usage
Click Apply when ready. Now you can deploy your application.
Note: In case something goes wrong during the hook execution, you’ll get the appropriate notification, whilst the deployment process will be aborted:
Click the Show Logs button in order to get the details on the occurred error by viewing the deployment action log (which corresponds to the hooks.log file, which can be accessed through the Logs section for the appropriate server).
Hooks provide a broad range of opportunities for developers, allowing to automate the majority of routine processes to get a ready-to-work application just after the deployment.
As an example, below we’ve gathered a number of the most common tasks that could be programmed to be automatically accomplished by hooks:
Below, we’ve provided a simple example of your own log file creation with the help of hooks.
echo "$(date) - deployment start" >> ~/mylog if ! grep -q "$(pwd)/mylog" /etc/jelastic/redeploy.conf; then echo "$(pwd)/mylog" >> /etc/jelastic/redeploy.conf fi
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.
echo "$(date) - deployment end" >> ~/mylog
Tip: If needed, you can use the exit command, which 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.
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.