How to deploy war to tomcat using jenkins pipeline?

10,101

In tomcat, there are two options to deploy a war:

  • copy war to webapps folder
  • upload war to /manager/text/deploy http endpoint published by your tomcat

Here some approaches to deploy a war and get the deployment status (success|failure)

You can put one of the following snippets in the deploy stage of your pipeline or migrate to groovy.

/manager/text/deploy

This is an endpoint which allow us to upload war from remote host to the tomcat server and as response:

  • Http status 200 for success or failure without distinction
  • Http Body like :

OK - Deployed application at context path /foo

FAIL - Deployed application 
at context path /my_app 
but context failed to start 

So, in order to detect is everything is ok, I perform this validation:

CURL_RESPONSE=$(curl -v -u $TOMCAT_USER:$TOMCAT_PASSWORD -T $WAR_PATH "http://$TOMCAT_HOST:$TOMCAT_PORT/manager/text/deploy?path=/$CONTEX_NAME&update=true")    

if [[ $CURL_RESPONSE == *"FAIL"* ]]; then
  echo "war deployment failed"
  exit 1
else    
  echo "war deployed successfully "
  exit 0
fi

Here you can find the required configurations to enable this endpoint :

Copy war file to webapps

After to copy war file to webapps, you can list the deployed apps, and find the name of your application in the http body response:

OK - Listed applications for virtual host localhost
/manager:running:0:manager
/:running:0:ROOT
/docs:running:0:docs
/examples:running:0:examples
/host-manager:running:0:host-manager
/my_app:running:0:my_app
/my_other_app:running:0:my_other_app

You can use a loop with a break as maximum attempts.

Here you can find the required configurations to enable this endpoint :

/health or /status

This is more clean and As I know, several monitoring platforms use this strategy.

All consist in expose an extra http endpoint in your application (web app, api rest ,daemon, etc)

This endpoint must return one of the following responses:

  • http stasus

    • (200) : Indicating that everything in your application is ok
    • (!200): Indicating that your app has problems. If your application was not deployed correctly, this endpoint will return 404.
  • xml or json

{
 "status":"200",
 "database_connectivity":"200",
 "read_write_disk":"200",
 "etc":"etc"
}

Finally you can use a loop to consume this /health endpoint from your Jenkins pipeline. This strategy will allow you to monitoring your apps from external platforms like:

Share:
10,101
Amit Swain
Author by

Amit Swain

Updated on June 14, 2022

Comments

  • Amit Swain
    Amit Swain almost 2 years

    I want to deploy war file using pipeline. What is the correct way to do it . Is there any way to use Deploy to container in pipeline code. The problem with calling catalina.sh or using curl command to deploy using jenkins manager is that I do not find any way to detect successful deployment.

    Is there any standard way to do that

    • JRichardsz
      JRichardsz over 5 years
      Are you using scripted pipeline or declarative pipeline in jenkins? Also is this your main question : How to detect successful deployment in tomcat?
    • hakamairi
      hakamairi over 5 years
      Too broad, you have to share some code or at least some more details.
    • Amit Swain
      Amit Swain over 5 years
      @JRichardsz using groovy syntax
    • JRichardsz
      JRichardsz over 5 years
      Did my answer help you?
  • Amit Swain
    Amit Swain over 4 years
    can it be used in pipeline groovy script ?