jenkins 自动下载文件并启动 适用于spring 微服务打包成jar

#!/bin/bash
#sleep 20
#wait
lock=’lock’

if [ -f $lock];then
rm -f $lock
fi

var=`date +%Y%m%d%H%M%S`
echo $var >> $lock

content=””
while [ $i -lt 1 ] do
content=$(curl -u name:password http://jenkins.xxxx.com/jenkins/job/fast_loan/lastSuccessfulBuild/api/json?depth=1)
echo $content >> $lock
result=$(echo $content | grep ‘_class’)
echo $result
if [ -n “$result” ];then
echo $result
let “i++”
echo $i
else
let “j++”
echo “Download Try Times: $j” >> $lock
sleep 1
fi

done

version=$(jq -r ‘.mavenArtifacts.moduleRecords[0].mainArtifact.version’ <<<“$content”)
echo $version
canonicalName=$(jq -r ‘.mavenArtifacts.moduleRecords[0].mainArtifact.canonicalName’ <<<“$content”)
echo $canonicalName
url=’http://jenkins.xxxx.com/jenkins/job/fast_loan/lastSuccessfulBuild/com.duwo$fast_loan/artifact/com.duwo/fast_loan/’${version}/${canonicalName}
echo $url
if [ ! -f $canonicalName ] ; then
echo “Target file not exist: $canonicalName , no need to delete.” >> $lock
else
echo “Delete current file: $canonicalName , download new file.” >> $lock
rm -f $canonicalName
fi

wget -q -c $url

export JAVA_HOME=/usr/local/jdk1.8
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

/usr/bin/nohup nohup java -jar /root/$canonicalName -Xms64m -Xmx512m -XX:MaxMetaspaceSize=512m -XX:CompressedClassSpaceSize=64m -Xss256k -Xmn8m -XX:InitialCodeCacheSize=4m -XX:ReservedCodeCacheSize=8m -XX:MaxDirectMemorySize=16m 2 >> $lock &