Skip to content

Commit

Permalink
re-use CSS compilation cached assets in pre-merge builds
Browse files Browse the repository at this point in the history
refs DE-1379

[build-registry-path=jenkins/canvas-lms/de-1379-3]
[change-merged]

Change-Id: I1e33e24ee61afa114739cbd4acd9eddb163e1f76
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/301709
Tested-by: Service Cloud Jenkins <[email protected]>
QA-Review: Aaron Ogata <[email protected]>
Product-Review: Aaron Ogata <[email protected]>
Reviewed-by: Andrea Cirulli <[email protected]>
Build-Review: Andrea Cirulli <[email protected]>
  • Loading branch information
aogata-inst committed Sep 26, 2022
1 parent 1211038 commit b286d9d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
7 changes: 5 additions & 2 deletions Dockerfile.jenkins.webpack-runner
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
FROM local/webpack-builder

RUN --mount=type=bind,from=local/cache-helper,source=/tmp/dst,target=/cache-helper \
RUN \
--mount=type=bind,from=local/cache-helper,source=/tmp/dst,target=/cache-helper \
--mount=type=bind,from=local/webpack-assets-previous,target=/mount/webpack-assets-previous \
tar --no-same-owner -xf /cache-helper/webpack-runner-dependencies.tar -C ${APP_HOME} && \
tar --no-same-owner -xf /cache-helper/webpack-runner.tar -C ${APP_HOME}
tar --no-same-owner -xf /cache-helper/webpack-runner.tar -C ${APP_HOME} && \
{ { cd /mount/webpack-assets-previous/usr/src/app && cp --parents -rf public/dist/brandable_css ${APP_HOME}; } || true; }

ARG JS_BUILD_NO_UGLIFY=0
ARG RAILS_LOAD_ALL_LOCALES=0
Expand Down
19 changes: 18 additions & 1 deletion build/new-jenkins/docker-build-helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ function add_log {
echo "$1" >> tmp/docker-build.log
}

function compute_hash {
echo "$@" | md5sum | cut -d' ' -f1
}

function compute_tags {
local tags=$1; shift
local cachePrefix=$1; shift
local cacheId=$(echo "$@" | md5sum | cut -d' ' -f1)
local cacheId=$(compute_hash $@)

compute_tags_from_hash $tags $cachePrefix $cacheId
}
Expand Down Expand Up @@ -42,6 +46,19 @@ function has_remote_tags {
return 0
}

function image_label_eq {
local imageName=$1; shift
local labelName=$1; shift
local expectedValue=$1; shift
local actualValue=$(docker inspect $imageName --format "{{ .Config.Labels.$labelName }}")

if [[ "$actualValue" != "$expectedValue" ]]; then
return 1
fi

return 0
}

