Node.js 如果webpack生成失败,则中止post接收挂钩
我有一个部署设置,可以将next.js repo推送到我在服务器上设置的git remote 我有一个post-receive-hook设置,它将在一个临时文件夹中构建应用程序,然后将其复制到永久文件夹并使用PM2启动Node.js 如果webpack生成失败,则中止post接收挂钩,node.js,bash,git,webpack,deployment,Node.js,Bash,Git,Webpack,Deployment,我有一个部署设置,可以将next.js repo推送到我在服务器上设置的git remote 我有一个post-receive-hook设置,它将在一个临时文件夹中构建应用程序,然后将其复制到永久文件夹并使用PM2启动 # post-receive #!/bin/sh # The production directory TARGET="/var/node-apps/my-app" # A temporary directory for deployment TEMP="/var/node
# post-receive
#!/bin/sh
# The production directory
TARGET="/var/node-apps/my-app"
# A temporary directory for deployment
TEMP="/var/node-apps/tmp/my-app"
# The Git repo
REPO="/var/git/my-app.git"
# The Git branch
BRANCH="master"
# Deploy the content to the temporary directory
echo ‘post-receive: copy to tmp’
mkdir -p $TEMP
git --work-tree=$TEMP --git-dir=$REPO checkout $BRANCH -f
echo ‘post-receive: checkout branch’
cd $TEMP
echo ‘post-receive: npm install…’
npm install \
&& echo ‘post-receive: building…’ \
&& npm run build \
&& echo ‘post-receive: → done.’
# Replace the production directory
# with the temporary directory
cd /
rm -rf $TARGET
mv $TEMP $TARGET
cd $TARGET
(pm2 delete 'my-app' || true)
pm2 start npm --name 'my-app' -- run start-production -i max
echo ‘post-receive: app started successfully with pm2.’
pm2 save
echo ‘post-receive: pm2 saved.’
这很好,但是如果构建失败(例如出现linting错误),post-receive脚本将继续执行,并将失败的构建复制到永久目录,然后使用PM2启动应用程序(当然,这会失败,因为构建失败)
如果webpack构建失败,我希望post-receive钩子不会继续,而是回显一个警告
我曾尝试过这样处理错误:
abort()
{
echo >&2 '
***************
*** ABORTED ***
***************
'
echo "An error occurred. Exiting..." >&2
exit 1
}
trap 'abort' 0
trap 'abort' 1
set -e
[rest of script here]
trap : 0
trap : 1
但这也不起作用。多亏了@torek的评论提示,才明白了这一点 我保留了中止陷阱,并将
和&npm运行构建\
更改为和&npm运行构建| |退出1 \
我不确定陷阱是否严格必要,因为如果npm run build
失败,脚本将退出
# post-receive
#!/bin/sh
abort()
{
echo >&2 '
***************
*** ABORTED ***
***************
'
echo "An error occurred. Exiting..." >&2
exit 1
}
trap 'abort' 0
trap 'abort' 1
set -e
echo ‘post-receive: Triggered.’
# The production directory
TARGET="/var/node-apps/myapp"
# A temporary directory for deployment
TEMP="/var/node-apps/tmp/myapp"
# The Git repo
REPO="/var/git/myapp.git"
# The Git branch
BRANCH="master"
# Deploy the content to the temporary directory
echo ‘post-receive: copy to tmp’
mkdir -p $TEMP
git --work-tree=$TEMP --git-dir=$REPO checkout $BRANCH -f
echo ‘post-receive: checkout branch’
cd $TEMP
# Do stuffs, like npm install…
echo ‘post-receive: npm install…’
npm install \
&& echo ‘post-receive: building…’ \
&& npm run build || exit 1 \
&& echo ‘post-receive: → done.’
# Replace the production directory
# with the temporary directory
cd /
rm -rf $TARGET
mv $TEMP $TARGET
cd $TARGET
(pm2 delete 'myapp' || true)
pm2 start npm --name 'myapp' -- run start-production -i max
echo ‘post-receive: app started successfully with pm2.’
pm2 save
echo ‘post-receive: pm2 saved.’
# Done!
trap : 0
trap : 1
echo >&2 '
************
*** DONE ***
************
'
通常,
trap
在接收到信号时执行代码。(陷阱0发生在退出时,与退出的原因无关;bash(而不是普通sh)有两个额外的陷阱。)使用if
判断命令是否成功:if npm。。。;然后(成功了);否则(它失败了);fi
。