自动化部署脚本参考

来自ling
跳转至: 导航搜索

需要在你发布的目标机器上编写该脚本,你也可以在开发环境编好后,放到目标机器上

Jenkins配置Publish Over SSH需要勾选Exec in pty

ps -ef | grep ling-cloud-register

日志默认会输出在当前执行命令文件夹下的logs中,不需要>将日志再次输出 见log4j2.xml配置

sh startup-ling-cloud-config.sh  &>startup-ling-cloud-config.log

maven命令

clean install -Dmaven.test.skip=true
-DskipTests,不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下。
-Dmaven.test.skip=true,不执行测试用例,也不编译测试用例类。

docker-maven-plugin

clean package docker:build

https://blog.csdn.net/aixiaoyang168/article/details/77453974

构建后操作

**/target/*.jar

停止原有程序

echo "Stopping ling-cloud-register Application"
pid=`ps -ef | grep ling-cloud-register | grep -v grep | awk '{print $2}'`
if [ -n "$pid" ]
then
   kill -9 $pid
fi
echo "Stopping ling-cloud-config Application"
pid=`ps -ef | grep ling-cloud-config | grep -v grep | awk '{print $2}'`
if [ -n "$pid" ]
then
   kill -9 $pid
fi
echo "Stopping ling-cloud-routing Application"
pid=`ps -ef | grep ling-cloud-routing | grep -v grep | awk '{print $2}'`
if [ -n "$pid" ]
then
   kill -9 $pid
fi
echo "end"
  • 启动新程序
cd /alidata/dockerdata/ling/cloud
export rootpath=`pwd -P`
echo "startup in: $rootpath"
echo "grant register"
chmod 777 $rootpath/ling-cloud-register/target/ling-cloud-register.jar
echo "exec register....."
nohup java -jar $rootpath/ling-cloud-register/target/ling-cloud-register.jar --eureka.client.serviceUrl.defaultZone=http://192.168.1.30:6889/eureka --eureka.instance.perferIpAddress=true --eureka.instance.hostname=192.168.1.30 >/dev/null 2>&1 &
echo "sleep 20"
sleep 20
echo "grant config"
chmod 777 $rootpath/ling-cloud-config/target/ling-cloud-config.jar
echo "exec config....."
nohup java -jar $rootpath/ling-cloud-config/target/ling-cloud-config.jar --eureka.client.serviceUrl.defaultZone=http://192.168.1.30:6889/eureka --eureka.instance.perferIpAddress=true --eureka.instance.hostname=192.168.1.30 >/dev/null 2>&1 &
echo "sleep 20"
sleep 20
echo "grant routing"
chmod 777 ling-cloud-routing/target/ling-cloud-routing.jar
echo "exec routing....."
nohup java -jar $rootpath/ling-cloud-routing/target/ling-cloud-routing.jar --eureka.client.serviceUrl.defaultZone=http://192.168.1.30:6889/eureka --eureka.instance.perferIpAddress=true --eureka.instance.hostname=192.168.1.30 >/dev/null 2>&1 &

更多参考nohup不生成nohup.out的方法

cd /alidata/dockerdata/ling/cloud
export rootpath=`pwd -P`
echo $rootpath
$rootpath/stop.sh
# nohup java -jar $rootpath/ling-cloud-register/target/ling-cloud-register.jar  &
# BUILD_ID=dontKillMe nohup $rootpath/startup.sh &
$rootpath/startup.sh

Jenkinsfile

Jenkinsfile (Declarative Pipeline)

 pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh './gradle build'
            }
        }
        stage('Test') {
            steps {
                sh 'echo "Fail!"; exit 1'
                sh './gradlew check'
            }
        }
    stage('Deploy - Staging') {
        steps {
            sh './deploy staging'
            sh './run-smoke-tests'
        }
    }
        stage('Sanity check') {
            steps {
                input "Does the staging environment look ok?"
            }
        }
     stage('Deploy - Production') {
        steps {
            sh './deploy production'
        }
        }
    }
    environment {
        DISABLE_AUTH = 'true'
        DB_ENGINE    = 'sqlite'
        AWS_ACCESS_KEY_ID     = credentials('AWS_ACCESS_KEY_ID')
        AWS_SECRET_ACCESS_KEY = credentials('AWS_SECRET_ACCESS_KEY')
    }
    post {
        always {
            echo 'This will always run'
            archive 'build/libs/**/*.jar'
            junit 'build/reports/**/*.xml'
            deleteDir() /* clean up our workspace */
        }
        success {
            echo 'This will run only if successful'
            slackSend channel: '#ops-room',
                  color: 'good',
                  message: "The pipeline ${currentBuild.fullDisplayName} completed successfully."
        }
        failure {
            echo 'This will run only if failed'
            mail to: 'team@example.com',
             subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
             body: "Something is wrong with ${env.BUILD_URL}"
            hipchatSend message: "Attention @here ${env.JOB_NAME} #${env.BUILD_NUMBER} has failed.",
                    color: 'RED'
        }
        unstable {
            echo 'This will run only if the run was marked as unstable'
        }
        changed {
            echo 'This will run only if the state of the Pipeline has changed'
            echo 'For example, if the Pipeline was previously failing but is now successful'
        }
    }
 }

