Jenkins and GitLab Integrated Automated Testing and Deployment Detailed Tutorial-Building Maven Project

When Jenkins and the services that need to be published are on the same server, you want to copy the Jar package to the specified folder and publish it. Configuration and Jenkins and GitLab Integrated Automated Testing and Deployment Detailed Tutorial - Building Maven Project (1) It's slightly different.

  1. Stop the original service
  2. Replace the original jar package by copying the package result of Jenkins into the specified directory
  3. Run the new jar package
    On the basis of the previous Jenkins Maven project, modify the project configuration and add pre- and post-construction operations


Add pre-build operations

Modify the project configuration in Jenkins, add Pre Steps midpoint and select execute shell.
Then add the following code

# Jump to the specified folder
cd /root/insurance/geosign/
# Execute the stop service script under the folder (need to be developed and placed under the folder in advance)
sh -stop

Add post-build operations

Add the middle point in Post Steps and select execute shell. Check Run only if build succeeds.
Enter the following code

# Wait for 1 minute
sleep 1m
# Copy the jar package to the specified directory
cp -Rf target/*.jar /root/insurance/geosign/
# Jump to the folder and execute the startup script
cd /root/insurance/geosign/
sh -start

It should be noted that the purpose of waiting for 1 minute is to avoid no error in the file.

If the configuration is correct, the code push will automatically run the jar package.

The code for starting and stopping the service is as follows, which can be referenced by other projects.

#!/bin/bash -ilex
if [ "${DIRNAME:0:1}" = "/" ];then
  CURDIR=`dirname $DIRNAME`
  CURDIR="`pwd`"/"`dirname $DIRNAME`"
 echo "----------Start running"
#source /etc/profile
#source ~/.bash_profile
  echo "----------Refresh variables"
case $CMD in
        echo "----------Startup service"
        nohup java  -jar /root/insurance/geosign/geosign.jar > nohup.out  2>&1 &
         echo "----------Stop running"
        pid=$(netstat -tnlp | grep -w ':'$jarport |awk '{print $nf}' | sed -r 's#.* (.*)/.*#\1#')
        if [  -n  "$pid"  ];  then
        kill $pid;
        pid=$(netstat -tnlp | grep -w ':'$jarport |awk '{print $nf}' | sed -r 's#.* (.*)/.*#\1#')
        if [  -n  "$pid"  ];  then
         kill $pid;
        nohup java  -jar /root/insurance/geosign/geosign.jar > nohup.out  2>&1 &


Keywords: jenkins Maven shell Java

Added by vtroubled on Thu, 03 Oct 2019 20:26:21 +0300