diff --git a/shell/deploy-jar-machine/backup_image.sh b/shell/deploy-jar-machine/backup_image.sh new file mode 100644 index 0000000..c3b55fd --- /dev/null +++ b/shell/deploy-jar-machine/backup_image.sh @@ -0,0 +1,31 @@ +#! /bin/sh +#镜像名称 +image_name=$1 +#标签 +image_tag=$2 +#镜像仓库地址 +registry_url=$3 +#备份tag +image_bak_tag=$4 + +#带仓库地址镜像名称 +image_full_name=$registry_url/$image_name + +colunm3='$3' + +# 获取当前正在运行的镜像 +echo "docker images | grep -w $image_full_name | awk '{print $colunm3}'" +image_check_command=`docker images | grep -w $image_full_name | awk '{print $3}'` +if [ "$image_check_command" != "" ] ; then + # 将运行的镜像,重新修改tag命名为备份tag + echo "docker tag ${image_full_name}:${image_tag} ${image_full_name}:${image_bak_tag}" + docker tag ${image_full_name}:${image_tag} ${image_full_name}:${image_bak_tag} + # 将备份的镜像发送到镜像仓库,备份起来 + echo "docker push ${image_full_name}:${image_bak_tag}" + docker push ${image_full_name}:${image_bak_tag} + #将本地刚重命名的镜像删除 + echo "docker rmi ${registry_url}/${image_name}:$image_bak_tag" + docker rmi ${registry_url}/${image_name}:$image_bak_tag +fi + + diff --git a/shell/deploy-jar-machine/backup_image_eureka.sh b/shell/deploy-jar-machine/backup_image_eureka.sh new file mode 100644 index 0000000..6b86c44 --- /dev/null +++ b/shell/deploy-jar-machine/backup_image_eureka.sh @@ -0,0 +1,29 @@ +#! /bin/sh +#镜像名称 +image_name=$1 +#标签 +image_tag=$2 +#镜像仓库地址 +registry_url=$3 +#备份tag +image_bak_tag=$4 + +#带仓库地址镜像名称 +image_full_name=$registry_url/$image_name + +colunm3='$3' + +# 获取当前正在运行的镜像 +echo "docker images | grep -w $image_full_name | awk '{print $colunm3}'" +image_check_command=`docker images | grep -w $image_full_name | awk '{print $3}'` +echo "image_check_command-----$image_check_command" +if [ "$image_check_command" != "" ] ; then + # 将运行的镜像,重新修改tag命名为备份tag + echo "docker tag ${image_full_name}:${image_tag} ${image_full_name}:${image_bak_tag}" + docker tag ${image_full_name}:${image_tag} ${image_full_name}:${image_bak_tag} + #将本地刚重命名的镜像删除 + echo "docker rmi ${registry_url}/${image_name}:$image_bak" + docker rmi ${registry_url}/${image_name}:$image_bak +fi + + diff --git a/shell/deploy-jar-machine/delete_image.sh b/shell/deploy-jar-machine/delete_image.sh new file mode 100644 index 0000000..392f6a8 --- /dev/null +++ b/shell/deploy-jar-machine/delete_image.sh @@ -0,0 +1,28 @@ +#! /bin/sh +#镜像名称 +imageName=$1 +tag=$2 + +#容器字符串 +containerStr=`docker ps -a | grep -w ${imageName}:${tag} | awk '{print $1}'` +imageStr=`docker images | grep -w $imageName | awk '{print $3}'` + +if [ "$imageStr" != "" ] ; then + if [ "$containerStr" != "" ] ; then + #停掉容器 + docker stop `docker ps -a | grep -w ${imageName}:${tag} | awk '{print $1}'` + + #删除容器 + docker rm -f `docker ps -a | grep -w ${imageName}:${tag} | awk '{print $1}'` + + #删除镜像 + docker rmi --force ${imageName}:${tag} + + echo "停止容器、删除镜像${imageName}:${tag}成功" + else + #删除镜像 + docker rmi --force ${imageName}:${tag} + echo "删除镜像${imageName}:${tag}成功" + fi +fi + diff --git a/shell/deploy-jar-machine/deploy_image.sh b/shell/deploy-jar-machine/deploy_image.sh new file mode 100644 index 0000000..071c42f --- /dev/null +++ b/shell/deploy-jar-machine/deploy_image.sh @@ -0,0 +1,47 @@ +#! /bin/sh +group_id=$1 +image_name=$1/$2 +module_name=$2 +image_port=$3 +registry_url=$4 +eureka_url=$5 +image_tag=latest +host_port=$image_port + +# 截取到服务名称,如fastwave/fastwave-service-admin,载取后变为fastwave-service-admin +service_withprefix_name=$1 +service_name_array=(${service_withprefix_name//\// }) +service_name=${service_name_array[1]} + +#向eureka发送指令,让服务停止,即不再接收新的请求,这样终止服务将更柔性化 +nohup sh /data/jenkins_shell/disable_service.sh ${service_name} ${image_port} ${eureka_url} + +# 休眠一段时间,建议设置超过90秒以上,演示可以设置为30秒 +# sleep 2m +sleep 30s + +# 备份镜像到镜像仓库 +echo "备份镜像到镜像仓库" +#/data/jenkins_shell/backup_image.sh $image_name $image_tag $registry_url + +#两个可以不用停止WEB服务就可以清空nohup.out的命令。 +#第一种:cp /dev/null nohup.out +#第二种:cat /dev/null > nohup.out +cp /dev/null nohup.out +#sleep 2m +#停掉容器、删除容器、删除镜像 +echo "停掉容器、删除容器、删除镜像 /data/jenkins_shell/delete_image.sh $image_name $image_tag" +/data/jenkins_shell/delete_image.sh $image_name $image_tag +#pull镜像 +#echo "docker pull $image_name:$image_tag" +docker pull localhost:5001/${image_name} + +# 将运行的镜像,重新修改tag命名为server +docker tag localhost:5001/${image_name}:${image_tag} ${image_name}:${image_tag} +#将本地刚重命名的镜像删除 +echo "docker rmi localhost:5001/${image_name}:${image_tag}" +docker rmi localhost:5001/${image_name}:${image_tag} + +#运行镜像 +echo "运行镜像 docker run --restart=always --name ${module_name} -d -t ${image_name}:$image_tag" +docker run --restart=always --name ${module_name} -d --network=host -t ${image_name}:$image_tag diff --git a/shell/deploy-jar-machine/deploy_image_eureka.sh b/shell/deploy-jar-machine/deploy_image_eureka.sh new file mode 100644 index 0000000..5c36918 --- /dev/null +++ b/shell/deploy-jar-machine/deploy_image_eureka.sh @@ -0,0 +1,63 @@ +#! /bin/sh +image_name=$1 +image_port=$2 +registry_url=$3 +eureka_url=$4 +profile_server=$5 +profile=$6 #--spring.profiles.active=xxxx +host_port=$2 +image_tag=latest + +echo "image_name:${image_name}" +echo "image_port:${image_port}" +echo "image_tag:${image_tag}" +echo "registry_url:${registry_url}" +echo "eureka_url:${eureka_url}" +echo "profile_server:${profile_server}" +echo "profile:${profile}" + +# 截取到服务名称,如fastwave/fastwave-service-admin,载取后变为fastwave-service-admin +echo "(1)截取到服务名称:$1" +service_withprefix_name=$1 +service_name_array=(${service_withprefix_name//\// }) +service_name=${service_name_array[1]} + +#向eureka发送指令,让服务停止,即不再接收新的请求,这样终止服务将更柔性化 +echo "(2) nohup sh /data/jenkins_shell/disable_service.sh ${service_name} ${image_port} ${eureka_url}" +nohup sh /data/jenkins_shell/disable_service.sh ${service_name} ${image_port} ${eureka_url} + +# 休眠一段时间,建议设置超过90秒以上,演示可以设置为30秒 +# sleep 2m +sleep 30s + +# 备份镜像到镜像仓库 +# echo "(3)备份镜像到镜像仓库" +#/data/jenkins_shell/backup_image_eureka.sh $image_name $image_tag $registry_url $profile_server + + +#两个可以不用停止WEB服务就可以清空nohup.out的命令。 +#第一种:cp /dev/null nohup.out +#第二种:cat /dev/null > nohup.out +cp /dev/null nohup.out +#sleep 2m +#停掉容器、删除容器、删除镜像 +echo "(4) 停掉容器、删除容器、删除镜像" +/data/jenkins_shell/delete_image.sh $image_name $image_tag + +#pull镜像 如果是两个虚拟机,则从要部署的机器195拉取136,这里是阿里云,无法拉取本地的镜像,所有这里注释掉 +#echo "(5) pull镜像" +#echo "docker pull $image_name:$image_tag" +docker pull localhost:5001/${image_name} + +# 将运行的镜像,重新修改tag命名为server +docker tag localhost:5001/${image_name}:${image_tag} ${image_name}:${image_tag} +#将本地刚重命名的镜像删除 +echo "docker rmi localhost:5001/${image_name}:${image_tag}" +docker rmi localhost:5001/${image_name}:${image_tag} + +#运行镜像 +echo "(6) 运行镜像 " +echo "docker run --restart=always --name renren-register -d --network=host -t ${image_name}:$image_tag ${profile}" +#docker run --restart=always --name renren-register -d --network=host -t ${image_name}:$image_tag ${profile} +docker run --restart=always --name renren-register -d --network=host -t ${image_name}:$image_tag ${profile} + diff --git a/shell/deploy-jar-machine/disable_service.sh b/shell/deploy-jar-machine/disable_service.sh new file mode 100644 index 0000000..25491b6 --- /dev/null +++ b/shell/deploy-jar-machine/disable_service.sh @@ -0,0 +1,13 @@ +#!/bin/sh +image_name=$1 +host_port=$2 +eureka_url=$3 + +# 获取本地IP地址,以centos7以上版本为参考(其它版本命令需要作调整) +# ip=`ifconfig eth0|grep broadcast |awk '{print $2}'` +ip=`ip a | grep inet | grep -v inet6 | grep -v 127 | sed 's/^[ \t]*//g' | cut -d ' ' -f2 |cut -d '/' -f1 | awk 'NR==1'` + +# 这里假定本机有eurake(如不存在或集群需多次尝试) +# 示例:http://10.101.43.197:8761/eureka/apps/fastwave-service-admin/fastwave-service-admin:10.101.43.197:8764/status?value=OUT_OF_SERVICE +echo "curl -X PUT -i http://${eureka_url}/eureka/apps/${image_name}/${ip}:${image_name}:${host_port}/status?value=OUT_OF_SERVICE" +curl -X PUT -i http://${eureka_url}/eureka/apps/${image_name}/${ip}:${image_name}:${host_port}/status?value=OUT_OF_SERVICE diff --git a/shell/deploy-jar-machine/docker_clear.sh b/shell/deploy-jar-machine/docker_clear.sh new file mode 100644 index 0000000..bfcf322 --- /dev/null +++ b/shell/deploy-jar-machine/docker_clear.sh @@ -0,0 +1,3 @@ +docker stop $(docker ps -a -q) +docker rm $(docker ps -a -q) +docker rmi $(docker images) diff --git a/shell/deploy-jar-machine/rollback_service.sh b/shell/deploy-jar-machine/rollback_service.sh new file mode 100644 index 0000000..b25aad4 --- /dev/null +++ b/shell/deploy-jar-machine/rollback_service.sh @@ -0,0 +1,26 @@ +#! /bin/sh +registry_url=$5 +image_name=$1 +image_tag=$2 +image_port=$3 +host_port=$4 +eureka_url=$6 +image_bak_tag=$7 +image_full_name=$registry_url/$image_name + +# 截取到服务名称,如fastwave/fastwave-service-admin,载取后变为fastwave-service-admin +service_withprefix_name=$1 +service_name_array=(${service_withprefix_name//\// }) +service_name=${service_name_array[1]} + +#停掉容器、删除容器、删除镜像 +echo "停掉容器、删除容器、删除镜像" +/data/jenkins_shell/delete_image.sh $image_full_name $image_tag + +#pull镜像 +echo "docker pull $image_full_name:$image_bak_tag" +docker pull $image_full_name:$image_bak_tag + +#运行镜像 +echo "docker run --net=host -d -p ${image_port}:${host_port} -t ${image_full_name}:$image_bak_tag" +docker run --net=host -d -p ${image_port}:${host_port} -t ${image_full_name}:$image_bak_tag diff --git a/shell/jenkins-machine/delete_image.sh b/shell/jenkins-machine/delete_image.sh new file mode 100644 index 0000000..4cd610a --- /dev/null +++ b/shell/jenkins-machine/delete_image.sh @@ -0,0 +1,27 @@ +#! /bin/sh +#镜像名称 +imageName=$1 +tag=$2 + +#容器字符串 +containerStr=`docker ps -a | grep -w ${imageName}:${tag} | awk '{print $1}'` +imageStr=`docker images | grep -w $imageName | awk '{print $3}'` +echo "imageStr:::${imageStr}----containerStr:::${containerStr}" +if [ "$imageStr" != "" ] ; then + if [ "$containerStr" != "" ] ; then + #停掉容器 + docker stop `docker ps -a | grep -w ${imageName}:${tag} | awk '{print $1}'` + + #删除容器 + docker rm `docker ps -a | grep -w ${imageName}:${tag} | awk '{print $1}'` + + #删除镜像 + docker rmi ${imageName}:${tag} + else + #删除镜像 + #docker rmi ${imageName}:${tag} + echo "skip docker rmi container" + + fi +fi + diff --git a/shell/jenkins-machine/upload_image.sh b/shell/jenkins-machine/upload_image.sh new file mode 100644 index 0000000..fa51753 --- /dev/null +++ b/shell/jenkins-machine/upload_image.sh @@ -0,0 +1,35 @@ +#! /bin/sh +#镜像名称 +image_name=$1 +#标签 +image_tag=$2 +#镜像仓库地址 +registry_url=$3 +#带仓库地址镜像名称 +image_full_name=${registry_url}/$image_name + +image_check_command=`docker images | grep -w $image_full_name | awk '{print $3}'` +echo "(1)image_check_command=$image_check_command" +if [ "$image_check_command" != "" ] ; then + #将仓库中的镜像名称删除 + docker rmi ${registry_url}/${image_name}:$image_tag +fi + +echo "(2)docker tag ${image_name}:${image_tag} ${registry_url}/$image_name" + +#必须要先将镜像的名称给变成 域名或ip/镜像名 +docker tag ${image_name}:${image_tag} ${registry_url}/$image_name + + +#推送到仓库上 +echo "(3-1)docker push ${registry_url}/$image_name" +docker push ${registry_url}/$image_name + +#删除本地镜像 +echo "(4)docker rmi $image_name:${image_tag}" +#docker rmi ${registry_url}/$image_name +docker rmi $image_name:${image_tag} + +echo "OK" + + diff --git a/shell/jenkins-pipeline/deploy_eureka b/shell/jenkins-pipeline/deploy_eureka new file mode 100644 index 0000000..b037567 --- /dev/null +++ b/shell/jenkins-pipeline/deploy_eureka @@ -0,0 +1,109 @@ + +pipeline { + agent any +// environment { +// def imageName = "cloud/${project_names_and_port}" +// def tag="latest" +// } + tools { + maven "maven-3.6.3" + } + stages { + stage('check out') { + steps { + script{ + def gitea_url + if("${gitea_server}" == "136Git") + { + gitea_url = "$local136_gitea_url" + } + if("${gitea_server}" == "aliyunGitea") + { + gitea_url = "$aliyun_gitea_url" + } + echo "================${gitea_url}" + checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: "${gitea_server}", url: gitea_url]]]) + + } + } + } + stage('building dependency') { + steps { + sh ''' + mvn -f renren-commons clean install -Dautoconfig.skip=true -Dmaven.test.skip=false -Dmaven.test.failure.ignore=true + ''' + } + } + + stage('test get param') { + steps { + script { + echo "registry_url=${registry_url}" + // project_names_and_port=renren-register@8761 + // select_ssh_name=aliyun@server1,195@server1,195@server2,195@server3 + def selectedProject = "${project_names_and_port}".split(',') + def selectedDeployServer = "${select_ssh_name}".split(',') + for(int i=0; i /opt/jenkins_shell/delete_image.log 2>&1" + + sh "mvn -f $module_name clean install dockerfile:build -Dautoconfig.skip=true -Dmaven.test.skip=false -Dmaven.test.failure.ignore=true" + + sh "$shell_path/upload_image.sh $group_id/$module_name $latest_tag $registry_url > /opt/jenkins_shell/upload_image.log 2>&1" + + echo "######### module_name=${module_name},register_port=${register_port}" + + + for(int j=0; j /data/jenkins_shell/deploy_image_eureka.log 2>&1", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) + + } + + + } + + } + + } + } + } +} \ No newline at end of file diff --git a/shell/jenkins-pipeline/deploy_microservice b/shell/jenkins-pipeline/deploy_microservice new file mode 100644 index 0000000..5f35876 --- /dev/null +++ b/shell/jenkins-pipeline/deploy_microservice @@ -0,0 +1,87 @@ +pipeline { + agent any +// environment { +// def imageName = "cloud/${project_names_and_port}" +// def tag="latest" +// } + tools { + maven "maven-3.6.3" + } + stages { + stage('check out') { + steps { + script{ + def gitea_url + if("${gitea_server}" == "136Git") + { + gitea_url = "$local136_gitea_url" + } + if("${gitea_server}" == "aliyunGitea") + { + gitea_url = "$aliyun_gitea_url" + } + echo "================${gitea_url}" + checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: "${gitea_server}", url: gitea_url]]]) + + } + } + } + stage('building dependency') { + steps { + sh ''' + mvn -f renren-commons clean install -Dautoconfig.skip=true -Dmaven.test.skip=false -Dmaven.test.failure.ignore=true + mvn -f renren-module/stock/stock-client clean install -Dautoconfig.skip=true -Dmaven.test.skip=false -Dmaven.test.failure.ignore=true + mvn -f renren-admin/renren-admin-client clean install -Dautoconfig.skip=true -Dmaven.test.skip=false -Dmaven.test.failure.ignore=true + ''' + } + } + + + stage('build images and deploy') { + + steps { + script { + //if(false){ + echo "registry_url=${registry_url}" + def selectedProjectNamesAndPort = "${project_names_and_port}".split(',') + def selectedSshNames = "${select_ssh_name}".split(',') + for(int i=0; i /opt/jenkins_shell/delete_image.log 2>&1" + + sh "mvn -f $projectNameWithGroup clean install dockerfile:build -Dautoconfig.skip=true -Dmaven.test.skip=false -Dmaven.test.failure.ignore=true" + + sh "$shell_path/upload_image.sh $group_id/$module_name $latest_tag $registry_url > /opt/jenkins_shell/upload_image.log 2>&1" + + echo "######### module_name=${module_name},register_port=${register_port}" + + + for(int j=0; j /data/jenkins_shell/deploy_image.log 2>&1", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) + + } + + // } + } + + } + } + } + } +} \ No newline at end of file diff --git a/shell/jenkins-pipeline/renren-cloud-admin b/shell/jenkins-pipeline/renren-cloud-admin new file mode 100644 index 0000000..152ff76 --- /dev/null +++ b/shell/jenkins-pipeline/renren-cloud-admin @@ -0,0 +1,56 @@ +pipeline { + agent any + //parameters { + //string(name:'publish_server', defaultValue: '195', description: '部署服务器') + //} + + stages { + // 获取或更新最新代码 + stage('check out') { + steps { + script{ + def gitea_url + if("${gitea_server}" == "136Git") + { + gitea_url = "$local136_gitea_url" + } + if("${gitea_server}" == "aliyunGitea") + { + gitea_url = "$aliyun_gitea_url" + } + echo "================${gitea_url}" + // git 通过 用户名、密码认证 + checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: "${gitea_server}", url: gitea_url]]]) + echo "check out OK" + } + } + } + stage('build') { + steps { + nodejs('node10.19') { + sh ''' + cd /root/.jenkins/workspace/${single_project_name} + rm -rf dist + npm config set registry https://registry.npmmirror.com + npm install + npm install echarts@4.9.0 + npm run build + #cd dist + #JENKINS_NODE_COOKIE=dontKillMe nohup npm start & + ''' + } + } + } + stage('publish') { + steps { + echo "copy file to nginx server" + sh ''' + pwd + ''' + sshPublisher(publishers: [sshPublisherDesc(configName: "$publish_server", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "/data/openresty/nginx/html/", remoteDirectorySDF: false, removePrefix: "dist", sourceFiles: "dist/**/*")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)]) + echo "copy file to nginx server end" + } + } + } + +} \ No newline at end of file