jenkins发布Spring Cloud(单机版)
一、概述
有一台测试服务器,需要部署Spring Cloud。通过Jenkins来发布!
环境说明
gitlab版本:GitLab 社区版 10.5.1
jenkins版本:2.219
测试服务器:操作系统为centos7.6
项目说明
微服务统一在一个git项目里面,项目的大致结构如下:
./ ├── auth-server │ ├── pom.xml │ └── src ├── common │ ├── pom.xml │ └── src ├── config-server │ ├── pom.xml │ └── src ├── eureka-server │ ├── pom.xml │ └── src ├── gateway │ ├── pom.xml │ └── src ├── pom.xml └── service-user ├── pom.xml └── src
主要有5个微服务器,分别是eureka,config,auth,user,gateway
二、ansible配置
增加分组
修改文件/etc/ansible/hosts,增加一个分组
[test_java] 192.168.28.229 ansible_ssh_port=22
ansible playbook
登录到jenkins服务器,创建目录
mkdir -p /opt/ansible/test
发布模板
vi /opt/ansible/test/deploy_standard_template.yml
内容如下:
--- - hosts: "{{ HOSTS }}" remote_user: root become: yes become_method: sudo # 需要传入变量HOSTS,ENV,PROJECT_NAME,PREFIX,PORT # 分别表示主机,环境,项目名,路径前缀(拼接jenkins jar包路径),端口。 # 声明变量 vars: # 远程项目基础目录 BASE_DIR: "/data/code" # 远程项目目录名 PROJECT_DIR: "{{ ENV }}_{{ PROJECT_NAME }}_{{ PORT }}" # 备份目录 BAK_DIR: "/data/website_backup" # 完整的jenkins jar包路径 JENKINS_JAR: "/data/jenkins_data/workspace/{{ JOB_NAME }}/{{ PREFIX }}/target/{{ PROJECT_NAME }}.jar" tasks: #删除原来的包 - name: move old package file: path={{ BASE_DIR }}/{{ PROJECT_DIR }}/{{ PROJECT_NAME }}.jar state=absent #同步jar包到目标服务器 - name: synchronize package to {{ PROJECT_NAME }} synchronize: src: "{{ JENKINS_JAR }}" dest: "{{ BASE_DIR }}/{{ PROJECT_DIR }}/" #修改包的属性 - name: modify package file: path="{{ BASE_DIR }}/{{ PROJECT_DIR }}/{{ PROJECT_NAME }}.jar" owner=root group=root # 备份当前的包 - name: backup jar shell: "mkdir -p {{ BAK_DIR }}/{{ PROJECT_DIR }}_rollback/{{ BUILD_NUMBER }}/;cp -a {{ BASE_DIR }}/{{ PROJECT_DIR }}/{{ PROJECT_NAME }}.jar {{ BAK_DIR }}/{{ PROJECT_DIR }}_rollback/{{ BUILD_NUMBER }}/;chown -R root:root {{ BAK_DIR }}/{{ PROJECT_DIR }}_rollback" #启动server - name: start shell: "/opt/script/{{ PROJECT_DIR }} restart" - name: view port,Wait for 90 seconds shell: "sleep 90;lsof -t -i:{{ PORT }}"
回滚模板
vi /opt/ansible/test/rollback_standard_template.yml
内容如下:
--- - hosts: "{{ HOSTS }}" remote_user: root become: yes become_method: sudo # 需要传入变量HOSTS,ENV,PROJECT_NAME,PORT,BUILD_ID # 分别表示主机,环境,项目名,端口,回滚的构建ID # 声明变量 vars: # 远程项目基础目录 BASE_DIR: "/data/code" # 远程项目目录名 PROJECT_DIR: "{{ ENV }}_{{ PROJECT_NAME }}_{{ PORT }}" # 备份目录 BAK_DIR: "/data/website_backup" tasks: # 还原指定的包 - name: reduction package shell: "\\cp -a {{ BAK_DIR }}/{{ PROJECT_DIR }}_rollback/{{ BUILD_ID }}/{{ PROJECT_NAME }}.jar {{ BASE_DIR }}/{{ PROJECT_DIR }}/{{ PROJECT_NAME }}.jar" #启动server - name: start shell: "/opt/script/{{ PROJECT_DIR }} restart" - name: view port,Wait for 80 seconds shell: "sleep 80;lsof -t -i:{{ PORT }}"
三、服务器配置
jdk配置
登录测试服务器,创建代码目录
mkdir -p /data/code
安装rsync,用于jenkins同步代码
yum install -y rsync
安装jdk,这里就不做描述了。请参考链接:
https://www.cnblogs.com/xiao987334176/p/11678894.html
java启动脚本
创建脚本目录
mkdir /opt/script
eureka启动脚本
vi /opt/script/test_eureka-server_8761
内容如下
#!/bin/bash # auditd Start jar package # chkconfig: 2345 14 87 # description: This is admin project #define var export JAVA_BIN=/data/jdk1.8.0_211/ export JAR_NAME=eureka-server.jar export PROJECT_SERVICE=eureka-server export ENV=test export XMS=512m export XMX=512m export PORT=8761 export PROJECT_HOME=/data/code/${ENV}_${PROJECT_SERVICE}_${PORT} . /etc/init.d/functions export START="nohup ${JAVA_BIN}bin/java -Xms${XMS} -Xmx${XMX} -XX:MetaspaceSize=50M -XX:MaxMetaspaceSize=256m -jar ${PROJECT_HOME}/${JAR_NAME} &" #服务脚本 start(){ echo "${PROJECT_SERVICE} starting....." source /etc/profile && cd $PROJECT_HOME && eval $START if [ $? -eq 0 ];then action "$PROJECT_SERVICE is starting" /bin/true else action "$PROJECT_SERVICE is starting" /bin/false fi } stop(){ for i in $(ps -aux|grep ${JAR_NAME}|grep -v grep|awk '{print $2}');do kill -9 $i; done if [ $? -eq 0 ];then action "$PROJECT_SERVICE is stoping" /bin/true else action "$PROJECT_SERVICE is stoping" /bin/false fi } status(){ if [ `ss -tunlp|grep ${PORT}|awk '{print $5}'|cut -d: -f2` = ${PORT} ];then echo "${PROJECT_SERVICE} is running....." else echo "${PROJECT_SERVICE} is stopping....." fi } case $1 in start) start ;; stop) stop ;; restart) stop start ;; status) status ;; *) echo "$0 <start|stop|restart>" esac
添加执行权限
chmod 755 /opt/script/test_eureka-server_8761
其它4个微服务脚本也是同样的,注意修改项目名和端口号。
四、jenkins配置
确保jenkins已经安装了插件:Git Parameter
新建job
名为:test_eureka-server,选择自由风格的。
参数化构建
指定分支
build参数
构建后执行shell脚本
完整内容如下:
case $Status in Deploy) echo "Status:$Status" ansible-playbook -v /opt/ansible/test/deploy_standard_template.yml -e "HOSTS=test_java JOB_NAME=${JOB_NAME} BUILD_NUMBER=${BUILD_NUMBER} ENV=test PROJECT_NAME=eureka-server PREFIX=eureka-server PORT=8761" echo "Completing!" ;; Rollback) echo "Status:$Status" echo "BUILD_ID:$BUILD_ID" ansible-playbook -v /opt/ansible/test/rollback_standard_template.yml -e "HOSTS=test_java ENV=test PROJECT_NAME=eureka-server PORT=8761 BUILD_ID=${BUILD_ID}" echo "Completing!" ;; *) exit ;; esac
构建后操作
五、发布
点击build
直接点开始构建,build_id不需要输入
六、回滚
先查看左侧的build_id
如果要回到上一个版本,选择rollback,输入数字1
点击开始构建,就可以了!