diff --git a/bin/sdk/images/baked/ecr2.sh b/bin/sdk/images/baked/ecr2.sh index 14a6d8f8d..5ff9e7636 100644 --- a/bin/sdk/images/baked/ecr2.sh +++ b/bin/sdk/images/baked/ecr2.sh @@ -18,8 +18,77 @@ function Ecr2::_md5() { return 0 } -function Assets::_packageLockHash() { - Ecr2::_md5 package-lock.json +function Assets::isCacheEnabled() { + case "${SPRYKER_ASSETS_CACHE}" in + 0 | false) return "${FALSE}" ;; + esac + return "${TRUE}" +} + +# Expands assets.cache.paths entries (wildcards allowed) to existing paths; warns about entries matching nothing. +function Assets::_cachePathInputs() { + local IFS=$'\n' + local entry match found + for entry in "${SPRYKER_ASSETS_CACHE_PATHS[@]}"; do + found=0 + for match in ${entry}; do + if [ -e "${match}" ]; then + echo "${match}" + found=1 + fi + done + if [ "${found}" = 0 ]; then + Console::warn "Assets cache path \"${entry}\" matches no file or directory, so it is not watched." >&2 + fi + done + return 0 +} + +# Hash of every input the assets build reads, so a change in any of them produces a new key. +function Assets::_computeCacheKey() { + [ -f package-lock.json ] || return 0 + + # Bump to invalidate every cached builder_assets image after a key format change. + local keyVersion=v2 + + local -a md5Cmd=(md5sum) + command -v md5sum >/dev/null 2>&1 || md5Cmd=(md5 -r) + + local -a inputs=() + local input + for input in package.json package-lock.json composer.lock .npmrc .browserslistrc nx.json project.json tsconfig*.json \ + frontend config/Yves config/install; do + if [ -e "${input}" ]; then + inputs+=("${input}") + fi + done + while IFS= read -r input; do + if [ -n "${input}" ]; then + inputs+=("${input}") + fi + done <<< "$(Assets::_cachePathInputs)" + + { + echo "${keyVersion} ${SPRYKER_ASSETS_MODE} ${SPRYKER_PIPELINE} ${SPRYKER_PLATFORM_IMAGE}" \ + "${SPRYKER_NODE_IMAGE_VERSION} ${SPRYKER_NODE_IMAGE_DISTRO} ${SPRYKER_NPM_VERSION}" + { + find "${inputs[@]}" -type f -not -path '*/node_modules/*' -print0 + if [ -d src ]; then + find src -type f -not -path '*/node_modules/*' \ + \( -path '*/Theme/*' -o -path '*/assets/*' -o -path '*/Presentation/Components/*' \) -print0 + fi + } 2>/dev/null | LC_ALL=C sort -z | xargs -0 ${XARGS_NO_RUN_IF_EMPTY} "${md5Cmd[@]}" + } | "${md5Cmd[@]}" | cut -d' ' -f1 + + return 0 +} + +function Assets::_cacheKey() { + if [ -n "${SPRYKER_ASSETS_CACHE_KEY}" ]; then + echo "${SPRYKER_ASSETS_CACHE_KEY}" + return 0 + fi + Assets::_computeCacheKey } function Images::_composerLockHash() { @@ -54,17 +123,17 @@ function Ecr2::_ecrImageExists() { >/dev/null 2>&1 } -# -- pull pre-built builder_assets from ECR by package-lock.json hash --------- +# -- pull pre-built builder_assets from ECR by assets cache key --------------- function Assets::_pullFromEcr() { local builderAssetsImage; builderAssetsImage=$(Assets::getImageTag) - local lockHash; lockHash=$(Assets::_packageLockHash) - [ -z "${lockHash}" ] && return "${FALSE}" + local cacheKey; cacheKey=$(Assets::_cacheKey) + [ -z "${cacheKey}" ] && return "${FALSE}" - local hashImage; hashImage="$(Ecr2::_ecrBase)/${SPRYKER_PROJECT_NAME}-builder_assets:${lockHash}" + local hashImage; hashImage="$(Ecr2::_ecrBase)/${SPRYKER_PROJECT_NAME}-builder_assets:${cacheKey}" local currentHash="${SPRYKER_BUILD_HASH:-current}" - Console::start "Trying builder_assets:${lockHash}..." + Console::start "Trying builder_assets:${cacheKey}..." if ! docker image inspect "${hashImage}" >/dev/null 2>&1 \ && ! docker pull "${hashImage}" >/dev/null 2>&1; then @@ -95,6 +164,7 @@ EOF # -- override Assets::areBuilt: only consult ECR; skip the local check -------- function Assets::areBuilt() { + Assets::isCacheEnabled || return "${FALSE}" Ecr2::_assertAwsContext || return "${FALSE}" Assets::_pullFromEcr } @@ -132,22 +202,22 @@ COPY --from=${sourceImage} /data/public /data/public EOF } -# -- tag minimal assets image with package-lock.json hash for cache push ------ +# -- tag minimal assets image with assets cache key for cache push ------------ function Ecr2::_tagAssetsCacheImage() { local minimalImage=${1} - local lockHash; lockHash=$(Assets::_packageLockHash) - [ -z "${lockHash}" ] && return "${FALSE}" + local cacheKey; cacheKey=$(Assets::_cacheKey) + [ -z "${cacheKey}" ] && return "${FALSE}" local repository="${SPRYKER_PROJECT_NAME}-builder_assets" - if Ecr2::_ecrImageExists "${repository}" "${lockHash}"; then - Console::verbose "${INFO}Assets cache already exists for ${lockHash} [SKIP]${NC}" + if Ecr2::_ecrImageExists "${repository}" "${cacheKey}"; then + Console::verbose "${INFO}Assets cache already exists for ${cacheKey} [SKIP]${NC}" return "${TRUE}" fi - docker tag "${minimalImage}" "$(Ecr2::_ecrBase)/${repository}:${lockHash}" + docker tag "${minimalImage}" "$(Ecr2::_ecrBase)/${repository}:${cacheKey}" export SPRYKER_PUSH_ASSETS_CACHE="${TRUE}" - Console::verbose "${INFO}Assets cache tagged: ${lockHash} [NEW]${NC}" + Console::verbose "${INFO}Assets cache tagged: ${cacheKey} [NEW]${NC}" } # -- build composer cache image from BuildKit cache mount --------------------- @@ -189,7 +259,14 @@ function Images::buildApplication() { Console::verbose "${INFO}Building application images for AWS ECR${NC}" # Reset state from any previous invocation in the same shell. - unset SPRYKER_COMPOSER_CACHE_IMAGE SPRYKER_PUSH_ASSETS_CACHE SPRYKER_PUSH_COMPOSER_CACHE + unset SPRYKER_COMPOSER_CACHE_IMAGE SPRYKER_PUSH_ASSETS_CACHE SPRYKER_PUSH_COMPOSER_CACHE SPRYKER_ASSETS_CACHE_KEY + + if Assets::isCacheEnabled; then + SPRYKER_ASSETS_CACHE_KEY="$(Assets::_computeCacheKey)" + export SPRYKER_ASSETS_CACHE_KEY + else + Console::log "${INFO}Assets cache is disabled, assets will be rebuilt${NC}" + fi if Ecr2::_assertAwsContext; then Ecr2::_pullComposerCache || true @@ -231,9 +308,11 @@ function Images::tagFrontend() { Ecr2::_assertAwsContext || return "${TRUE}" - local minimalImage="${SPRYKER_DOCKER_PREFIX}_builder_assets_minimal:${SPRYKER_DOCKER_TAG}" - Ecr2::_buildMinimalAssetsImage "${minimalImage}" - Ecr2::_tagAssetsCacheImage "${minimalImage}" + if Assets::isCacheEnabled; then + local minimalImage="${SPRYKER_DOCKER_PREFIX}_builder_assets_minimal:${SPRYKER_DOCKER_TAG}" + Ecr2::_buildMinimalAssetsImage "${minimalImage}" + Ecr2::_tagAssetsCacheImage "${minimalImage}" + fi Ecr2::_buildComposerCacheImage } @@ -254,8 +333,8 @@ function Images::push() { done if [ "${SPRYKER_PUSH_ASSETS_CACHE}" == "${TRUE}" ]; then - local lockHash; lockHash=$(Assets::_packageLockHash) - image="${ecr_base}/${SPRYKER_PROJECT_NAME}-builder_assets:${lockHash}" + local cacheKey; cacheKey=$(Assets::_cacheKey) + image="${ecr_base}/${SPRYKER_PROJECT_NAME}-builder_assets:${cacheKey}" echo "${image}" docker push "${image}" & pids+=($!) fi diff --git a/docs/07-deploy-file/02-deploy.file.reference.v1.md b/docs/07-deploy-file/02-deploy.file.reference.v1.md index 65ccc805c..a2c39790c 100644 --- a/docs/07-deploy-file/02-deploy.file.reference.v1.md +++ b/docs/07-deploy-file/02-deploy.file.reference.v1.md @@ -372,6 +372,21 @@ Defines the setting of *Assets*. * `assets: compression: engine: static:` - defines a comression mode. Allowed values are `only`, `true`, and `false`. * `assets: compression: engine: level:` - defines a compression level. Allowed range is from `1` to `9`. * `assets: compression: engine: types:` - defines additional MIME types to be compressed. +* `assets: cache:` - defines the assets cache used by the `ecr2` image export. The cache is enabled by default: without any `cache` configuration, the export watches the default inputs below and rebuilds assets only when one of them changes; otherwise, the previously built assets are reused. Default inputs: `package.json`, `package-lock.json`, `composer.lock`, `.npmrc`, `.browserslistrc`, `nx.json`, `project.json`, `tsconfig*.json`, `frontend/`, `config/Yves/`, `config/install/`, every `Theme`, `assets` and `Presentation/Components` directory under `src/` (any namespace), the assets mode, the pipeline, the PHP platform image, and the Node.js and npm versions. `node_modules` directories are ignored, and default inputs missing in a project are skipped. `assets: cache: false` is a shorthand for `assets: cache: enabled: false`. +* `assets: cache: enabled:` - defines whether the assets cache is used. Possible values are `true` and `false`. This variable is optional with the default value of `true`. Set to `false` to rebuild assets on every export. The `SPRYKER_ASSETS_CACHE` environment variable overrides it: `0` disables the cache and `1` enables it. +* `assets: cache: paths:` - defines files or directories, relative to the project root, that are watched in addition to the default inputs. Use it for front-end sources outside the default locations, so their changes trigger a rebuild. Wildcards, such as `*.config.js`, are expanded; quote an entry that starts with `*`, because YAML reads a leading `*` as an alias. An entry that matches no file or directory is reported as a warning in the build output and is not watched. + +```yaml +assets: + mode: production + cache: + enabled: true + paths: + - src/Acme/Yves + - frontend-custom + - "*.config.js" +``` + *** ### regions: diff --git a/generator/deploy-file-generator/config/validation.yml b/generator/deploy-file-generator/config/validation.yml index 25fbcc619..fdfcca491 100644 --- a/generator/deploy-file-generator/config/validation.yml +++ b/generator/deploy-file-generator/config/validation.yml @@ -47,6 +47,8 @@ assets.mode: string-type: assets.compression.*.level: integer-type: +assets.cache.paths: + array-type: # Groups groups.*.region: diff --git a/generator/src/templates/deploy.bash.twig b/generator/src/templates/deploy.bash.twig index f6434b333..3802427e9 100644 --- a/generator/src/templates/deploy.bash.twig +++ b/generator/src/templates/deploy.bash.twig @@ -131,6 +131,10 @@ readonly SPRYKER_COMPOSER_AUTOLOAD="{{ composer.autoload | default('') }}" readonly SPRYKER_COMPOSER_VERBOSE="{{ composer.verbose is defined ? composer.verbose : '-vvv' }}" readonly SPRYKER_LOG_DIRECTORY="{{ docker['logs']['path'] | default('/var/log/spryker') }}" readonly SPRYKER_ASSETS_MODE="{{ assets['mode'] | default('development') }}" +{% set assetsCache = assets['cache'] ?? true -%} +{% set assetsCacheEnabled = assetsCache is iterable ? (assetsCache['enabled'] ?? true) : assetsCache -%} +readonly SPRYKER_ASSETS_CACHE=${SPRYKER_ASSETS_CACHE:-{{ (assetsCacheEnabled is same as(false) or assetsCacheEnabled is same as(0) or assetsCacheEnabled is same as('false') or assetsCacheEnabled is same as('0')) ? '0' : '1' }}} +declare -a -r SPRYKER_ASSETS_CACHE_PATHS=({{ (assetsCache is iterable ? (assetsCache['paths'] ?? []) : []) | map(path => "'" ~ path ~ "'") | join(' ') | raw }}) readonly SPRYKER_DB_ENGINE="{{ services['database']['engine'] == 'mysql' ? 'mysql' : 'pgsql' }}" readonly SPRYKER_STORES=({{ regions | map((regionData, region) => "'local REGION=" ~ '"' ~ region ~ '"' ~ "; local STORES=(" ~ (regionData['stores'] | keys | join(' ')) ~ ")'") | join(' ') | raw }}) readonly SPRYKER_DEFAULT_STORE="{{ (regions | first)['stores'] | keys | first }}"