2017-08-27 181 views
0

在jenkins maven項目中,我們可以使用BUILD_ID=DontKillMe來阻止hudson腳本關閉shell調用。如何在jenkins管道中使用Jenkinsfile部署Java war文件

像:BUILD_ID=DontKillMe java -jar target.jar

但添加BUILD_IDJenkinsfile工作。


Jenkinsfile:

哈德森腳本執行
#!/usr/bin/env groovy 

node { 
    stage('Build') { 
     checkout scm 
     sh '/opt/gradle/gradle-4.1/bin/gradle clean build' 
    } 
    stage('Deploy') { 
     sh 'mkdir -p /opt/www/foobar' 
     sh 'cp build/libs/*.war /opt/www/foobar/newest.war' 
     sh 'chmod 755 ./deploy.sh' 
     sh 'nohup ./deploy.sh &' 
     sh 'while ! httping -qc1 http://localhost:10000 ; do sleep 1 ; done' 
    } 
} 

後,全部由哈德森腳本調用炮彈將被關閉。 即使是雙重nohup仍然無法正常工作。

deploy.sh:

#!/bin/bash 

nohup java -jar -Dspring.profiles.active=prod /opt/www/foobar/newest.war /var/log/foobar.log 2>&1 & 

回答

0

您可以在sh步驟中使用BUILD_ID=dontKillMe以同樣的方式

sh 'BUILD_ID=dontKillMe nohup ./deploy.sh &' 
+0

它在管道的'Jenkinsfile'構建腳本中沒用。 –

+1

@SuCheung它應該工作。但它似乎有一個錯誤,當它在某些操作系統上不起作用時https://issues.jenkins-ci.org/browse/JENKINS-46481 –

+0

瞭解,但我總是覺得'BUILD_ID = dontKillMe'很髒。 –

0

我發現另一種方式來執行腳本,不能被殺死。

  • Linux的服務

/etc/init.d/www-project

#!/bin/bash 

. /etc/init.d/functions 

SERVICE_NAME="www-project" 
RETVAL=0 
PID=-1 
PIDFILE=/var/run/${SERVICE_NAME}.PID 

start() { 
    if [ -f ${PIDFILE} ]; then 
    echo "PID file ${PIDFILE} already exists, please stop the service !" 
    exit 
    fi 
    echo "Starting service ${SERVICE_NAME} ..." 
    java -jar -Dspring.profiles.active=prod /opt/www/project/newest.war > /var/log/www-project.log 2>&1 & 
    PID = $! 
    if [ -z ${PID} ]; then 
    echo "Failed to get the process id, exit!" 
    exit 
    else 
    echo "Starting successfully, whose pid is ${PID}" 
    fi 
    touch $PIDFILE 
    echo ${PID} > ${PIDFILE} 
} 

stop() { 
    if [ -f $PIDFILE ]; then 
    PID = `cat ${PIDFILE}` 
    if [ -z $PID ]; then 
     echo "PIDFILE $PIDFILE is empty!" 
     exit 
    fi 
    if [ -z "`ps axf | grep $PID | grep -v grep`" ]; then 
     echo "Process dead but pidfile exists!" 
     exit 
    else 
     kill -9 $PID 
     echo "Stopping service successfully, whose pid is $PID" 
     rm -f $PIDFILE 
    fi 
    else 
    echo "File $PIDFILE does NOT exist!" 
    fi 
} 

restart() { 
    stop 
    start 
} 

status() { 
    if [ -f $PIDFILE ]; then 
    PID=`cat $PIDFILE` 
    if [ -z $PID ] ; then 
     echo "No effective pid but pidfile exists!" 
    else 
     if [ -z "`ps axf | grep $PID | grep -v grep`" ]; then 
     echo "Process dead but pidfile exist" 
     else 
     echo "Running" 
     fi 
    fi 
    else 
    echo "Service not running" 
    fi 
} 

case "$1" in 
    start) 
    start 
    ;; 
    stop) 
    stop 
    ;; 
    restart) 
    restart 
    ;; 
    status) 
    status 
    ;; 
    *) 
    echo "Usage: www-project {start|stop|restart|status}" 
    ;; 
esac 
  • systemd單元服務

/usr/lib中/ systemd /系統/www-project.service

[Unit] 
Description=project 
After=mysqld.service 
Wants=mysqld.service 

[Service] 
ExecStart=/usr/lib/jvm/java/bin/java -jar -Dspring.profiles.active=prod /opt/www/project/newest.war > /var/log/www-project.log 
PIDFile=/var/run/www-project.pid 
ExecReload=/bin/kill -HUP $MAINPID 
ExecStop=/bin/kill -9 $MAINPID 

[Install] 
WantedBy=multi-user.target 

在Jenkinsfile,我們可以調用

service restart www-project 

systemctl restart www-project 

和Hudson腳本絕不會殺死服務。