本地VUE项目自动化部署到服务器
一、 授权免密登陆
(1) 本地新建.ssh文件夹,然后进入该文件夹
mkdir .ssh
cd .ssh
(2) ssh-keygen -t rsa –P 生成公钥私钥对,.ssh 目录下会生成 id_rsa.pub等文件
(3) 复制 id_rsa.pub 中的内容到 服务器 上的 .ssh/authorized_keys文件 (若没有该文件需要新建,给相应的权限)
二、编辑脚本
touch deploy.sh
#!/bin/sh
handle=$1;
env=$2;
#用户名
APP_USR=username
#服务器ip地址
ip=yourip
#项目地址
APP_HME=项目地址
#远程部署机 webhook
#清空dist目录
emptyRemoteDist(){
if [ $env == "prod" ]
then
echo "[exec]remove remote:yourip folder"
ssh $APP_USR@$ip "rm -rf APP_HME*"
else
echo "[exec]remove remote:yourip folder"
ssh $APP_USR@$ip "rm -rf APP_HME*"
fi
}
#发送文件到正式服
transferFileToProSever(){
echo "[exec]transfer file to product:yourip sever"
scp -r ./dist/* $APP_USR@$ip:$APP_HME
}
#发送文件到测试服
transferFileToTestSever(){
echo "[exec]transfer file to development::yourip sever"
echo $ip
scp -r ./dist/* $APP_USR@$ip:$APP_HME
}
if [ $handle == "build" ]
then
if [ $env == "prod" ]
then
env='prod'
echo "[exec]build ==> build production"
npm run build
emptyRemoteDist
transferFileToProSever
else
env='dev'
echo "[exec]build ==> build development"
npm run build
emptyRemoteDist
transferFileToTestSever
fi
fi
三. 执行脚本
sh deploy.sh build prod