Jenkins定时构建发布,手动回滚指定版本,导出和导入Jenkins 配置xml模板及一些常见细节
- 我将自己的datad项目导出XML配置模板
java -jar jenkins-cli.jar -s http:// ip :port/ get-job datad --username xxx --password xxx > datad.xml
XML模板如下
<?xml version='1.1' encoding='UTF-8'?>
<project>
<actions/>
<description>xxx项目</description>
<keepDependencies>false</keepDependencies>
<properties>
<jenkins.model.BuildDiscarderProperty>
<strategy class="hudson.tasks.LogRotator">
<daysToKeep>-1</daysToKeep>
<numToKeep>5</numToKeep>
<artifactDaysToKeep>-1</artifactDaysToKeep>
<artifactNumToKeep>-1</artifactNumToKeep>
</strategy>
</jenkins.model.BuildDiscarderProperty>
<com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin="gitlab-plugin@1.5.12">
<gitLabConnection></gitLabConnection>
</com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty>
<hudson.model.ParametersDefinitionProperty>
<parameterDefinitions>
<com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition plugin="extended-choice-parameter@0.78">
<name>DEPLOY_DEV</name>
<description>构建变量</description>
<quoteValue>false</quoteValue>
<saveJSONParameterToFile>false</saveJSONParameterToFile>
<visibleItemCount>2</visibleItemCount>
<type>PT_SINGLE_SELECT</type>
<value>deploy,rollback</value>
<defaultValue>deploy</defaultValue>
<multiSelectDelimiter>,</multiSelectDelimiter>
<descriptionPropertyValue>deploy:正式发布,rollback:回滚到version指定版本</descriptionPropertyValue>
<projectName>datad</projectName>
</com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
<hudson.model.StringParameterDefinition>
<name>version</name>
<description>deploy时请忽略此参数,rollback时指定回滚版本</description>
<defaultValue>0</defaultValue>
<trim>false</trim>
</hudson.model.StringParameterDefinition>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>
</properties>
<scm class="hudson.plugins.git.GitSCM" plugin="git@3.10.0">
<configVersion>2</configVersion>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<url>http://xxx.datad.git</url>
<credentialsId>xxxx</credentialsId>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>*/dev</name>
</hudson.plugins.git.BranchSpec>
</branches>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<submoduleCfg class="list"/>
<extensions/>
</scm>
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers>
<hudson.triggers.TimerTrigger>
<spec>0 21 * * *</spec>
</hudson.triggers.TimerTrigger>
</triggers>
<concurrentBuild>true</concurrentBuild>
<builders>
<hudson.tasks.Shell>
<command>
#!/bin/bash
BUILD_ID=dontKillMe
env=$DEPLOY_DEV
ver=$version
workspaces=/workspace/${JOB_NAME}/
chmod -R 777 ${JENKINS_HOME}/workspace/datad*
/data/jenkins/script/./killAll.sh 8080@
${JENKINS_HOME}$workspaces./mvnw clean
#cp -f ${JENKINS_HOME}$workspacessrc/main/webapp/package.json ${JENKINS_HOME}$workspaces
case $env in
deploy)
echo "deploy $env"
sed -ig "s#servername=.*#servername=http://172.20.56.106:8080#g" ${JENKINS_HOME}$workspaces"src/main/resources/sdk.properties"
cd ${JENKINS_HOME}$workspaces
./mvnw -DskipTests -P dev package
;;
rollback)
echo "rollback $env version=$ver"
cp -R ${JENKINS_HOME}/jobs/${JOB_NAME}/builds/$ver/archive/target ${JENKINS_HOME}$workspaces"target"
;;
*)
;;
esac
</command>
</hudson.tasks.Shell>
<hudson.tasks.Shell>
<command>#!/bin/bash
BUILD_ID=dontKillMe
java -jar /root/.jenkins/workspace/datad/target/datad-0.0.1-SNAPSHOT.jar --httpPort=8080 > /var/log/datad/datad.out 2>&1 &
</command>
</hudson.tasks.Shell>
</builders>
<publishers>
<hudson.tasks.ArtifactArchiver>
<artifacts>target/datad-0.0.1-SNAPSHOT.jar</artifacts>
<allowEmptyArchive>false</allowEmptyArchive>
<onlyIfSuccessful>false</onlyIfSuccessful>
<fingerprint>false</fingerprint>
<defaultExcludes>true</defaultExcludes>
<caseSensitive>true</caseSensitive>
</hudson.tasks.ArtifactArchiver>
</publishers>
<buildWrappers/>
- 大家只需要导入上述模板即可
java -jar jenkins-cli.jar -s http:// ip :port/ create-job datad-dev --username xxx --password xxx < datad.xml
- 这里有一些细节我也一并说了: 在执行java -jar jenkins-cli.jar之前务必要打开匿名的角色,否则提示某用户错误:ERROR: xxx is missing the Job/ExtendedRead permission
ERROR: anonymous is missing the Job/ExtendedRead permission
- Jenkins 定时构建与发布及手动回滚到指定版本,可以在导入的模板中查看,具体问题具体分析
- 我这里定时为每天晚上9点,这样开发的小伙伴提交代码,自动构建完成之后无论失败还是成功都可以及时处理不影响一天的前后端开发进度
- 构建与发布细节如下,脚本下面可复制参考: 1. ==Post-build Actions这块是个细节,如果需要回滚版本的话,务必要指定构建成功后jar或war的归档位置,以便回滚成功。==
- ==可参考的Shell脚本== 1. 构建或回滚脚本:指定系统参数,杀掉应用进程,执行系统maven构建命令
#!/bin/bash
BUILD_ID=dontKillMe
env=$DEPLOY_DEV
ver=$version
workspaces=/workspace/${JOB_NAME}/
chmod -R 777 ${JENKINS_HOME}/workspace/datad*
/data/jenkins/script/./killAll.sh 8080@
${JENKINS_HOME}$workspaces./mvnw clean
#cp -f ${JENKINS_HOME}$workspacessrc/main/webapp/package.json ${JENKINS_HOME}$workspaces
case $env in
deploy)
echo "deploy $env"
sed -ig "s#servername=.*#servername=http://172.20.56.106:8080#g" ${JENKINS_HOME}$workspaces"src/main/resources/sdk.properties"
cd ${JENKINS_HOME}$workspaces
./mvnw -DskipTests -P dev package
;;
rollback)
echo "rollback $env version=$ver"
cp -R ${JENKINS_HOME}/jobs/${JOB_NAME}/builds/$ver/archive/target ${JENKINS_HOME}$workspaces"target"
;;
*)
;;
esac
- 发布脚本
#!/bin/bash
BUILD_ID=dontKillMe
java -jar /root/.jenkins/workspace/${JOB_NAME}/target/datad-0.0.1-SNAPSHOT.jar --httpPort=8080 > /var/log/${JOB_NAME}/datad.out 2>&1 &
- 杀进程脚本
#!/bin/bash
GREP=$1
i=1
while((1==1))
do
victim=`echo $GREP|cut -d "@" -f$i`
if [ "$victim" != "" ];then
((i++))
result=$(ps aux | grep $victim | grep -v grep)
result2=$(netstat -nltp|grep $victim)
if [[ $result != "" ]] && [[ $result2 != "" ]];then
echo ">>>>>>>>>>>>>>>>>>>>>>>>开始杀掉$victim相关进程<<<<<<<<<<<<<<<<<<<<<<<<"
ps aux | grep $victim | grep -v grep | awk '{print $2}'| xargs kill -9
$(ps aux | grep $victim)
else
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>$victim无进程<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
continue
fi
else
break
fi
done