git 导出指定版本_Jenkins定时构建发布+手动回滚指定版本+导出和导入配置xml模板及一些常见细节+shell脚本...

本文介绍了如何使用Jenkins进行定时构建发布,手动回滚到指定版本,以及导出和导入配置XML模板的详细步骤。文中提到在执行jenkins-cli.jar前需开启匿名权限,分享了定时构建设置为每天晚上9点以确保及时处理构建结果。同时,文章提供了构建与发布、回滚的Shell脚本示例,强调了归档位置的重要性以备回滚。
摘要由CSDN通过智能技术生成

5114956d209a2c39a9dbd94ac863bcd9.png

 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  &gt; /var/log/datad/datad.out 2&gt;&amp;1 &amp;
</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

9672258a1343cca4018f322e460c06fb.png
  • 这里有一些细节我也一并说了: 在执行java -jar jenkins-cli.jar之前务必要打开匿名的角色,否则提示某用户错误:ERROR: xxx is missing the Job/ExtendedRead permission
ERROR: anonymous is missing the Job/ExtendedRead permission

8eff12aa3bc5fe7562f1c9cc373431ce.png

  • Jenkins 定时构建与发布及手动回滚到指定版本,可以在导入的模板中查看,具体问题具体分析

4c48883ba2928e197b648d1adb676914.png
  • 我这里定时为每天晚上9点,这样开发的小伙伴提交代码,自动构建完成之后无论失败还是成功都可以及时处理不影响一天的前后端开发进度

a9807a0bfcd5c613b7dbf2a983818223.png

- 构建与发布细节如下,脚本下面可复制参考: 1. ==Post-build Actions这块是个细节,如果需要回滚版本的话,务必要指定构建成功后jar或war的归档位置,以便回滚成功。==

a41795a43710268c0a5ba0394933c0eb.png

- ==可参考的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
  1. 发布脚本
#!/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 &
  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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值