当前位置:   article > 正文

jenkins & gitlab 实现自动拉取代码 打包 发版_jenkins获取git指定标签打包

jenkins获取git指定标签打包

1.首先安装jenkins。

1.1编写Dockfile文件。

cat  Dockfile

FROM jenkins/jenkins:2.414.3-jdk17
USER root
RUN apt-get update && apt-get install -y lsb-release
ADD sources.list /etc/apt/
RUN apt-get update && apt-get install -y software-properties-common
RUN curl  -fsSL https://mirrors.huaweicloud.com/docker-ce/linux/debian/gpg | apt-key add -
RUN add-apt-repository "deb [arch=amd64] https://mirrors.huaweicloud.com/docker-ce/linux/debian $(lsb_release -cs) stable"
RUN apt-get update  && apt-get install -y docker-ce-cli
USER jenkins
RUN jenkins-plugin-cli --plugins "blueocean docker-workflow"

 1.2编写启动脚本

cat run.sh

docker run --name jenkins-blueocean -u root  --restart=on-failure -itd --network jenkins --env DOCKER_HOST=tcp://docker:2376 --env DOCKER_CERT_PATH=/certs/client --env DOCKER_TLS_VERIFY=1 --publish 39412:8080 --publish 51206:50000 --volume /opt/jenkins/jenkins-data:/var/jenkins_home --volume /opt/jenkins/jenkins-docker-certs:/certs/client:ro myjenkins-blueocean:2.414.3-1

1.3编写sources.list文件

cat sources.list

deb https://mirrors.aliyun.com/debian/ bookworm main non-free non-free-firmware contrib
deb-src https://mirrors.aliyun.com/debian/ bookworm main non-free non-free-firmware contrib
deb https://mirrors.aliyun.com/debian-security/ bookworm-security main
deb-src https://mirrors.aliyun.com/debian-security/ bookworm-security main
deb https://mirrors.aliyun.com/debian/ bookworm-updates main non-free non-free-firmware contrib
deb-src https://mirrors.aliyun.com/debian/ bookworm-updates main non-free non-free-firmware contrib
deb https://mirrors.aliyun.com/debian/ bookworm-backports main non-free non-free-firmware contrib
deb-src https://mirrors.aliyun.com/debian/ bookworm-backports main non-free non-free-firmware contrib 

2.首先安装docker-ce。

2.1通过阿里云镜像站安装,文档附上,根据系统版本进行安装。

docker-ce镜像_docker-ce下载地址_docker-ce安装教程-阿里巴巴开源镜像站

2.2 使用docker拉取jenkins镜像。

docker pull jenkins/jenkins:2.414.3-jdk17   # 拉取镜像

docker images                                            # 查看镜像

docker network create jenkins                    # 安装jenkins所需网络服务

docker build -t myjenkins-blueocean:2.414.3-1  .         #使用刚才的Dockerfile文件构建镜像

2.3启动构建的Jenkins镜像。

./run.sh       #运行刚刚的脚本

docker ps   #查看容器运行情况

netstat -nutlp   #查看端口

 2.4查看Jkenkins密码登录。

cat /opt/jenkins/jenkins-data/secrets/initialAdminPassword
 

账号为admin

IP+39412

2.5登录上jenkins后,修改密码并且安装必要的插件。

系统管理-插件管理-updates,搜索安装以下插件。

ssh server

GitLab

GitLab Authentication

Generic Webhook Trigger

2.6配置跨域,不然后续构建时会报错403相关无效的碎屑 。

报错如下

WARNING hudson.security.csrf.CrumbFilter#doFilter: Found invalid crumb c0e527c9839db9e6e01a72ced74467cadec80c034df05525bd89ec032883f411. If you are calling this URL with a script, please use the API Token instead. More information: https://www.jenkins.io/redirect/crumb-cannot-be-used-for-script

WARNING hudson.security.csrf.CrumbFilter#doFilter: No valid crumb was included in request for /$stapler/bound/2c81919f-4aa1-4390-b361-c0ab2c945e46/render by admin. Returning 403.

具体解决方法如下:

系统管理-全局安全配置-跨站请求伪造保护-启动代理兼容(此项勾选上即可)

2.7 以上jenkins 安装以及插件安装都已安装完成,后续安装完gitlab,会配置构建脚本参数等。 


3.安装gitlab。

wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-16.4.1-ce.0.el7.x86_64.rpm

yum -y install local gitlab-ce-16.4.1-ce.0.el7.x86_64.rpm

3.1修改配置文件设置访问端口。

