在 committing/deploying 之前检测 Browserify 损坏的构建

Detect Browserify broken build before committing/deploying

在推送到我的 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 输出永远不会被破坏的合理方法 committed/deployed?

我以前没有用过Codeship,但是我用过其他类似的服务。你没有描述你是如何推动的 - 我假设你正在使用 git.

有了 git,这就变得简单了:写一个 pre-push 挂钩,如果出现问题,它将中止推送。这是我正在从事的一个项目的示例:

#!/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,.*/\(.*\),,')

# 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 中找到的脚本的修改版本。

基本上,此脚本会检查我是否正在推送受保护的分支之一,如果是,运行 就是我的测试。如果这些测试失败,则推送中止。

当然,您可以更改中止推送的条件。例如,编写一些代码来检查并查看您的 browserify 包是否正确,如果不正确则失败。您提到检查包的长度 - 可能类似于 length=$(ls -l | cut -c 30-34) 然后检查长度值(抱歉,我不是真正的 bash 专家)。

这种方法的好处是混乱的代码永远不会离开您的本地机器 - 您 运行 在本地测试,如果失败,代码不会被推送。这可能比 运行 加入 Codeship 的服务要快。