问题
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
回答1:
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
回答2:
In tomcat, configuration with jenkins and tomcat:
- Install and download the jenkins on your server and start the server go to jenkins portal after that create the project using 'New Item' and select the pom.xml and create the maven project.
- Now go to your project and click on Configure and select the "Restrict where this project can be run" and add master in your Level Expression.
- select the "Source Code Management" clisck on git and configure your git repository and credential and branch name.
- Select the "Build" add Root pom : pom.xml and Goals and options : clean install -DskipTests
- select the "Post-build Actions" and select the "Deploy war/ear to a container"
- WAR/EAR files : target/test.war
- Context path : test
- Containers select tomcat and add Credentials
- Tomcat URL : example : http://localhost:8080/
Update the 'apache-tomcat-8.5.5\webapps\manager\META-INF\context.xlm file. uncomment the Value tag. and restart server
context.xml file
Before :
<Context antiResourceLocking="false" privileged="true">
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="192\.168\.0\.9|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
</Context>
After change :
<Context antiResourceLocking="false" privileged="true" >
</Context>
for auto deployment: go to 'apache-tomcat-8.5.5\conf\context.xml' and add antiResourceLocking="true" in 'Context' tag
回答3:
i think the best way shoul be the deploy plugin https://www.jdev.it/deploying-your-war-file-from-jenkins-to-tomcat
来源:https://stackoverflow.com/questions/54288611/how-to-deploy-war-to-tomcat-using-jenkins-pipeline