0

I am using a job in Junkins to build my application (.ear) and then deploy it in Glassfish. I want to execute asadmin undeploy myApp before I deploy my application (the new version). The problem is in the 1st execution there is no application deployed so executing asadmin undeploy myApp generates an error. Any suggestion to deal with this situation. Any proposition is the most welcomed. Thank's.


EDIT :

Correct me if I am wrong in my method, maybe I am doing things wrong! Is this the right way to have a chain of production of a sowtware? Do I have to stop the server and restart it?

ziMtyth
  • 1,008
  • 16
  • 32

2 Answers2

1

I never used Glassfish, but you could check if your app is deployed before to execute the undeploy.

If you know the port in which your app should be in execution, you could simply check with netstat or lsof.

EDIT:

Have a look to this doc (Example 2–3 Listing Applications), seems that you can see that with:

list-applications --type web

Regarding this:

Correct me if I am wrong in my method, maybe I am doing things wrong! Is this the right way to have a chain of production of a sowtware? Do I have to stop the server and restart it?

I think the correct answer is that it depends on the web server you are using (for example Glassfish provide the autodeploy). But generally, the approach works.

Davide Patti
  • 3,391
  • 2
  • 18
  • 20
  • Thank you for the reply, but I don't think this could work in my case because my application is running on port 8080 which is the same port as glassfish. If just I could add a parameter to `asadmin undeploy myApp` saying `undeploy if exist`, I'm searching in this way right now. – ziMtyth May 17 '18 at 11:05
  • Indeed it could be the start of a solution, but I am having hard time trying to use in it in Jenkins. I will use a conditional build. I'm struggling to find how to write a condition that takes the list of apps and check if my app is in. – ziMtyth May 17 '18 at 11:40
  • @ziMtyth How are you running 2 application on the same port ?? It shouldn't allow you to do that as they will be listening to the same port which will raise an issue...secondly the above is the right way to do ..list all the application on glassFish if your application is there then remove it – rohit thomas May 18 '18 at 03:48
  • @ziMtyth Actually it is quite simple. Have a look to this answer: https://stackoverflow.com/a/40888664/3881320 You need to create a script (I suggest using bash) and insert this check. At the end is just an if statement parsing the output of a command. If from the output you don't see your application, don't do nothing. Otherwise you can execute your undeploy. – Davide Patti May 18 '18 at 06:21
  • @ziMtyth this is an example https://stackoverflow.com/a/23256906/3881320 but be careful because this script will undeploy (if there are) all the deployed app. – Davide Patti May 18 '18 at 07:26
0

After watching some videos on Bash and with the help provided by Davide Patti, I figured out how to do it.

Knowing that I used the answer of Davide Patti and I thank him for his help I choosed to write my own answer for a simple reason: Patti's answer didn't work.

In order to test if an application is deployed and undeploy it if it is deployed I used the following Bash code which worked for me:

apps=`asadmin list-applications -t --user=admin --passwordfile=password.txt`

for app in $apps
do
    if [ $app = "the_name_of_your_app" ]
    then
        asadmin --user=admin --passwordfile=password.txt undeploy the_name_of_your_app
    fi
done;

PS: the content of password.txt is a single line: AS_ADMIN_PASSWORD=admin

ziMtyth
  • 1,008
  • 16
  • 32
  • I wrote, at the start of my answer, that I never used glassfish. And certainly I wouldn't have done that just to solve your answer. You asked for a proposition/suggestion. You cannot expect the exact code of your solution (even because, stack overflow, didn't work in this way). If you think my answer didn't help you to solve the question, you did well to not accept it. – Davide Patti May 21 '18 at 09:27
  • @DavidePatti in fact your answer helped alot and I thanked you for that, I believe you don't have to explain any thing. I appreciated your help =) – ziMtyth May 21 '18 at 09:38