Hook is a term used to indicate a code insertion into some standard operation to provide customization. In confines of Apiqcloud Platform, you are allowed to execute your custom script prior / after an application build/deployment operations. Such implementation allows to completely automate your solution delivery by setting all the required application customizations within a single service panel to subsequently apply them all at once during a single deploy action.
Below, we’ll overview how to work with hooks and provide a list of the common use cases with a pair of simple step-by-step instructions.
Hooks Management
Being a part of the deployment process, Hooks can be set directly in the application deployment dialog. The appropriate form will be shown when referring to one of the following options:
- Deployment Manager > Deploy to… icon (this tab is located in the dashboard bottom)
- Deployments buttons for application server
- Within the opened frame, expand the Hooks section.
Note: For the Maven build node and Golang application server, the Build hooks can be additionally provided alongside with the Deploy ones.
Here, click on either the Pre or Post button to provide your code, which will be executed just before or immediately after deployment.
2. Within the opened code editor window, you can use any preferable programming language – the only requirement here is the presence of the appropriate code interpreter being pre-installed on a container.
This way, depending on an engine your container is running on, you can call the execution of the appropriate programming language scripts within your hooks body. For example, to fetch the exact container engine version and store it within server logs, the following strings can be specified as a hook:
- Java
- PHP
- Node.js
Tip: To make the work with your scripts code more convenient, the top editor pane provides a few supplementary options, namely:
- Wrap lines – brakes line if it reaches the frame border
- Search – allows to easily find the needed information, with the additional Match case and Regex search options
- Help – redirects to the current document to provide hooks usage details
Click Apply when ready and proceed with application deploy.
3. When the deployment is finished, you can click the Show Logs button within the appeared dashboard notification to view the details on the handled processes and their result.
Hooks Use Cases
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-build/deploy operations (i.e. executed before building and/or deploying application)
- to check if all of the requirements are met
- to pre-install required software
- to clear or prepare a dedicated folder for an application
- to log data
- Post-build/deploy actions (i.e, executed after the application build and/or deployment is actually finished)
- to restart your application server after deployment
- to apply any other preferred customization
- to log data
Below, we’ve provided two step-by-step examples of the hooks usage – Actions Logging and Application Restart.
Actions Logging with Hooks
Check out a simple example of your own log file creation with the help of hooks.
- Initiate deployment of your project in any preferable way. We’ll use the HelloWorld.zip archive, available out-of-box for each newly created account within the Deployment manager.
- Expand the Hooks section, click on the Pre hook and paste the following code into the opened editor frame:
echo “$(date) – deployment start” >> ~/my log
This will add a string into the log file (will be automatically created in the home server directory if does not exist) to indicate the deployment start time.
3. For the Post hook add the next code:
echo “$(date) – deployment end” >> ~/mylog
Here, we just log the end of our deployment.
4. Finally, deploy your application and check your mylog file to check whether the hooks we’ve stated were successfully executed:
In our case, the presence of the custom mylog file with application deployment start/end timestamps inside it indicates that our scripts have worked as intended.
Application Restart with Hooks
Let’s check another example of hooks usage – e.g. with restarting your Tomcat application server right after the project deployment.
1. Initiate deployment of any preferred application (we’ll use the default unified HelloWorld package from Apiqcloud Deployment Manager) and configure the following Post deploy hook:
sudo service tomcat restart
That’s it! Now, your Tomcat application server will be automatically restarted after deployment.
2. Open the Log section and view hooks.log to get a full response to your hook execution.
In such a way you can start working with your app immediately after deployment without any additional steps required.