在推送到我的 Bitbucket 存储库之前,我使用 Browserify 捆绑我的 JS,然后使用 Codeship 测试构建并推送到 Heroku。
我正在使用 Node/Express 为我的应用提供服务,并且在我的 index.jade
中我有一个 <script />
指向 /dist/index.js
.
有几次,我错误地推送了带有损坏的 Browserify 输出的最新代码,即。 /dist/index.js
的内容将是:
console.error('cannot find module XYZ')
我已经将它部署到我的实时应用程序中。呃哦。
我已经进行了一个非常基本的测试,该测试在 Codeship 上运行,我希望将来能避免这种情况:
var exit = function() {
process.exit(1)
}
var success = function() {
process.exit(0)
}
var fs = require('fs')
var index
try {
index = fs.readFileSync(__dirname + '/../public/dist/index.js', 'utf-8')
} catch (e) {
exit()
}
if(!index){
exit()
}
var invalid = index.length < 1000
if(invalid){
return exit()
}
success()
我只是检查文件是否存在,以及文件的内容是否超过 1000 个字符。
不确定是否有对此的具体答案,但是否是确保损坏的 Browserify 输出永远不会被提交/部署的合理方法?
最佳答案
我以前没有用过Codeship,但是我用过其他类似的服务。您还没有描述如何推送 - 我假设您使用的是 git
。
使用 git
,这变得很容易:编写一个 pre-push
Hook ,如果出现问题,它将中止推送。这是我正在从事的项目中的一个示例:
#!/bin/bash
# the protected branches
#
protected_branches='develop master'
# Check if we actually have commits to push
#
commits=`git log @{u}..`
if [ -z "$commits" ]; then
exit 0
fi
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
# is the current branch in the list of protected branchs? if so, then run the
# tests
#
if grep -q "$current_branch" <<< "$protected_branches"; then
# move into the dir containing the tests
#
pushd $(git rev-parse --show-toplevel)/contract >/dev/null
gulp test
RESULT=$?
# back to whatever dir we were in before
#
popd >/dev/null
if [ $RESULT -ne 0 ]; then
echo "-------- Failed Tests"
exit 1
fi
fi
exit 0
这是我在 this blog post 中找到的脚本的修改版本.
基本上,此脚本会检查我是否正在推送 protected 分支之一,如果是,则运行我的测试。如果这些测试失败,则推送将中止。
当然,您可以更改中止推送的条件。例如,编写一些代码来检查并查看您的 browserify
包是否正确,如果不正确则失败。您提到检查包的长度 - 可能类似于 length=$(ls -l | cut -c 30-34)
然后检查长度值(抱歉,我不是真正的bash
大师)。
这种方法的好处是,困惑的代码永远不会离开您的本地机器 - 您在本地运行测试,如果失败,代码不会被推送。这可能比在 Codeship 的服务上运行更快。
关于javascript - 在提交/部署之前检测 Browserify 损坏的构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41059772/