vi /etc/gitlab/gitlab.rb  

  1. external_url 'http://ip地址:9091'
  2. # 公网内网都可以,后面是端口

 3.2初始化安装并启动。

gitlab-ctl reconfigure  #初始化

# 如果需要修改配置,修改完后需要重新初始化,否则无法生效。

gitlab-ctl start

gitlab-ctl status

3.3通过浏览器访问地址。

 http://ip地址:9091'

登录账号为root

登录密码在

cat /etc/gitlab/initial_root_password

jenkins 我这边选择的为推荐安装,后续可按特定需求安装特定插件。

3.4设置gitlab,修改登录密码,配置jenkins免密登录的key,实现免密登录。

 

3.5 在jenkins容器服务器内,生成ssh免密文件,获取Key并添加到gitlab上。

  1. docker ps
  2. #查看容器运行情况
  3. docker exec -it 容器id /bin/bash
  4. #进入容器并指定解释器
  5. ssh-keygen
  6. #在jenkins容器内运行此命令,生成密钥文件,一路回车即可。
  7. cd /root/.ssh/
  8. #会看到两个文件 id_rsa id_rsa.pub
  9. cat id_rsa.pub
  10. #将获取到的key上传到gitlab。
  11. ssh-copy-id root@ip
  12. ssh  #刚刚的IP试一下 是否可以远程登录
  13. #这里是将生成的密钥文件传到部署服务的服务器上
  14. #ip为部署服务的服务器,也可以给容器服务器传,也可以给别的服务器传,主要实现免密。
  15. #此步骤为Jenkins将编译好,打包好的代码服务,需要分发到部署服务器进行部署,此步骤是方便jenkins部署服务。
  16. #由于jenkins,需要通过连接gitlab实现拉取代码,所以需要生成sshkey,并填写到gitlab上,实现拉取代码,打包操作。后续jenkins需要将打包好的代码发布到服务器,所以需要ssh-copy-id到每个部署服务的服务器上。

 3.6创建新项目,并且发送给开发人员地址,他们即可提交代码到仓库,以下为图片教程。

 

 

# 将获取到的git地址,连同登录账号密码,一并发送给开发人员,方便他们后续提交代码。

3.8在jenkins容器内配置,环境变量,方便后续打包发布。

配置容器环境变量。

vim /etc/profiles

export MAVEN_HOME=/var/jenkins_home/mvn/apache-maven-3.8.8
export PATH=${PATH}:${MAVEN_HOME}/bin
export JAVA_HOME=/var/jenkins_home/jdk1.8.0_341
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:$CLASSPATH
export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin
export PATH=$PATH:${JAVA_PATH}

# 以上目录为容器启动时的映射目录,如果Dockerfile文件改了,这里也需要更改。 

# 另外需要自行下载准备maven和jdk 我这里版本号都有,可自行下载

# maven需要修改配置文件,文件详情如下:

 settings.xml

  1. <settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
  4. <localRepository>/var/jenkins_home/mvn/repo</localRepository>
  5. <mirror>
  6. <id>aliyunmaven</id>
  7. <mirrorOf>*</mirrorOf>
  8. <name>阿里云公共仓库</name>
  9. <url>https://maven.aliyun.com/repository/public</url>
  10. </mirror>
  11. </mirrors>

#退出容器,由于容器启动时定义了映射,所以需要在宿主机映射目录下,上传maven和jdk即可。

exit  #退出容器

# 以下操作在宿主机进行

cd /opt/jenkins/jenkins-data  #此目录为容器映射目录

mkdir mvn

mkdir mvn/repo

#repo 是因为maven会用到这个目录,正常创建即可。

#将maven 上传至 mvn ,将jdk上传至当前目录解压即可。

4.0 现在已经配置好环境变量,现在配置Jenkins的页面。

4.1通过jenkins,新建任务。

 

 

 记住这个GROUP 和 METHOD 这是一个变量调用,后面脚本的脚本里会说到。

上图这个脚本的位置为jenkins容器内的位置,放置和更改脚本可直接在宿主机对应的映射目录上传脚本即可。

