Unverified Commit 1f8c988b authored by Jeremy Morrell's avatar Jeremy Morrell Committed by GitHub

Save previous run data (#600)

Automatically make the metadata from the previous run available by copying it at the start of the build. This will be used to provide better failure messages.
parent 4fb01f30
......@@ -134,6 +134,10 @@ install_bins() {
bd_set "npm-version-request" "$npm_engine"
bd_set "yarn-version-request" "$yarn_engine"
bd_set "node-version-request" "$node_engine"
bd_set "npm-version-request" "$npm_engine"
bd_set "yarn-version-request" "$yarn_engine"
if [ -n "$iojs_engine" ]; then
echo "engines.iojs (package.json): $iojs_engine (iojs)"
else
......
......@@ -2,10 +2,18 @@
# variable shared by this whole module
BUILD_DATA_FILE=""
PREVIOUS_BUILD_DATA_FILE=""
bd_create() {
local cache_dir="$1"
BUILD_DATA_FILE="$cache_dir/build-data/node"
BUILD_DATA_FILE="$cache_dir/build-data/nodejs"
PREVIOUS_BUILD_DATA_FILE="$cache_dir/build-data/nodejs-prev"
# if the file already exists because it's from the last build, save it
if [[ -f "$BUILD_DATA_FILE" ]]; then
cp "$BUILD_DATA_FILE" "$PREVIOUS_BUILD_DATA_FILE"
fi
kv_create "$BUILD_DATA_FILE"
# make sure this doesnt grow over time
kv_clear "$BUILD_DATA_FILE"
......@@ -29,6 +37,26 @@ bd_time() {
kv_set "$BUILD_DATA_FILE" "$key" "$time"
}
# similar to mtime from stdlib
bd_time() {
local key="$1"
local start="$2"
local end="${3:-$(nowms)}"
local time
time="$(echo "$start" "$end" | awk '{ printf "%.3f", ($2 - $1)/1000 }')"
kv_set "$BUILD_DATA_FILE" "$1" "$time"
}
# Retrieve a value from a previous build if it exists
# This is useful to give the user context about what changed if the
# build has failed. Ex:
# - changed stacks
# - deployed with a new major version of Node
# - etc
bd_prev_get() {
kv_get "$PREVIOUS_BUILD_DATA_FILE" "$1"
}
log_build_data() {
# print all values on one line in logfmt format
# https://brandur.org/logfmt
......
......@@ -80,6 +80,11 @@ log_build_scripts() {
bd_set "heroku-prebuild-script" "$heroku_prebuild"
bd_set "heroku-postbuild-script" "$heroku_prebuild"
bd_set "build-script" "$build"
bd_set "postinstall-script" "$postinstall"
bd_set "heroku-prebuild-script" "$heroku_prebuild"
bd_set "heroku-postbuild-script" "$heroku_prebuild"
if [ -n "$build" ]; then
mcount "scripts.build"
......
......@@ -180,6 +180,44 @@ testBuildData() {
assertEquals "30.002" "$(bd_get time)"
}
testBuildDataPreviousBuild() {
local cache_dir=$(mktemp -d)
# the first time, there will be no previous build file
bd_create "$cache_dir"
assertContains "nodejs" "$BUILD_DATA_FILE"
assertContains "nodejs-prev" "$PREVIOUS_BUILD_DATA_FILE"
assertFileExists "$BUILD_DATA_FILE"
# set a value in the build data file
bd_set "test" "foo"
assertFileContains "test=foo" "$BUILD_DATA_FILE"
assertFileDoesNotExist "$PREVIOUS_BUILD_DATA_FILE"
assertEquals "$(bd_get test)" "foo"
assertEquals "$(bd_prev_get test)" ""
# the second time this is called (cache restored)
# there will be a previous build file
bd_create "$cache_dir"
assertFileExists "$BUILD_DATA_FILE"
assertFileExists "$PREVIOUS_BUILD_DATA_FILE"
# the data stored in the previous build should now be in the second file
assertFileNotContains "test=foo" "$BUILD_DATA_FILE"
assertFileContains "test=foo" "$PREVIOUS_BUILD_DATA_FILE"
assertEquals "$(bd_get test)" ""
assertEquals "$(bd_prev_get test)" "foo"
bd_set "test" "bar"
# doing it once more does not result in an error
bd_create "$cache_dir"
assertFileExists "$BUILD_DATA_FILE"
assertFileExists "$PREVIOUS_BUILD_DATA_FILE"
assertEquals "$(bd_prev_get test)" "bar"
assertEquals "$(bd_get test)" ""
}
testWebConcurrencyProfileScript() {
# this was set when we sourced the WEB_CONCURRENCY.sh file
unset WEB_MEMORY
......@@ -232,5 +270,8 @@ source "$(pwd)"/lib/kvstore.sh
source "$(pwd)"/lib/build-data.sh
source "$(pwd)"/profile/WEB_CONCURRENCY.sh
# testing utils
source "$(pwd)"/test/utils
# import the testing framework
source "$(pwd)"/test/shunit2
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment