bash - npm run build 命令后 shell 脚本不继续
问题描述
我编写了一个 shell 脚本,deploy.sh
用于将应用程序部署VueJS
到生产或开发(用于测试和 QA)服务器。在部署应用之前,该脚本会先dist
通过 生成一个文件夹npm run build
,然后将该dist
文件夹复制到各自的生产或开发服务器上。
该脚本需要 4 个必需选项,每个选项都有一个必需参数。
所需的 4 个选项是:
t
- 部署类型“prod”或“dev”i
- 将应用程序部署到的服务器的 IP 地址f
- 部署在/var/www/html
服务器的nginx
文件夹p
.pem
-用于针对生产或开发服务器进行身份验证的文件路径
这就是我的脚本的运行方式./deploy.sh -t dev -i 12.345.67.890 -f my-folder -p ~/.ssh/DevServerCredentials.pem
。
在这个 shell 脚本的一个部分中,我有以下内容来生成dist
文件夹:
#!/bin/bash
# Use this script to deploy app to production or development server
curdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
usage() {
echo ""
echo "Usage: ./deploy.sh -t [ prod | dev ] -i [ server-IP-to-deploy-to ] -f [ folder-in-server-to-deploy-to ] -p [ pem-file-location ]"
echo ""
echo "Example:"
echo " Development Server >> ./deploy.sh -t dev -i 12.345.67.890 -f my-dev-folder -p ~/.ssh/DevServerCredentials.pem"
echo " Production Server >> ./deploy.sh -t prod -i website.domain-name.com -f my-prod-folder -p ~/.ssh/ProdServerCredentials.pem"
echo ""
exit 2
}
set_variable() {
local varname=$1
shift
if [ -z "${!varname}" ]; then
eval "$varname=\"$@\""
else
echo "Error: $varname already set"
usage
fi
}
while getopts 't:i:f:p:?h' c; do
case $c in
t) set_variable type $OPTARG ;;
i) set_variable ip $OPTARG ;;
f) set_variable folder $OPTARG ;;
p) set_variable pem $OPTARG ;;
h) usage ;;
esac
done
# checks for wrong and missing arguments
if [ "$type" != 'prod' ] && [ "$type" != 'dev' ] ; then
echo "Wrong argument given: -t accepts either 'prod' or 'dev'."
usage
exit 1
fi
if [ -z "$type" ]; then
echo "Missing argument: -t argument is missing."
usage
exit 1
fi
if [ -z "$ip" ]; then
echo "Missing argument: -i argument is missing."
usage
exit 1
fi
if [ -z "$folder" ]; then
echo "Missing argument: -f argument is missing."
usage
exit 1
fi
if [ -z "$pem" ]; then
echo "Missing argument: -p argument is missing."
usage
exit 1
fi
# set remote server home directory
if [ "$type" = "prod" ] ; then
homedir=/home/ubuntu
user=ubuntu
elif [ "$type" = "dev" ] ; then
homedir=/home/ec2-user
user=ec2-user
else
echo "type is neither 'prod' nor 'dev'"
fi
# generate dist folder to be copied to server
if [ "$type" == 'prod' ]; then
echo "execute npm run build using environment variables from .env.production.local"
npm run build
fi
if [ "$type" == 'dev' ]; then
echo "execute npm run build-test using environment variables from .env.test.local"
npm run build-test
fi
scp -r -i $pem $curdir/../dist $user@$ip:/$homedir/temp
if [ "$folder" = "/" ] ; then
ssh -t -i $pem $user@$ip "
dir=\$PWD;
mv \$dir/temp/index.html \$dir;
mv \$dir/temp \$dir/dist;
sudo cp -r \$dir/dist /var/www/html
sudo cp -r \$dir/index.html /var/www/html
rm -rf \$dir/*
"
else
ssh -t -i $pem $user@$ip "
dir=\$PWD;
mkdir \$dir/$folder;
cd \$dir/$folder;
mkdir dist
mv \$dir/temp/index.html \$dir/$folder;
mv \$dir/temp/* \$dir/$folder/dist;
rm -rf \$dir/temp;
sudo cp -r \$dir/$folder /var/www/html
rm -rf \$dir/*
"
fi
以下是package.json
文件:
{
.
.
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"build-test": "vue-cli-service build --mode test",
"lint": "vue-cli-service lint"
},
.
.
}
根据传递给t
选项的参数,它运行其各自的npm
命令以生成一个dist
文件夹,该文件夹将相应地部署到生产或开发服务器。
我遇到的问题是,shell 脚本在命令后暂停npm
,随后超时并退出,因此dist
文件夹没有复制到服务器。
dist
将文件夹复制到服务器的 shell 脚本部分出现在npm
命令运行之后。
应该如何编辑我的 shell 脚本,使其在npm
命令完成后继续运行?
根据我的阅读和理解,sh
可以使用npm
脚本运行脚本,这些脚本也接受带有变通方法的参数,尽管npm
本身并不支持此功能。同样通过npm
,我必须将参数值传递给脚本npm
,然后再传递给sh
脚本。这似乎是我的部署sh
脚本中存在错误的地方。
我宁愿通过sh
脚本而不是通过npm
.