如何使用詹金斯管道向Tomcat部署战争? [英] How to deploy war to tomcat using jenkins pipeline?

查看:120
本文介绍了如何使用詹金斯管道向Tomcat部署战争?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用管道部署war文件. 什么是正确的方法.有什么方法可以在管道代码中使用部署到容器". 调用catalina.sh或使用curl命令使用jenkins manager进行部署时出现的问题是我找不到检测成功部署的任何方法.

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

推荐答案

在tomcat中,有两种部署战争的选择:

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

  • 将war复制到webapps文件夹
  • 将战争上传到您的tomcat发布的/manager/text/deploy http端点
  • 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.

这是一个端点,它使我们可以将战争从远程主机上传到tomcat服务器并作为响应:

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

  • 成功或失败的Http状态200,不加区别
  • Http正文为:

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 :

将war文件复制到webapp之后,您可以列出已部署的应用,并在http正文响应中找到您的应用的名称:

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 :

这更干净,而且据我所知,有几个监视平台都在使用这种策略.

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

所有步骤都包括在您的应用程序(网络应用程序,api rest,守护程序等)中暴露一个额外的http终结点

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):表明您的应用程序中的一切正常
  • (!200):表明您的应用有问题.如果您的应用程序部署不正确,则此端点将返回404.

xml或json

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

最后,您可以使用循环从Jenkins管道使用此/health端点.通过该策略,您可以从以下外部平台监视应用程序:

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

这篇关于如何使用詹金斯管道向Tomcat部署战争?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