티스토리 뷰

IT

[Jenkins/shell] 빌드된 jar 실행하기.sh

주인장 진빼이

Jenkins에서 프로젝트 빌드를 진행한 후 스프링 부트 서버를 실행해야한다.

이때 빌드에 성공하면 " /var/lib/jenkins/workspace/<Project>/build/libs/ " 에 SNAPSHOT  jar 파일이 생성되는데

jar 파일을 실행하려면 java -jar 명령어를 사용한다.

 

 

아래는 Jenkins 플러그인 post build task에서 빌드된 jar를 실행하기 위한 shell script이다.

#!/bin/bash

JAR_NAME=<JAR 파일명>

if [ -z "`ps -eaf | grep $JAR_NAME | grep -v grep`" ]; then
  echo "not found $JAR_NAME"
else

  ps -eaf | grep $JAR_NAME | grep -v grep | awk '{print $2}' |
  while read PID
  do
    echo "Killing $PID ..."
    kill -9 $PID
    echo "$PID is shutdown!"
  done
fi

nohup java -jar "/var/lib/jenkins/workspace/<프로젝트명>/build/libs/$JAR_NAME" &

 

실 사용 젠킨스 로그

04:01:46 BUILD SUCCESSFUL in 1m 6s
04:01:46 8 actionable tasks: 8 up-to-date
04:01:47 Build step 'Invoke Gradle script' changed build result to SUCCESS
04:01:48 Performing Post build task...
04:01:48 Match found for :SUCCESSFUL : True
04:01:48 Logical operation result is TRUE
04:01:48 Running script  : JAR_NAME=mylog-0.0.1-SNAPSHOT.jar
04:01:48 
04:01:48 if [ -z "`ps -eaf | grep $JAR_NAME | grep -v grep`" ]; then
04:01:48 
04:01:48   echo "not found $JAR_NAME"
04:01:48 
04:01:48 else
04:01:48 
04:01:48   ps -eaf | grep $JAR_NAME | grep -v grep | awk '{print $2}' |
04:01:48   while read PID
04:01:48   do
04:01:48 
04:01:48     echo "Killing $PID ..."
04:01:48 
04:01:48     kill -9 $PID
04:01:48     echo "$PID is shutdown!"
04:01:48   done
04:01:48 fi
04:01:48 
04:01:48 nohup java -jar "/var/lib/jenkins/workspace/mylog/build/libs/$JAR_NAME" &
04:01:48 
04:01:48 [mylog] $ /bin/sh -xe /tmp/jenkins211964374787323196.sh
04:01:48 + JAR_NAME=mylog-0.0.1-SNAPSHOT.jar
04:01:48 + ps -eaf
04:01:48 + grep -v grep
04:01:48 + grep mylog-0.0.1-SNAPSHOT.jar
04:01:48 + [ -z jenkins   166629       1 32 18:56 ?        00:01:36 java -jar /var/lib/jenkins/workspace/mylog/build/libs/mylog-0.0.1-SNAPSHOT.jar ]
04:01:48 + ps -eaf
04:01:48 + grep -v grep
04:01:48 + grep mylog-0.0.1-SNAPSHOT.jar
04:01:48 + awk {print $2}
04:01:48 + read PID
04:01:48 + echo Killing 166629 ...
04:01:48 Killing 166629 ...
04:01:48 + kill -9 166629
04:01:48 + echo 166629 is shutdown!
04:01:48 166629 is shutdown!
04:01:48 + read PID
04:01:48 + nohup java -jar /var/lib/jenkins/workspace/mylog/build/libs/mylog-0.0.1-SNAPSHOT.jar
04:01:55 
04:01:56 POST BUILD TASK : SUCCESS
04:01:56 END OF POST BUILD TASK : 0
04:01:56 Finished: SUCCESS

빌드를 2번 이상 진행할 시, 기존 PID는 삭제됨이 확인되었다. (16629 -> 166910)

 

댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함