how to tell jenkins to restart tomcat after deployment? - maven

unfortunately I am new to jenkins and linux and hope somebody can help me here.
It is about the automatic build for our system. We use jenkins job to updatethe web system. after updating, tomcat and the system shoud be restarted. For updating the system we use the following execution command:
bash -l distr/deploy.sh -s /distr -a /data/mySystem -c /opt/apache-tomcat-8.0.5 2>&1 | tee log.log
how to tell jenkins to start tomcat after deployment?

This tutorial is for Tomcat 7, but the idea should be similar for v8: http://www.jdev.it/deploying-your-war-file-from-jenkins-to-tomcat/

Related

springboot project building non-stop server on AWS EC2

I made java springboot project for the first time. SO need some advice.
I used gradle, war.
I git-pushed my project on AWS EC2.
and run java -jar filename.war which succeeded data communication with frontend.
But when I leave the terminal with ctrl +c , the java server stops.
Which way is the best to build non-stop server like Node pm2?
In my EC2 virtual machine, there are several projects and I use NginX to route each projects to different port numbers.
Thanks in advance!
Linux
You can use nohup:
nohup java -jar -Djava.awt.headless=true your_server.war > /path/log.out &
Later you can use:
ps aux // to get the $PID of your process
kill -9 $PID // to kill the process
Windows
Please check this answer

how to check the Jboss (EAP 7) server running status via shell script

I have multiple jboss instance on a particular linux box . I don't want to grep the process id of jboss instance and listen port .Is there any work around from server.log or CLI command ?
In addition to the Panagiotis Chavarioti´s answer, the status of the application component also can be checked with the commands which are handy.
To check if JBoss server is started:
$JBOSS_HOME/bin/jboss-cli.sh -c --commands="read-attribute server-state"
To check if application is started:
$JBOSS_HOME/bin/jboss-cli.sh -c --commands="cd deployment,cd myapp.war, read-attribute status"
example:
$JBOSS_HOME/bin/jboss-cli.sh -c
'/host=sandbox/server=server1:readattribute(name=server-state)'
Try the following CLI command:
$JBOSS_HOME/bin/jboss-cli.sh -c
'/host=<HOSTNAME>/server=<SERVERNAME>:readattribute(name=server-state)'

Nohup didn't work in Jenkins shell

I want my JBoss server to run in background, for that I am using nohup ./startPID.sh > /dev/null 2>&1& command. But when I pass same command in Jenkins, it doesn't work as expected. The console output in Jenkins says "command ran successfully" but in backend the JBoss server is still down.
Any inputs?
Regards
Manish Mehra
Use "at now" instead of "nohup"
In your job jenkins (execute shell) put :
set +e #so "at now" will run even if java -jar fails
#Run java app in background
echo "java -jar $(ls | grep *.jar | head -n 1)" | at now + 1 min
You could look at the JBoss management plugin
which spins up JBoss for you
This plugin allows to manage a JBoss Application Server during build
procedure.
With the plugin we can start/stop JBoss AS. It's very useful if we
need to run some integration tests against of the server. There is
also operation allows verification if artifacts are deployable.
It looks to be quite an old plugin but has cuurent users

How to deploy SpringBoot Maven application with Jenkins ?

I have a Spring Boot application which runs on embedded Tomcat servlet container mvn spring-boot:run . And I don’t want to deploy the project as separate war to standalone Tomcat.
Whenever I push code to BitBucket/Github, a hook runs and triggers Jenkins job (runs on Amazon EC2) to deploy the application.
The Jenkins job has a post build action: mvn spring-boot:run, the problem is that the job hangs when post build action finished.
There should be another way to do this. Any help would be appreciated.
The problem is that Jenkins doesn't handle spawning child process from builds very well. Workaround suggested by #Steve in the comment (nohuping) didn't change the behaviour in my case, but a simple workaround was to schedule app's start by using the at unix command:
> echo "mvn spring-boot:run" | at now + 1 minutes
This way Jenkins successfully completes the job without timing out.
If you end up running your application from a .jar file via java -jar app.jar be aware that Boot breaks if the .jar file is overwritten, you'll need to make sure the application is stopped before copying the artifact. If you're using ApplicationPidListener you can verify that the application is running (and stop it if it is) by adding execution of this command:
> test -f application.pid && xargs kill < application.pid || echo 'App was not running, nothing to stop'
I find very useful to first copy the artifacts to a specified area on the server to keep track of the deployed artifacts and not to start the app from the jenkins job folder. Then create a server log file there and start to listening to it on the jenkins window until the server started.
To do that I developed a small shell script that you can find here
You will also find a small article explaining how to configure the project on jenkins.
Please let me know if worked for you. Thnaks
The nohup and the at now + 1 minutes didn't work for me.
Since Jenkins was killing the process spun in the background, I ensured the process to not be killed by setting a fake BUILD_ID to that Jenkins task. This is what the Jenkins Execute shell task looks like:
BUILD_ID=do_not_kill_me
java -jar -Dserver.port=8053 /root/Deployments/my_application.war &
exit
As discussed here.
I assume you have a Jenkins-user on the server and this user is the owner of the Jenkins-service:
log in on the server as root.
run sudo visudo
add "jenkins ALL=(ALL) NOPASSWD:ALL" at the end (jenkins=your Jenkins-user)
Sign In in Jenkins and choose your jobs and click to configure
Choose "Execute Shell" in the "Post build step"
Copy and paste this:
service=myapp
if ps ax | grep -v grep | grep -v $0 | grep $service > /dev/null
then
sudo service myapp stop
sudo unlink /etc/init.d/myapp
sudo chmod +x /path/to/your/myapp.jar
sudo ln -s /path/to/your/myapp.jar /etc/init.d/myapp
sudo service myapp start
else
sudo chmod +x /path/to/your/myapp.jar
sudo ln -s /path/to/your/myapp.jar /etc/init.d/myapp
sudo service myapp start
fi
Save and run your job, the service should start automatically.
This worked for me on jenkins on a linux machine
kill -9 $(lsof -t -i:8080) || echo "Process was not running."
mvn clean compile
echo "mvn spring-boot:run" | at now + 1 minutes
In case no process on 8080 it will print the message and will continue.
Make sure that at is installed on your linux machine. You can use
sudo apt-get install at
to install at

Running Glassfish asadmin Commands via Jenkins

I'm trying to create an automated deployment via Jenkins that deploys an application to Glassfish-2.1.
The problem comes when Jenkins is running the following commands:
sh /usr/local/glassfish/bin/asadmin start-domain --user admin --passwordfile /usr/local/glassfish/passwordfile.txt domain1
sh /usr/local/glassfish/bin/asadmin deploy -s --contextroot admin /tmp/artifacts/$admin_war_file_name
But Jenkins just hangs, apparently waiting for the password. However, I can run this script manaually on the server and it works.
Also, I was curious if this ok from a security standpoint? Should I actually be storing the master password (if in fact I can even get Jenkins to run these commands in the first place!)
Any help is greatly appreciated.

Resources