Skip to content

Commit 0d6dabc

Browse files
authored
Invalidate the cache when the stack changes (#21)
Previously the buildpack did not invalidate the cache when the stack changed, which causes the errors seen in #19. This will be particularly an issue when Heroku-24 is GAed and Heroku-20 deprecated, since there will be a number of apps changing stack for the first time after switching to this buildpack. Now, the stack version is stored in the cache and if it differs from the current stack version (or the version file is missing since the cache is from an older version of this buildpack), the cache will be purged. This implementation is an improved version of what's implemented for the generic APT buildpack (I'll be backporting those improvements over to that buildpack shortly): https://github.com/heroku/heroku-buildpack-apt/blob/4eb4b35d35d0178e5cef73d6998a26ba2c9bbf17/bin/compile#L42-L86 Longer term we should probably re-evaluate whether we need the cache at all, since from my testing locally it offered very little benefit, and if anything causes a number of issues (particularly when several APT-using buildpacks are set on an app at the same time, given the shared Debian archives directory and install strategy). Fixes #19.
1 parent 31fd2c2 commit 0d6dabc

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

bin/install-chrome-dependencies

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,36 @@ case "${STACK}" in
5151
error "STACK must be 'heroku-20', 'heroku-22' or 'heroku-24', not '${STACK}'."
5252
esac
5353

54-
if [ ! -f $CACHE_DIR/PURGED_CACHE_V1 ]; then
55-
echo "Purging cache" | indent
56-
rm -rf $CACHE_DIR/apt
57-
rm -rf $CACHE_DIR/archives
58-
rm -rf $CACHE_DIR/lists
59-
touch $CACHE_DIR/PURGED_CACHE_V1
54+
# We must invalidate the cache if the stack changes, since the cached indexes and
55+
# package archives will not be compatible with a different Ubuntu version.
56+
# This file really should be inside "${CACHE_DIR}/apt" not "${CACHE_DIR}/.apt", however,
57+
# the generic APT buildpack has used the wrong directory name for its version file for some
58+
# time, so we have to match that, given this buildpack and the APT buildpack share all the
59+
# other cached APT directories.
60+
STACK_VERSION_FILE="${CACHE_DIR}/.apt/STACK"
61+
62+
if [[ -d "${CACHE_DIR}/apt" ]]; then
63+
if [[ -f "${STACK_VERSION_FILE}" ]]; then
64+
CACHED_STACK=$(cat "${STACK_VERSION_FILE}")
65+
else
66+
CACHED_STACK=
67+
fi
68+
69+
if [[ "${CACHED_STACK}" == "${STACK}" ]]; then
70+
echo "Reusing APT cache" | indent
71+
elif [[ -z "${CACHED_STACK}" ]]; then
72+
# Older versions of the buildpack won't have written the version file.
73+
# (Plus any other broken APT-related buildpacks that don't invalidate on cache change.)
74+
echo "Clearing APT cache since it's missing the stack version metadata file." | indent
75+
rm -rf "${CACHE_DIR}/apt"
76+
else
77+
echo "Clearing APT cache since stack changed from ${CACHED_STACK} to ${STACK}" | indent
78+
rm -rf "${CACHE_DIR}/apt"
79+
fi
6080
fi
6181

62-
# This is where the original buildpack installed Chrome itself,
63-
# but Chrome for Testing is installed from direct downloads.
64-
#
65-
# echo "Installing Google Chrome from the $channel channel." | indent
66-
# PACKAGES="$PACKAGES https://dl.google.com/linux/direct/google-chrome-${channel}_current_amd64.deb"
82+
mkdir -p "${CACHE_DIR}/.apt"
83+
echo "${STACK}" > "${STACK_VERSION_FILE}"
6784

6885
APT_CACHE_DIR="$CACHE_DIR/apt/cache"
6986
APT_STATE_DIR="$CACHE_DIR/apt/state"
@@ -73,7 +90,7 @@ mkdir -p "$APT_STATE_DIR/lists/partial"
7390

7491
APT_OPTIONS="-o debug::nolocking=true -o dir::cache=$APT_CACHE_DIR -o dir::state=$APT_STATE_DIR"
7592

76-
echo "Updating apt caches" | indent
93+
echo "Updating APT package index" | indent
7794
apt-get $APT_OPTIONS update | indent
7895

7996
for PACKAGE in $PACKAGES; do

0 commit comments

Comments
 (0)