42

I'm writing a shell script to auto deploy/undeploy using the tomcat manager.

Following the instructions on http://tomcat.apache.org/tomcat-6.0-doc/manager-howto.html#Deploy_A_New_Application_Remotely, I use curl for my deployment

curl --anyauth -u username:pwd -d path=/something -d war=file:target/someWar.war https://someurl.com/manager/deploy

And I get the response saying HTTP method POST is not supported by this URL.

So I change my curl to be a get using -G

curl --anyauth -u username:pwd -G -d path=/something -d war=file:target/someWar.war https://someurl.com/manager/deploy

I get a response of FAIL - Failed to deploy application at context path /something and it seems to be looking for the file locally on the server instead of my machine. There are pluings which do remote deploy without having to scp the file over so I'm wondering what I'm missing.

I'm currently out of ideas (I don't see any other option on the tomcat manager configuration page).

Braiam
  • 1
  • 11
  • 47
  • 78
bluesman
  • 2,242
  • 2
  • 25
  • 35

6 Answers6

59

Providing an update to this question.

Tomcat 7 has changed it's manager API.

Please refer to: Manager commands

Following new URL pattern :

http://{host}:{port}/manager/text/{command}?{parameters}

Example

curl -T "myapp.war" "http://manager:manager@localhost:8080/manager/text/deploy?path=/myapp&update=true"

Security

Keep in mind the server must be able to accept your remote IP. This is a sample configuration:

<Context privileged="true" antiResourceLocking="false"
         docBase="${catalina.home}/webapps/manager">
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.0\.0\.1" />
</Context>

This is an optional setting and isn't required but having Cross domain role and proper manager credentials is a must.

Tomcat 8 - the same rules apply as Tomcat 7. Same commands.

Here is a full documentation:

http://tomcat.apache.org/tomcat-8.0-doc/manager-howto.html

Ian
  • 50,146
  • 13
  • 101
  • 111
jeveloper
  • 890
  • 8
  • 10
  • 9
    It should be noted that the user deploying to tomcat needs to have `manager-script` role enabled in order to access the text interface. – Franklin Apr 14 '14 at 17:53
  • can we do this for deploying in a remote machine tomcat? – jos Jul 04 '14 at 10:33
  • @jos You absolutely can. As long as your firewall allows connection from outside. You're also encouraged to use HTTPS – jeveloper Aug 11 '14 at 18:22
  • this saves me a lot of time when deploying to a remote machine which has a cluster (same WAR for every instance) and for which I have ssh access – Riccardo Cossu Dec 17 '14 at 11:24
  • Hi Friend, I am able to upload war file by your example and it is working fine. One Issue: When i fire this curl on Linux machine, it deployed on server, but goes on wait forever. any idea? – ankush yadav Nov 27 '15 at 16:05
45

This way is working for me on Tomcat 6 (See jevelopers answer for tomcat 7):

curl --upload-file <path to warfile> "http://<tomcat username>:<tomcat password>@<hostname>:<port>/manager/deploy?path=/<context>&update=true"

Example:

curl --upload-file target\debug.war "http://tomcat:tomcat@localhost:8088/manager/deploy?path=/debug&update=true"

Very easy peasy. Output is like this:

OK - Undeployed application at context path /debug
OK - Deployed application at context path /debug
s3v1
  • 2,923
  • 2
  • 33
  • 25
  • 1
    Got my info from the cURL manual: http://curl.haxx.se/docs/httpscripting.html check out section 5 – s3v1 Mar 07 '11 at 17:43
  • 2
    This does not work for Tomcat 7. Take a look at [the answer from jeveloper](http://stackoverflow.com/a/13367460/428628) – Michael R Apr 03 '13 at 14:51
  • 1
    Thanks, Michael R! I updated the text to say Tomcat 6 and added reef to jevelopers answers... just in case someone doesn't read comments – s3v1 Apr 09 '13 at 16:18
  • thanks a lot! I confirm it works on Tomcat 8.5.29 as follows: curl.exe -v -u "user:pass" --upload-file /path/to/my-war.war "http://localhost:8080/manager/text/deploy?path = /my-context-path&update = true" – a.parfenov Apr 04 '18 at 15:31
3

For those who use Jenkins and want to deploy using shell script in GitBash on a Windows machine instead of Jenkins deploy plugin

tomcat_host=192.10.10.100
tomcat_port=8080
tomcat_username=admin
tomcat_password=12345

context_path=myApplication

curl -v -u ${tomcat_username}:${tomcat_password} -T ${artifact} 'http://'${tomcat_host}':'${tomcat_port}'/manager/text/deploy?path=//'${context_path}''

Note:

  1. curl -v option is verbose (optional)
  2. // two forward slashes before the context path works for GitBash on a Windows machine (/ single forward slash will not somehow)
  3. Also when deploying on a remote server, consider your firewall yeah!
JRichardsz
  • 14,356
  • 6
  • 59
  • 94
Jet
  • 661
  • 5
  • 11
3

Improving Jet answer, this works for me in tomcat 8, java 64 bits.

This was what I execute:

curl -v -u some_user:some_password -T /../my_app.war 'http://127.0.0.1:tomcat_port/manager/text/deploy?path=/my_app&update=true'

This will work if we configure tomcat users in :

/.../.../apache-tomcat-8.5.0_001/conf/tomcat-users.xml

with:

<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>

<user username="some_user" password="some_password" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/>

Restart tomcat and it will be ready to deploy wars from remote clients like curl, jenkins, travis, etc

JRichardsz
  • 14,356
  • 6
  • 59
  • 94
1

The easiest way to deploy an app is to write an Ant script. The only other thing (apart from Ant) you will need is catalina-ant.jar to be present in the classpath.

Have a look at this chapter of the manual: http://tomcat.apache.org/tomcat-6.0-doc/manager-howto.html#Executing_Manager_Commands_With_Ant

The script does a similar thing: uses HTTP to deploy your .war to the manager app. You might even want to capture the packets to see the exact headers if you still want to use curl. I would not recommend curl though as I think Ant solution is more portable and error-prone (e.g what if they will change low level deployment API?).

mindas
  • 26,463
  • 15
  • 97
  • 154
  • Thanks for the response. I guess as a worst case scenario I will use ant, but it would be nice to use some sort of deploy mechanism without any dependencies (easier to scale out). – bluesman Dec 14 '10 at 16:20
  • The catalina-ant.jar is part of every Tomcat installation so I am not sure if understood your point. Even if you (re)deploy thousands of webapps every day, the overhead of launching JVM should be negligible. – mindas Dec 14 '10 at 16:38
  • 1
    We're not concerned about the server, to deploy using ant you still need ant installed on the client machine. – bluesman Dec 15 '10 at 04:29
0

I was getting error

curl: Can't open webapp.war 

when I only mentioned

curl -T 'webapp.war'

But it worked when I used the complete path of build artifact like

curl -T ./target/webapp.war
Bimal
  • 1,175
  • 8
  • 16
Rohit R.
  • 51
  • 5