欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

Linux之自动化部署

程序员文章站 2022-05-01 09:46:59
No.1  自动化部署git项目 No.2  自动化更新git项目 No.3  自动化部署已有项目 No.4  自动化回滚项目 ———— 欢迎各路大神指点,转载请附上链接:https://www.cnblogs.com/leozhang... ......


no.1  自动化部署git项目

一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一

#!/bin/bash

# 清除项目进程和历史文件
pkill -f start.py
sleep 1
cd /root/automation |rm -rf testproduce/

# 获取项目最新git代码(前提服务器配置好git账户)
git clone git@dev.test.com:test_code/testproduce.git

# 启动项目
cd testproduce/
nohup /usr/python/bin/python3 start.py &
sleep 3

# 检查是否启动成功
pinfo=`pgrep -af start.py`
if [ -n $pinfo ]
then
  echo "successfully!!!"
else
  echo "failed!!!"
fi

 

 

 

no.2  自动化更新git项目

一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一

#!/bin/bash

# 切换至项目路径
cd /root/automation

# 检查项目是否有更新
gitinfo=`git pull`
if [[ "${gitinfo}" == "already up-to-date." ]]
then
  echo "already up-to-date."
else
  # 重启项目
  pkill -f start.py
  sleep 1
  nohup /usr/python/bin/python3 start.py &
  sleep 3

  # 检查是否启动成功
  pinfo=`pgrep -af start.py`
  if [ -n $pinfo ]
  then
    echo "successfully!!!"
  else
    echo "failed!!!"
  fi
fi

 

 

no.3  自动化部署已有项目

一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一

#!/bin/bash

# 设置源服务器信息
username="root"
password="root"
host="10.22.33.44"
dir="/usr/local/app"

# 备份当前项目(以备回滚)
echo "saving testproduce..."
now=`date +%y%m%d%h%m%s`
cd $dir | mkdir -p bak/$now
tar -czvf testproduce.tar.gz testproduce/ testproduce-web/
mv testproduce.tar.gz bak/$now/testproduce.tar.gz

# 拷贝项目更新包
echo "copying testproduce..."
/usr/bin/expect<<eof
  set timeout 10
  spawn scp -r $username@$host:$dir/testproduce-web/ $dir
  expect "*password:"
  send "$password\r"
  expect eof

  spawn scp -r $username@$host:$dir/testproduce/lib $dir/testproduce/
  expect "*password:"
  send "$password\r"
  expect eof

  spawn scp $username@$host:$dir/testproduce/testproduce.jar $dir/testproduce/
  expect "*password:"
  send "$password\r"
  expect eof
  #interact
eof

# 重启项目
echo "restarting testproduce..."
sh testproduce/restart.sh
sleep 8

# 检查是否启动成功
pinfo=`pgrep -af testproduce.jar`
if [ -n $pinfo ]
then
  echo "successfully!!!"
else
  echo "failed!!!"
fi

:<<commentblock
  pkill -f testproduce.jar
commentblock

 

no.4  自动化回滚项目

一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一

#!/bin/bash

# 清除当前项目
echo "clear..."
rm -rf testproduce* |cd bak

# 检查是否指定回滚版本(默认回滚上个版本,按日期排序,所以此路径不能有其他文件)
if [ -z $1 ]
then
  vs=`ls -l |sort -r |awk 'nr==2 {print $nf}'`
else
  vs=$1
fi

# 回滚项目
echo "reset>>> $vs"
cd $vs |cp testproduce.tar.gz ../../
tar -zxvf testproduce.tar.gz |rm -f testproduce.tar.gz

# 重启项目
echo "restarting testproduce..."
sh testproduce/restart.sh
sleep 8

# 检查是否启动成功
pinfo=`pgrep -af testproduce.jar`
if [ -n $pinfo ]
then
  echo "successfully!!!"
else
  echo "failed!!!"
fi

 

 

———— 欢迎各路大神指点,转载请附上链接:https://www.cnblogs.com/leozhanggg/p/10917704.html