docker network

docker network create --driver overlay ling-cloud --attachable

其他

#!/bin/sh

# update code
git pull

# package
mvn clean
mvn package -Dmaven.test.skip=true

# deploy
WAR=`ls target | grep war`
TOMCAT=/home/test/apache-tomcat-6.0.41
mv target/$WAR $TOMCAT
cd $TOMCAT
# invoke another deploy script
sh deploy-war.sh $WAR webapps

查找pid并杀掉

#!/bin/bash  
#defined   
TOMCAT_HOME="/save/apache-tomcat-7.0.56"  
TOMCAT_PORT=8080  
PROJECT="$1"  
#param validate  
if [ $# -lt 1 ]; then    
  echo "you must use like this : ./publish.sh <projectname> [tomcat port] [tomcat home dir]"    
  exit    
fi   
if [ "$2" != "" ]; then  
   TOMCAT_PORT=$2  
fi  
if [ "$3" != "" ]; then  
   TOMCAT_HOME="$3"  
fi  
#shutdown tomcat  
"$TOMCAT_HOME"/bin/shutdown.sh  
echo "tomcat shutdown"  
#check tomcat process  
tomcat_pid=`/usr/sbin/lsof -n -P -t -i :$TOMCAT_PORT`  
echo "current :" $tomcat_pid  
while [ -n "$tomcat_pid" ]  
do  
 sleep 5  
 tomcat_pid=`/usr/sbin/lsof -n -P -t -i :$TOMCAT_PORT`  
 echo "scan tomcat pid :" $tomcat_pid  
done  
#publish project  
echo "scan no tomcat pid,$PROJECT publishing"  
rm -rf "$TOMCAT_HOME"/webapps/$PROJECT*  
cp /save/$PROJECT*.war "$TOMCAT_HOME"/webapps/$PROJECT.war  
#bak project  
BAK_DIR=/save/bak/$PROJECT/`date +%Y%m%d`  
mkdir -p "$BAK_DIR"  
cp "$TOMCAT_HOME"/webapps/$PROJECT.war "$BAK_DIR"/"$PROJECT"_`date +%H%M%S`.war  
#remove tmp  
rm -rf /save/$PROJECT*.war  
#start tomcat  
"$TOMCAT_HOME"/bin/startup.sh  
echo "tomcat is starting,please try to access $PROJECT conslone url"

tomcat文件替换和重启

# 将应用停止
#stop.sh
#!/bin/bash
echo "Stopping SpringBoot Application"
pid=`ps -ef | grep model.jar | grep -v grep | awk '{print $2}'`
if [ -n "$pid" ]
then
   kill -9 $pid
fi
#replace.sh 用于将上次构建的结果备份,然后将新的构建结果移动到合适的位置
#!/bin/bash
# 先判断文件是否存在,如果存在,则备份
file="/usr/local/SBA/model.jar"
if [ -f "$file" ]
then
   mv /usr/local/SBA/model.jar /usr/local/SBA/backup/model.jar.`date +%Y%m%d%H%M%S`
fi
mv /root/.jenkins/workspace/Test/target/model.jar /usr/local/SBA/model.jar
# startup.sh 启动项目
#!/bin/sh
echo "授予当前用户权限"
chmod 777 /usr/local/SBA/model.jar
echo "执行....."
java -jar /usr/local/SBA/model.jar
#先用新文件替换tomcat下的旧文件   
cp -r   /root/deploy/*    /usr/local/my/tomcat/webapps/test/WEB-INF/classes #关闭tomcat 
sh /usr/local/my/tomcat/bin/shutdown.sh sleep 15s #启动tomcat 
sh /usr/local/my/tomcat/bin/startup.sh  
获取tomcat  pid的脚本如下: 
ps -ef|grep tomcat|grep start|grep -v  'grep'|awk '{print $2}' 

杜老师

spring boot

  • maven
clean package -Pdev
  • shell
env=dev
service_name=tms-iit-service
env_service_name=${env}_${service_name}
scp /data/jenkins/workspace/${env}_${service_name}/target/${service_name}*.jar tmslnxdev002:/data/deploy/${env_service_name}.jar
  • 远程调用
env=dev
service_name=tms-iit-service
service_port=8500
env_service_name=${env}_${service_name}
ipAddress=47.100.28.123
deploy_date=$(date +%Y%m%d%H%M)

cd /data/deploy

n=2
for((i=0;i<=n-1;i++));  
do   
       instance_port=$[ ${service_port} + i ]
       docker_name=${env_service_name}_${instance_port}
       echo ${docker_name}
       docker stop ${docker_name}
       sleep 10
       docker rm -f ${docker_name}
       ps -ef|grep ${env_service_name}|grep ${instance_port}|grep -v grep|cut -c 9-15|xargs kill -9
       rm -rf /data/deploy/${docker_name}.jar
       cp ${env_service_name}.jar ${docker_name}.jar
       rm -rf /data/deploy/bootstrap_${docker_name}.sh
       cp ./config/bootstrap.sh ./bootstrap_${docker_name}.sh
       echo "nohup java  -Xms512m -Xmx1024m -jar  -Drocketmq.log4j2.enabled=true  -Dfile.encoding=UTF-8 -Ddefault.client.encoding=UTF-8 -Duser.timezone=GMT+08  -Dspring.profiles.active=${env} -Dserver.port=${instance_port}  -Deureka.instance.preferIpAddress=true   -Deureka.instance.ip-address=${ipAddress} -javaagent:/data/deploy/transmittable-thread-local-2.10.2.jar=ttl.agent.enable.timer.task:true /data/deploy/${docker_name}.jar  > /data/log/${docker_name}.log &" >> ./bootstrap_${docker_name}.sh
       docker run -d --name ${docker_name} -v /data:/data -v /data/log:/log  -v /data/deploy/bootstrap_${docker_name}.sh:/etc/bootstrap.sh --expose=${instance_port} -p ${instance_port}:${instance_port}  --restart always tmslnxdev001:5000/centos7-jdk8
     sleep 60
done 

rm -rf /data/deploy/${env_service_name}.jar

portal

  • shell
env=dev
web_name=tms-portal

cd /data/deploy
rm -rf ./${env}_${web_name}.tar.gz
rm -rf ./${env}_${web_name} 
\cp -f -r /data/jenkins/workspace/${env}_${web_name}  /data/deploy
tar -zcvf ${env}_${web_name}.tar.gz ./${env}_${web_name} --exclude=.git
scp /data/deploy/${env}_${web_name}.tar.gz  tmslnxdev002:/data/deploy/
env=dev
web_name=tms-portal
cd /data/deploy/
rm -rf ./${env}_${web_name}
tar -zxvf ./${env}_${web_name}.tar.gz
cd  /data/deploy/${env}_${web_name}
yarn
ng build -c=ip --sourceMap=true
env=dev
web_name=tms-portal
web_port=4200
cd /data/deploy

n=1
for((i=0;i<=n-1;i++));  
do   
        instance_name=${web_name}-${i}
        instance_port=$[ ${web_port} + i ]
	docker stop ${instance_name}
	docker rm -f ${instance_name}
	docker network disconnect -f bridge${instance_name}

        dirname=${env}_${instance_name} 
	rm -rf /data/deploy/${dirname}
       mkdir -p  /data/deploy/${dirname}
	
       \cp -r /data/deploy/${env}_${web_name}/dist  /data/deploy/${dirname}/
  
      docker run -d --name=${instance_name}  --expose=80 -p ${instance_port}:80  -v /data:/data -v /data/config/nginx.conf:/etc/nginx/nginx.conf -v /data/deploy/${dirname}:/opt/${web_name} --privileged --restart always  tmslnxdev001:5000/nginx 

done 

mv -f /data/deploy/${env}_${web_name}.tar.gz /data/deploy/${env}_${web_name}.tar.gz.bak