How to deploy war to tomcat using jenkins pipeline?

后端 未结 3 947
北荒
北荒 2021-01-23 17:17

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 o

3条回答
  •  星月不相逢
    2021-01-23 17:51

    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 :

    • https://stackoverflow.com/a/37622865/3957754

    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 :

    • https://stackoverflow.com/a/40722537/3957754

    /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:

    • https://www.site24x7.com/
    • Pingdom
    • New Relic
    • etc

提交回复
热议问题