4.2下面来我这边来放这个脚本,供大家参考。

  1. #!/bin/bash
  2. DATE=`date +%Y-%m-%d_%H:%M:%S`
  3. METHOD=$1
  4. GROUP=$2
  5. source /etc/profile
  6. MAVEN_HOME=/var/jenkins_home/mvn/apache-maven-3.8.8
  7. DOCKER_WOKER_HOME="/var/jenkins_home/deploy/sjyq" #脚本地址
  8. DOCKER_WOKER_JAR_HOME="/var/jenkins_home/deploy/sjyq/xxx" #项目地址
  9. function clone_code(){
  10. echo "开始clone代码..."
  11. cd $DOCKER_WOKER_HOME ; rm -rf xxx #项目名称
  12. git clone -b master git@xxx:root/xxx.git #填写git地址
  13. echo "clone 代码完成..."
  14. }
  15. function code_maven(){
  16. echo "开始编译代码..."
  17. if [[ $GROUP == *"kn-delayMq-event-push"* ]];then
  18. cd $DOCKER_WOKER_HOME/kn-commentandmonitor/kn-delayMq-event-push && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
  19. elif [[ $GROUP == *"kn-delete-information"* ]];then
  20. cd $DOCKER_WOKER_HOME/kn-commentandmonitor/kn-delete-information && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
  21. elif [[ $GROUP == *"kn-delete-monitor"* ]];then
  22. cd $DOCKER_WOKER_HOME/kn-commentandmonitor/kn-delete-monitor && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
  23. elif [[ $GROUP == *"kn-delete-publicsentiment"* ]];then
  24. cd $DOCKER_WOKER_HOME/kn-commentandmonitor/kn-delete-publicsentiment && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
  25. elif [[ $GROUP == *"kn-monitor-MqHandle"* ]];then
  26. cd $DOCKER_WOKER_HOME/kn-commentandmonitor/kn-monitor-MqHandle && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
  27. elif [[ $GROUP == *"kn-monitor-isexist"* ]];then
  28. cd $DOCKER_WOKER_HOME/kn-commentandmonitor/kn-monitor-isexist && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
  29. elif [[ $GROUP == *"kn-monitor-provide"* ]];then
  30. cd $DOCKER_WOKER_HOME/kn-commentandmonitor/kn-monitor-provide && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
  31. elif [[ $GROUP == *"kn-review-handle"* ]];then
  32. cd $DOCKER_WOKER_HOME/kn-commentandmonitor/kn-review-handle && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
  33. elif [[ $GROUP == *"monitor-scheme-task"* ]];then
  34. cd $DOCKER_WOKER_HOME/kn-commentandmonitor/monitor-scheme-task && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
  35. elif [[ $GROUP == *"redis-clustor-text"* ]];then
  36. cd $DOCKER_WOKER_HOME/kn-commentandmonitor/redis-clustor-text && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
  37. elif [[ $GROUP == *"wx-master"* ]];then
  38. cd $DOCKER_WOKER_HOME/kn-commentandmonitor/wx-master && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
  39. elif [[ $GROUP == *"monitor_auto_push"* ]];then
  40. cd $DOCKER_WOKER_HOME/kn-commentandmonitor/kn_monitor_auto_push && $MAVEN_HOME/bin/mvn clean package -Dmaven.test.skip=true
  41. fi
  42. echo "代码编译完成..."
  43. }
  44. usage() {
  45. echo "Usage: sh java_demo.sh [deploy_jar|rollback_last_version] [jar]"
  46. exit 1
  47. }
  48. function scp_jar_file() {
  49. echo "开始删除旧包..."
  50. if [[ $GROUP == *"kn-delayMq-event-push"* ]];then
  51. ssh root@ip地址1 "rm -rf /data/update/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar"
  52. elif [[ $GROUP == *"kn-delete-information"* ]];then
  53. ssh root@ip地址1 "rm -rf /data/update/kn-delete-information-1.0.jar"
  54. elif [[ $GROUP == *"kn-delete-monitor"* ]];then
  55. ssh root@ip地址1 "rm -rf /data/update/kn-delete-monitor-0.0.1-SNAPSHOT.jar"
  56. elif [[ $GROUP == *"kn-delete-publicsentiment"* ]];then
  57. ssh root@ip地址1 "rm -rf /data/update/kn-delete-publicsentiment-1.0.jar"
  58. elif [[ $GROUP == *"kn-monitor-MqHandle"* ]];then
  59. ssh root@ip地址1 "rm -rf /data/update/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar"
  60. elif [[ $GROUP == *"kn-monitor-isexist"* ]];then
  61. ssh root@ip地址1 "rm -rf /data/update/kn-monitor-isexist-0.0.1-SNAPSHOT.jar"
  62. elif [[ $GROUP == *"kn-monitor-provide"* ]];then
  63. ssh root@ip地址1 "rm -rf /data/update/kn-monitor-provide-0.0.1-SNAPSHOT.jar"
  64. elif [[ $GROUP == *"kn-review-handle"* ]];then
  65. ssh root@ip地址1 "rm -rf /data/update/kn-review-handle-0.0.1-SNAPSHOT.jar"
  66. elif [[ $GROUP == *"monitor-scheme-task"* ]];then
  67. ssh root@ip地址1 "rm -rf /data/update/monitor-scheme-task-0.0.1-SNAPSHOT.jar"
  68. elif [[ $GROUP == *"redis-clustor-text"* ]];then
  69. ssh root@ip地址1 "rm -rf /data/update/redis-clustor-text-0.0.1-SNAPSHOT.jar"
  70. elif [[ $GROUP == *"wx-master"* ]];then
  71. ssh root@ip地址1 "rm -rf /data/update/wxproject-1-SNAPSHOT.jar"
  72. fi
  73. echo "远程服务器旧版本jar包删除完成..."
  74. echo "开始拷贝文件到远程服务器..."
  75. if [[ $GROUP == *"kn-delayMq-event-push"* ]];then
  76. scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/kn-delayMq-event-push/target/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar ip地址1:/data/update/
  77. elif [[ $GROUP == *"kn-delete-information"* ]];then
  78. scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/kn-delete-information/target/kn-delete-information-1.0.jar ip地址1:/data/update/
  79. elif [[ $GROUP == *"kn-delete-monitor"* ]];then
  80. scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/kn-delete-monitor/target/kn-delete-monitor-0.0.1-SNAPSHOT.jar ip地址1:/data/update/
  81. elif [[ $GROUP == *"kn-delete-publicsentiment"* ]];then
  82. scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/kn-delete-publicsentiment/target/kn-delete-publicsentiment-1.0.jar ip地址1:/data/update/
  83. elif [[ $GROUP == *"kn-monitor-MqHandle"* ]];then
  84. scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/kn-monitor-MqHandle/target/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar ip地址1:/data/update/
  85. elif [[ $GROUP == *"kn-monitor-isexist"* ]];then
  86. scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/kn-monitor-isexist/target/kn-monitor-isexist-0.0.1-SNAPSHOT.jar ip地址1:/data/update/
  87. elif [[ $GROUP == *"kn-monitor-provide"* ]];then
  88. scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/kn-monitor-provide/target/kn-monitor-provide-0.0.1-SNAPSHOT.jar ip地址1:/data/update/
  89. elif [[ $GROUP == *"kn-review-handle"* ]];then
  90. scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/kn-review-handle/target/kn-review-handle-0.0.1-SNAPSHOT.jar ip地址1:/data/update/
  91. elif [[ $GROUP == *"monitor-scheme-task"* ]];then
  92. scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/monitor-scheme-task/target/monitor-scheme-task-0.0.1-SNAPSHOT.jar ip地址1:/data/update/
  93. elif [[ $GROUP == *"redis-clustor-text"* ]];then
  94. scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/redis-clustor-text/target/redis-clustor-text-0.0.1-SNAPSHOT.jar ip地址1:/data/update/
  95. elif [[ $GROUP == *"wx-master"* ]];then
  96. scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/wx-master/target/wxproject-1-SNAPSHOT.jar ip地址1:/data/update/
  97. elif [[ $GROUP == *"monitor_auto_push"* ]];then
  98. scp ${DOCKER_WOKER_HOME}/kn-commentandmonitor/kn_monitor_auto_push/target/kn_monitor_auto_push-1.0.jar ip地址2:/data/update/
  99. fi
  100. }
  101. function bak_jar() {
  102. echo "开始备份文件..."
  103. if [[ $GROUP == *"kn-delayMq-event-push"* ]];then
  104. ssh ip地址1 "mv /data/kn-delayMq-event-push/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar /data/kn-delayMq-event-push/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar-${DATE}"
  105. elif [[ $GROUP == *"kn-delete-information"* ]];then
  106. #ssh ip地址1 "mv ${DATE}"
  107. echo "404,server not found..."
  108. elif [[ $GROUP == *"kn-delete-monitor"* ]];then
  109. ssh ip地址1 "mv /data/delete-monitor/kn-delete-monitor-0.0.1-SNAPSHOT.jar /data/delete-monitor/kn-delete-monitor-0.0.1-SNAPSHOT.jar-${DATE}"
  110. elif [[ $GROUP == *"kn-delete-publicsentiment"* ]];then
  111. #ssh ip地址1 "mv ${DATE}"
  112. echo "404,server not found..."
  113. elif [[ $GROUP == *"kn-monitor-MqHandle"* ]];then
  114. ssh ip地址1 "mv /data/MqHandle/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar /data/MqHandle/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar-${DATE}"
  115. elif [[ $GROUP == *"kn-monitor-isexist"* ]];then
  116. #ssh ip地址1 "mv ${DATE}"
  117. echo "404,server not found..."
  118. elif [[ $GROUP == *"kn-monitor-provide"* ]];then
  119. ssh ip地址1 "mv /data/provide/kn-monitor-provide-0.0.1-SNAPSHOT.jar /data/provide/kn-monitor-provide-0.0.1-SNAPSHOT.jar-${DATE}"
  120. elif [[ $GROUP == *"kn-review-handle"* ]];then
  121. ssh ip地址1 "mv /data/review-handle/kn-review-handle-0.0.1-SNAPSHOT.jar /data/review-handle/kn-review-handle-0.0.1-SNAPSHOT.jar-${DATE}"
  122. elif [[ $GROUP == *"monitor-scheme-task"* ]];then
  123. #ssh ip地址1 "mv ${DATE}"
  124. echo "404,server not found..."
  125. elif [[ $GROUP == *"redis-clustor-text"* ]];then
  126. ssh ip地址1 "mv /data/redis-clustor-text/redis-clustor-text-0.0.1-SNAPSHOT.jar /data/redis-clustor-text/redis-clustor-text-0.0.1-SNAPSHOT.jar-${DATE}"
  127. elif [[ $GROUP == *"wx-master"* ]];then
  128. #ssh ip地址1 "mv ${DATE}"
  129. echo "404,server not found..."
  130. fi
  131. }
  132. function cp_jar_file() {
  133. echo "开始拷贝文件..."
  134. if [[ $GROUP == *"kn-delayMq-event-push"* ]];then
  135. ssh ip地址1 "cp /data/update/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar /data/kn-delayMq-event-push/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar"
  136. elif [[ $GROUP == *"kn-delete-information"* ]];then
  137. echo "404,server not found..."
  138. elif [[ $GROUP == *"kn-delete-monitor"* ]];then
  139. ssh ip地址1 "cp /data/update/kn-delete-monitor-0.0.1-SNAPSHOT.jar /data/delete-monitor/kn-delete-monitor-0.0.1-SNAPSHOT.jar"
  140. elif [[ $GROUP == *"kn-delete-publicsentiment"* ]];then
  141. echo "404,server not found..."
  142. elif [[ $GROUP == *"kn-monitor-MqHandle"* ]];then
  143. ssh ip地址1 "cp /data/update/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar /data/MqHandle/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar"
  144. elif [[ $GROUP == *"kn-monitor-isexist"* ]];then
  145. echo "404,server not found..."
  146. elif [[ $GROUP == *"kn-monitor-provide"* ]];then
  147. ssh ip地址1 "cp /data/update/kn-monitor-provide-0.0.1-SNAPSHOT.jar /data/provide/kn-monitor-provide-0.0.1-SNAPSHOT.jar"
  148. elif [[ $GROUP == *"kn-review-handle"* ]];then
  149. ssh ip地址1 "cp /data/update/kn-review-handle-0.0.1-SNAPSHOT.jar /data/review-handle/kn-review-handle-0.0.1-SNAPSHOT.jar"
  150. elif [[ $GROUP == *"monitor-scheme-task"* ]];then
  151. echo "404,server not found..."
  152. elif [[ $GROUP == *"redis-clustor-text"* ]];then
  153. ssh ip地址1 "cp /data/update/redis-clustor-text-0.0.1-SNAPSHOT.jar /data/redis-clustor-text/redis-clustor-text-0.0.1-SNAPSHOT.jar"
  154. elif [[ $GROUP == *"monitor_auto_push"* ]];then
  155. ssh ip地址2 "cp /data/update/kn_monitor_auto_push-1.0.jar /data/monitor_auto_push/kn_monitor_auto_push-1.0.jar"
  156. elif [[ $GROUP == *"wx-master"* ]];then
  157. echo "404,server not found..."
  158. fi
  159. echo "拷贝文件执行结束..."
  160. }
  161. function delete_history_jar() {
  162. echo "开始删除历史版本..."
  163. if [[ $GROUP == *"kn-delayMq-event-push"* ]];then
  164. NUM=$(ssh ip地址1 "ls -l -d -rt /data/kn-delayMq-event-push/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar* |wc -l")
  165. if [ ${NUM} -gt 3 ];then
  166. NAME=$(ssh ip地址1 "ls -l -d -rt /data/kn-delayMq-event-push/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar* |head -n1 |awk '{print \$9}'")
  167. ssh ip地址1 "rm -rf ${NAME}"
  168. echo "删除历史版本${NAME}成功..."
  169. fi
  170. elif [[ $GROUP == *"kn-delete-information"* ]];then
  171. echo "404,server not found..."
  172. elif [[ $GROUP == *"kn-delete-monitor"* ]];then
  173. NUM=$(ssh ip地址1 "ls -l -d -rt /data/delete-monitor/kn-delete-monitor-0.0.1-SNAPSHOT.jar* |wc -l")
  174. if [ ${NUM} -gt 3 ];then
  175. NAME=$(ssh ip地址1 "ls -l -d -rt /data/delete-monitor/kn-delete-monitor-0.0.1-SNAPSHOT.jar* |head -n1 |awk '{print \$9}'")
  176. ssh ip地址1 "rm -rf ${NAME}"
  177. echo "删除历史版本${NAME}成功..."
  178. fi
  179. elif [[ $GROUP == *"kn-delete-publicsentiment"* ]];then
  180. echo "404,server not found..."
  181. elif [[ $GROUP == *"kn-monitor-MqHandle"* ]];then
  182. NUM=$(ssh ip地址1 "ls -l -d -rt /data/MqHandle/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar* |wc -l")
  183. if [ ${NUM} -gt 3 ];then
  184. NAME=$(ssh ip地址1 "ls -l -d -rt /data/MqHandle/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar* |head -n1 |awk '{print \$9}'")
  185. ssh ip地址1 "rm -rf ${NAME}"
  186. echo "删除历史版本${NAME}成功..."
  187. fi
  188. elif [[ $GROUP == *"kn-monitor-isexist"* ]];then
  189. echo "404,server not found..."
  190. elif [[ $GROUP == *"kn-monitor-provide"* ]];then
  191. NUM=$(ssh ip地址1 "ls -l -d -rt /data/provide/kn-monitor-provide-0.0.1-SNAPSHOT.jar* |wc -l")
  192. if [ ${NUM} -gt 3 ];then
  193. NAME=$(ssh ip地址1 "ls -l -d -rt /data/provide/kn-monitor-provide-0.0.1-SNAPSHOT.jar* |head -n1 |awk '{print \$9}'")
  194. ssh ip地址1 "rm -rf ${NAME}"
  195. echo "删除历史版本${NAME}成功..."
  196. fi
  197. elif [[ $GROUP == *"kn-review-handle"* ]];then
  198. NUM=$(ssh ip地址1 "ls -l -d -rt /data/review-handle/kn-review-handle-0.0.1-SNAPSHOT.jar* |wc -l")
  199. if [ ${NUM} -gt 3 ];then
  200. NAME=$(ssh ip地址1 "ls -l -d -rt /data/review-handle/kn-review-handle-0.0.1-SNAPSHOT.jar* |head -n1 |awk '{print \$9}'")
  201. ssh ip地址1 "rm -rf ${NAME}"
  202. echo "删除历史版本${NAME}成功..."
  203. fi
  204. elif [[ $GROUP == *"monitor-scheme-task"* ]];then
  205. echo "404,server not found..."
  206. elif [[ $GROUP == *"redis-clustor-text"* ]];then
  207. NUM=$(ssh ip地址1 "ls -l -d -rt /data/redis-clustor-text/redis-clustor-text-0.0.1-SNAPSHOT.jar* |wc -l")
  208. if [ ${NUM} -gt 3 ];then
  209. NAME=$(ssh ip地址1 "ls -l -d -rt /data/redis-clustor-text/redis-clustor-text-0.0.1-SNAPSHOT.jar* |head -n1 |awk '{print \$9}'")
  210. ssh ip地址1 "rm -rf ${NAME}"
  211. echo "删除历史版本${NAME}成功..."
  212. fi
  213. elif [[ $GROUP == *"wx-master"* ]];then
  214. echo "404,server not found..."
  215. fi
  216. echo "删除历史版本结束..."
  217. }
  218. function rollback_last_version() {
  219. echo "开始回滚..."
  220. if [[ $GROUP == *"kn-delayMq-event-push"* ]];then
  221. ssh ip地址1 "rm -rf /data/kn-delayMq-event-push/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar;LAST_VERSION=\$(ls -l -t /data/kn-delayMq-event-push/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar* |awk '{print \$9}' |head -n1);mv \$LAST_VERSION /data/kn-delayMq-event-push/kn-delayMq-event-push-0.0.1-SNAPSHOT.jar"
  222. elif [[ $GROUP == *"kn-delete-information"* ]];then
  223. echo "404,server not found..."
  224. elif [[ $GROUP == *"kn-delete-monitor"* ]];then
  225. ssh ip地址1 "rm -rf /data/delete-monitor/kn-delete-monitor-0.0.1-SNAPSHOT.jar;LAST_VERSION=\$(ls -l -t /data/delete-monitor/kn-delete-monitor-0.0.1-SNAPSHOT.jar* |awk '{print \$9}' |head -n1);mv \$LAST_VERSION /data/delete-monitor/kn-delete-monitor-0.0.1-SNAPSHOT.jar"
  226. elif [[ $GROUP == *"kn-delete-publicsentiment"* ]];then
  227. echo "404,server not found..."
  228. elif [[ $GROUP == *"kn-monitor-MqHandle"* ]];then
  229. ssh ip地址1 "rm -rf /data/MqHandle/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar;LAST_VERSION=\$(ls -l -t /data/MqHandle/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar* |awk '{print \$9}' |head -n1);mv \$LAST_VERSION /data/MqHandle/kn-monitor-MqHandle-0.0.1-SNAPSHOT.jar"
  230. elif [[ $GROUP == *"kn-monitor-isexist"* ]];then
  231. echo "404,server not found..."
  232. elif [[ $GROUP == *"kn-monitor-provide"* ]];then
  233. ssh ip地址1 "rm -rf /data/provide/kn-monitor-provide-0.0.1-SNAPSHOT.jar;LAST_VERSION=\$(ls -l -t /data/provide/kn-monitor-provide-0.0.1-SNAPSHOT.jar* |awk '{print \$9}' |head -n1);mv \$LAST_VERSION /data/provide/kn-monitor-provide-0.0.1-SNAPSHOT.jar"
  234. elif [[ $GROUP == *"kn-review-handle"* ]];then
  235. ssh ip地址1 "rm -rf /data/review-handle/kn-review-handle-0.0.1-SNAPSHOT.jar;LAST_VERSION=\$(ls -l -t /data/review-handle/kn-review-handle-0.0.1-SNAPSHOT.jar* |awk '{print \$9}' |head -n1);mv \$LAST_VERSION /data/review-handle/kn-review-handle-0.0.1-SNAPSHOT.jar"
  236. elif [[ $GROUP == *"monitor-scheme-task"* ]];then
  237. echo "404,server not found..."
  238. elif [[ $GROUP == *"redis-clustor-text"* ]];then
  239. ssh ip地址1 "rm -rf /data/redis-clustor-text/redis-clustor-text-0.0.1-SNAPSHOT.jar;LAST_VERSION=\$(ls -l -t /data/redis-clustor-text/redis-clustor-text-0.0.1-SNAPSHOT.jar* |awk '{print \$9}' |head -n1);mv \$LAST_VERSION /data/redis-clustor-text/redis-clustor-text-0.0.1-SNAPSHOT.jar"
  240. elif [[ $GROUP == *"wx-master"* ]];then
  241. echo "404,server not found..."
  242. fi
  243. echo "回滚完成..."
  244. }
  245. function restart_app() {
  246. echo "重启中..."
  247. if [[ $GROUP == *"kn-delayMq-event-push"* ]];then
  248. ssh ip地址1 "/bin/bash /data/kn-delayMq-event-push/server.sh restart"
  249. elif [[ $GROUP == *"kn-delete-information"* ]];then
  250. echo "404,server not found..."
  251. elif [[ $GROUP == *"kn-delete-monitor"* ]];then
  252. ssh ip地址1 "/bin/bash /data/delete-monitor/server.sh restart"
  253. elif [[ $GROUP == *"kn-delete-publicsentiment"* ]];then
  254. echo "404,server not found..."
  255. elif [[ $GROUP == *"kn-monitor-MqHandle"* ]];then
  256. ssh ip地址1 "/bin/bash /data/MqHandle/server.sh restart"
  257. elif [[ $GROUP == *"kn-monitor-isexist"* ]];then
  258. echo "404,server not found..."
  259. elif [[ $GROUP == *"kn-monitor-provide"* ]];then
  260. ssh ip地址1 "/bin/bash /data/provide/server.sh restart"
  261. elif [[ $GROUP == *"kn-review-handle"* ]];then
  262. ssh ip地址1 "/bin/bash /data/review-handle/server.sh restart"
  263. elif [[ $GROUP == *"monitor-scheme-task"* ]];then
  264. echo "404,server not found..."
  265. elif [[ $GROUP == *"redis-clustor-text"* ]];then
  266. ssh ip地址1 "/bin/bash /data/redis-clustor-text/server.sh restart"
  267. elif [[ $GROUP == *"monitor_auto_push"* ]];then
  268. ssh ip地址2 "/bin/bash /data/monitor_auto_push/server.sh restart"
  269. elif [[ $GROUP == *"wx-master"* ]];then
  270. echo "404,server not found..."
  271. fi
  272. echo "重启完成..."
  273. }
  274. main(){
  275. case $1 in
  276. deploy_jar)
  277. clone_code
  278. code_maven
  279. scp_jar_file
  280. bak_jar
  281. cp_jar_file
  282. delete_history_jar
  283. restart_app
  284. ;;
  285. rollback_last_version)
  286. rollback_last_version
  287. restart_app
  288. ;;
  289. *)
  290. usage
  291. ;;
  292. esac
  293. }
  294. main $1 $2

脚本按自己需求进行修改,需要注意的是发服务的服务器,需要提前配置好免密,否则jenkins无法免密登录,会导致部署失败。

注意: 第一次构建运行时,由于linux系统验证问题,需要手动点击yes,所以会导致构建报错,

需要手动在容器内 git clone 输入一次yes。

 4.3 下面就可以在jenkins,上进行构建发布。

  

 这里可查看构建和发布输出的日志。

 输出 Finished: SUCCESS 代表发布成功,去对应部署服务的服务器上验证即可。


 补充一点,编辑打包和发布的脚本里,发布到对应服务器上时,会在发布服务的服务器上执行一个服务器重启的脚本,这个脚本需要提前准备,我也把我目前用的重启脚本发出来,大家可以参考。

  1. [root@iv-yd5ck6aeww5i3z3d41dk ~]# cat server.sh
  2. #!/bin/bash
  3. APP_NAME=test.jar
  4. BASE_PATH=/root
  5. usage() {
  6. echo "Usage: sh java_demo.sh [start|stop|restart|status]"
  7. exit 1
  8. }
  9. is_exist() {
  10. pid=`ps -ef | grep $APP_NAME | grep -v grep | awk '{print $2}' `
  11. #如果不存在返回1,存在返回0
  12. if [ -z "${pid}" ]; then
  13. return 1
  14. else
  15. return 0
  16. fi
  17. }
  18. start() {
  19. is_exist
  20. if [ $? -eq "0" ]; then
  21. echo "${APP_NAME} is already running. pid=${pid} ."
  22. else
  23. cd $BASE_PATH
  24. nohup java -jar -XX:+UseG1GC -Xms1024m -Xmx1024m -Xss256k -XX:MaxGCPauseMillis=100 -Xloggc:./gc.log -XX:+PrintGCTimeStamps -XX:+PrintGCDetails $APP_NAME > /root/test.log 2>&1 &
  25. echo $! > test.pid
  26. #tail -f test.log
  27. fi
  28. }
  29. stop() {
  30. is_exist
  31. if [ $? -eq 0 ]; then
  32. kill $pid
  33. sleep 5
  34. pid=`ps -ef | grep $APP_NAME | grep -v grep | awk '{print $2}'`
  35. while [ -n "$pid" ];
  36. do
  37. sleep 2
  38. pid=`ps -ef | grep $APP_NAME | grep -v grep | awk '{print $2}'`
  39. echo "The program exits gracefully, please wait..."
  40. done
  41. else
  42. echo "${APP_NAME} is not running"
  43. fi
  44. }
  45. status() {
  46. is_exist
  47. if [ $? -eq "0" ]; then
  48. echo "${APP_NAME} is running. Pid is ${pid}"
  49. else
  50. echo "${APP_NAME} is not running."
  51. fi
  52. }
  53. restart() {
  54. stop
  55. sleep 1
  56. start
  57. }
  58. case "$1" in
  59. "start")
  60. start
  61. ;;
  62. "stop")
  63. stop
  64. ;;
  65. "status")
  66. status
  67. ;;
  68. "restart")
  69. restart
  70. ;;
  71. *)
  72. usage
  73. ;;
  74. esac

 以上内容,可供大家测试使用,如果问题或者更好的方法和脚本,也请各位大佬提出。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/960839
推荐阅读
相关标签
  

闽ICP备14008679号