Build and Deploy Hooks in Jelastic PaaS

13

1159-1-deployment-hooksHook is a term used to indicate a code insertion into some standard operation to provide customization. In confines of Jelastic PaaS platform, you are allowed to execute your custom script prior / after an application build / deployment operation. 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);
     1159-1-deployment-manager
  • Deployments buttons for application server.
     1159-1-deployments-buttons-for-application-server

1. Within the opened frame, expand the Hooks section.

1159-1-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 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.

1159-1-edit-post-deploy-hook

This way, depending on an engine your container is running on, you can call 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;
    1159-1-java-script-to-fetch-container-engine-version
  • PHP;
    1159-1-php-script-to-fetch-container-engine-version
  • Node.js.
    1159-1-nodejs-script-to-fetch-container-engine-version

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.

1159-1-edit-pre-deploy-hook

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.

1159-1-click-show-logs-to-view-details-on-handled-processes-and-result

Tip: Subsequently, the dedicated hooks.log file can be viewed within the application server Log section:

1159-1-view-hooks-log-file-within-application-server-log-section

In order to explore the logged data on Build hooks execution, check the same file at appropriate Maven build node:

1159-1-check-hooks-log-file-at-maven-build-node

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.

1. 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.

1159-1-deploy-helloworld

2. Expand the Hooks section, click on the Pre hook and paste the following code into the opened editor frame:

echo “$(date) – deployment start” >> ~/mylog

1159-1-pre-hook-deployment-start

This will add a string into the mylog 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

1159-1-code-for-post-deploy-hook

Tip: If needed, you can use the exit command 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 your mylog file to check whether the hooks we’ve stated were successfully executed:

1159-1-mylog-file-to-check-hooks-were-executed-successfully

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 Jelastic Deployment Manager) and configure the following Post deploy hook:

sudo service tomcat restart

1159-1-configure-post-deploy-hook

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.

1159-1-view-hooks-log-to-get-a-full-response-on-hook-execution

In such a way you can start working with your app immediately after deployment without any additional steps required.

Try out the hooks usage for yourself with a free trial at our Jelastic PaaS platform.

If you face any issues when working with hooks, feel free to appeal for Jelastic technical expert assistance at Stackoverflow.
SOURCEJelastic, Inc.