–>Wed Mar 22 2017
–>Jenkins_2.32.3
–>Jenkins plugins: Maven Integration plugin, Publish Over SSH, Deploy to Container Plugin
(These plugins you have to install manually!)
–>Tomcat9
–>Ubuntu_16.04 Server
Jenkins is a simple tool to deploy WARs automatically.
With several plugins and a few simple configurations, Jenkins can deploy projects on a local pc or publish local projects to remote sites.
You can see installation steps in other blogs. For 2.32.3, some options during the confiurations may lead to failures and here just take a note for how to configure these options.
(1) Configure System
Jenkins Location
Jenkins URL, http://localhost:8080/jenkins or http://www.abc.abc/jenkins if the server had mapped to the domain.
Email Notification
User Name, your email account without suffix.
Use SSL, yes or it goes failure when deploying.
(Attention: your email should allow POP3 or IMAP.)
Publish Over SSH
Remote Directory –> When Jenkins publish over SSH, it will upload WARs to [Remote Directory]. It must be set here or Jenkins could not find the directory on server. Examples like “/home/usr/jenkins_upload”.
(Attention:You can add more than one server at here, then you can choose them at the project configuration.)
(2)Global Tool Configuration
Maven Configuration
File path should be of the settings.xml currently in use.
JDK
JAVA_HOME, the path of java_home.
(3)Project Configure
Build Triggers
Build whenever a SNAPSHOT dependency is built, YES.
Build periodically,
Schedule –>
15 11 * * * –> [minute] [hour] [day of month] [month] [day of week]
H 11 * * * –>H means a random range
Poll SCM
Schedule –>
*/2 * * * * –> Detect the gitlab every 2 minutes, if the SNAPSHOT have changed, Jenkins will re-publish the project.
Build
Post-build Actions
(1) Deploy war/ear to a container
Jenkins is installed on PC-A(local), and now you are going to deploy a project to Tomcat on PC-A(local).
WAR/EAR files–>figure out the war or ear files to deploy.
“**/*.war”, “**” means all directories in current jenkins project directory, “*.war” means all war(s) in each directory.
Context path–>The context path of the project on remote tomcat server. Jenkins need this path to visit the target project by web, so that jenkins will know if the project deploying finished.
Send build artifacts over SSH
Jenkins is installed on PC-A(local), and now you are going to use Jenkins to publish a project to Tomcat on a remote server PC-B(remote).
(Attention: You can add more than one remote servers here, PC-C, PC-D, PC-E…)
Source files–>The war files that you want to upload.
Remove prefix–>remove the listed prefix of Source files.
Remote directory–>Rember the Remote directory of Publish Over SSH? Here means the subdirctory of Publish Over SSH’s Remote directory. Examples like “/project_name”.
Exec command –> A shell command or file.
Here I’ve created a shell file–>
(Attention: You must point out the java home in shell, because Jenkins doesn’t know the java home at remote server.)
#!/bin/bash
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_121
while getopts "r:t:p:" arg
do
case $arg in
r)
remote_dir=${OPTARG}
;;
t)
tomcat_home=${OPTARG}
;;
p)
project_name=${OPTARG}
;;
?)
exit 1
;;
esac
done
echo "${remote_dir} ${tomcat_home} ${project_name}"
tomcat_pid=`/usr/bin/lsof -n -P -t -i :8080`
if [ -n "${tomcat_pid}" ];then
echo "if"
kill -9 ${tomcat_pid}
sleep 1
fi
rm -rf ${tomcat_dir}/webapps/${project_name}*
cp ${remote_dir}/${project_name}/*.war ${tomcat_home}/webapps/
${tomcat_home}/bin/startup.sh