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

operation

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 startup.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/
BUILD_ID=DONTKILLME
sh startup.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
CMD=$1
DIRNAME=$0
if [ "${DIRNAME:0:1}" = "/" ];then
  CURDIR=`dirname $DIRNAME`
else
  CURDIR="`pwd`"/"`dirname $DIRNAME`"
fi
 echo "----------Start running"
jarpath=test
jarport=8061
#source /etc/profile
#source ~/.bash_profile
  echo "----------Refresh variables"
case $CMD in
-start)
        echo "----------Startup service"
        nohup java  -jar /root/insurance/geosign/geosign.jar > nohup.out  2>&1 &
        ;;
-stop)
         echo "----------Stop running"
        pid=$(netstat -tnlp | grep -w ':'$jarport |awk '{print $nf}' | sed -r 's#.* (.*)/.*#\1#')
        if [  -n  "$pid"  ];  then
        kill $pid;
        fi
        ;;
-restart)
        pid=$(netstat -tnlp | grep -w ':'$jarport |awk '{print $nf}' | sed -r 's#.* (.*)/.*#\1#')
        if [  -n  "$pid"  ];  then
         kill $pid;
        fi
        nohup java  -jar /root/insurance/geosign/geosign.jar > nohup.out  2>&1 &
        ;;
esac

 

Keywords: jenkins Maven shell Java

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