shell脚本实现自动更新服务器项目

1,210 阅读1分钟

需要两个shell脚本,直接看代码:

  • 进入远程服务器
#!/bin/bash

echo $1
echo $2

DEPLOY_SERVER="*.*.*.*"

if [[ $1 = "production" ]]; then
    DEPLOY_SERVER="*.*.*.*"
elif [[ $1 = "test" ]]; then
    DEPLOY_SERVER="*.*.*.*"
fi

echo $DEPLOY_SERVER




ssh root@$DEPLOY_SERVER 'bash -s' < deploy/remote_run.sh $2

说明: *号那些代表的是服务器地址;remote_run.sh是下面代码的文件名称

  • 远程更新代码 pm2重启发布项目
#!/bin/bash

DIR="/srv/koala-blog"
BRANCH=$1

echo $BRANCH

cd $DIR

# git status
# git stash
# git checkout .

git fetch
git checkout $BRANCH
git pull origin $BRANCH

# cnpm install

pm2 restart koala-blog
pm2 logs koala-blog

说明: DIR是项目在linux服务器中的地址

  • 脚本运行启动

./deploy/run.sh 项目环境(production正式服务器,test测试服务器) 分支名称

eg: ./deploy/run.sh production master
    ./deploy/run.sh test master

觉得本文对你有帮助?请分享给更多人

欢迎大家关注我的公众号——程序员成长指北。请自行微信搜索——“程序员成长指北”