function pull_first_tag {
local -n selectedTag=$1; shift
local loadTags=$@
Expand Down
27 changes: 26 additions & 1 deletion build/new-jenkins/docker-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ WORKSPACE=${WORKSPACE:-$(pwd)}
# $WEBPACK_BUILDER_TAG: additional tag for the webpack-builder image
# - set to patchset unique ID for builds to reference without knowing about the hash ID

export CACHE_VERSION="2022-09-23.1"
export CACHE_VERSION="2022-09-26.1"
export DOCKER_BUILDKIT=1

source ./build/new-jenkins/docker-build-helpers.sh
Expand Down Expand Up @@ -101,6 +101,16 @@ WEBPACK_ASSETS_PARTS=(
$WEBPACK_ASSETS_DOCKERFILE_MD5
)

# If any of these SHAs change - we don't want to use the previously cached webpack assets to prevent the
# cache from using stale dependencies.
WEBPACK_ASSETS_CACHE_ID_PARTS=(
"${WEBPACK_BUILDER_PARTS[@]}"
$WEBPACK_RUNNER_DEPENDENCIES_MD5
$WEBPACK_RUNNER_DOCKERFILE_MD5
$WEBPACK_ASSETS_DOCKERFILE_MD5
)
WEBPACK_ASSETS_CACHE_ID=$(compute_hash ${WEBPACK_ASSETS_CACHE_ID_PARTS[@]})

declare -A BASE_RUNNER_TAGS; compute_tags "BASE_RUNNER_TAGS" $BASE_RUNNER_PREFIX ${BASE_RUNNER_PARTS[@]}
declare -A RUBY_RUNNER_TAGS; compute_tags "RUBY_RUNNER_TAGS" $RUBY_RUNNER_PREFIX ${RUBY_RUNNER_PARTS[@]}
declare -A YARN_RUNNER_TAGS; compute_tags "YARN_RUNNER_TAGS" $YARN_RUNNER_PREFIX ${YARN_RUNNER_PARTS[@]}
Expand Down Expand Up @@ -172,6 +182,8 @@ if [ -z "${WEBPACK_ASSETS_SELECTED_TAG}" ]; then
WEBPACK_BUILDER_SELECTED_TAG=${WEBPACK_BUILDER_TAGS[SAVE_TAG]}

add_log "built ${WEBPACK_BUILDER_SELECTED_TAG}"

tag_many starlord.inscloudgate.net/jenkins/core:focal local/webpack-assets-previous
else
RUBY_RUNNER_SELECTED_TAG=$(docker inspect $WEBPACK_BUILDER_SELECTED_TAG --format '{{ .Config.Labels.RUBY_RUNNER_SELECTED_TAG }}')
YARN_RUNNER_SELECTED_TAG=$(docker inspect $WEBPACK_BUILDER_SELECTED_TAG --format '{{ .Config.Labels.YARN_RUNNER_SELECTED_TAG }}')
Expand All @@ -183,6 +195,18 @@ if [ -z "${WEBPACK_ASSETS_SELECTED_TAG}" ]; then

./build/new-jenkins/docker-with-flakey-network-protection.sh pull $RUBY_RUNNER_SELECTED_TAG
tag_many $RUBY_RUNNER_SELECTED_TAG local/ruby-runner ${RUBY_RUNNER_TAGS[SAVE_TAG]}

(
./build/new-jenkins/docker-with-flakey-network-protection.sh pull $WEBPACK_ASSETS_FUZZY_TAG

if ! image_label_eq $WEBPACK_ASSETS_FUZZY_TAG "WEBPACK_ASSETS_CACHE_ID" $WEBPACK_ASSETS_CACHE_ID; then
exit 1
fi

tag_many $WEBPACK_ASSETS_FUZZY_TAG local/webpack-assets-previous
) || (
tag_many starlord.inscloudgate.net/jenkins/core:focal local/webpack-assets-previous
)
fi

tag_many $WEBPACK_BUILDER_SELECTED_TAG local/webpack-builder ${WEBPACK_BUILDER_TAGS[SAVE_TAG]} ${WEBPACK_BUILDER_TAGS[UNIQUE_TAG]-}
Expand All @@ -194,6 +218,7 @@ if [ -z "${WEBPACK_ASSETS_SELECTED_TAG}" ]; then

docker build \
--label "RUBY_RUNNER_SELECTED_TAG=$RUBY_RUNNER_SELECTED_TAG" \
--label "WEBPACK_ASSETS_CACHE_ID=$WEBPACK_ASSETS_CACHE_ID" \
--label "WEBPACK_BUILDER_SELECTED_TAG=$WEBPACK_BUILDER_SELECTED_TAG" \
--label "YARN_RUNNER_SELECTED_TAG=$YARN_RUNNER_SELECTED_TAG" \
--tag "${WEBPACK_ASSETS_TAGS[SAVE_TAG]}" \
Expand Down
2 changes: 2 additions & 0 deletions build/new-jenkins/library/vars/buildDockerImageStage.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def premergeCacheImage() {
def patchsetImage(asyncStepsStr = '') {
credentials.withStarlordCredentials {
def cacheScope = configuration.isChangeMerged() ? env.IMAGE_CACHE_MERGE_SCOPE : env.IMAGE_CACHE_BUILD_SCOPE
def webpackAssetsFuzzyTag = configuration.isChangeMerged() ? "" : "${env.WEBPACK_ASSETS_PREFIX}:${getFuzzyTagSuffix()}"

slackSendCacheBuild {
withEnv([
Expand All @@ -184,6 +185,7 @@ def patchsetImage(asyncStepsStr = '') {
"RAILS_LOAD_ALL_LOCALES=${getRailsLoadAllLocales()}",
"RUBY_RUNNER_PREFIX=${env.RUBY_RUNNER_PREFIX}",
"WEBPACK_BUILDER_PREFIX=${env.WEBPACK_BUILDER_PREFIX}",
"WEBPACK_ASSETS_FUZZY_TAG=${webpackAssetsFuzzyTag}",
"WEBPACK_ASSETS_PREFIX=${env.WEBPACK_ASSETS_PREFIX}",
"YARN_RUNNER_PREFIX=${env.YARN_RUNNER_PREFIX}",
]) {
Expand Down

0 comments on commit b286d9d

Please sign in to comment.