我有一个部署设置,可以将next.js存储库推送到我在服务器上已设置的git remote上。
我有一个接收后挂钩设置,然后将其构建到临时文件夹中,然后再将其复制到其永久文件夹并以PM2启动。
# 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.’
这很好用,但是,如果构建失败(例如,出现掉毛错误),则接收后脚本将继续执行,并将失败的构建复制到永久目录,并使用PM2启动应用程序(课程失败,因为构建失败)。
如果webpack构建失败,我希望接收后钩子不再继续,而是发出警告。
我试图处理类似这样的错误:
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
但是这也不起作用。
答案 0 :(得分:0)
通过@torek的注释提示找出了答案
我保留了中止陷阱,并将&& npm run build \
更改为&& npm run build || exit 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 ***
************
'