Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching yarn and pnpm dependencies in Docker #43329

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 162 additions & 2 deletions Dockerfile.ci
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,137 @@ COPY <<"EOF" /entrypoint_exec.sh
exec /bin/bash "${@}"
EOF


# The content below is automatically copied from scripts/docker/install_yarn_dependencies_from_branch_tip.sh
COPY <<"EOF" /install_yarn_dependencies_from_branch_tip.sh
#!/usr/bin/env bash

. "$( dirname "${BASH_SOURCE[0]}" )/common.sh"

: "${AIRFLOW_REPO:?Should be set}"
: "${AIRFLOW_BRANCH:?Should be set}"

function install_yarn_dependencies_from_branch_tip() {
echo
echo "${COLOR_BLUE}Installing Yarn dependencies from ${AIRFLOW_BRANCH}. It is used to cache dependencies${COLOR_RESET}"
echo
local TEMP_AIRFLOW_DIR
TEMP_AIRFLOW_DIR=$(mktemp -d)

# Download the source code from the specified branch
set -e
set -x
curl -fsSL "https://github.com/${AIRFLOW_REPO}/archive/${AIRFLOW_BRANCH}.tar.gz" | \
tar xz -C "${TEMP_AIRFLOW_DIR}" --strip 1

# Install Yarn dependencies
cd "${TEMP_AIRFLOW_DIR}/airflow/www"
yarn install --frozen-lockfile
set +x
set +e
echo "${COLOR_BLUE}Yarn dependencies installed successfully${COLOR_RESET}"

# Copy Yarn packages to the .yarn-cache directory
echo
echo "${COLOR_BLUE}Copying Yarn packages to the ${YARN_CACHE_DIR} directory${COLOR_RESET}"
echo
set -e
set -x
cp -r ./node_modules $YARN_CACHE_DIR/
echo "${COLOR_BLUE}Yarn packages copied successfully${COLOR_RESET}"
set +x

# Clean up
rm -rf "${TEMP_AIRFLOW_DIR}"
set +e
}

common::get_colors

install_yarn_dependencies_from_branch_tip
EOF

# The content below is automatically copied from scripts/docker/install_pnpm_dependencies_from_branch_tip.sh
COPY <<"EOF" /install_pnpm_dependencies_from_branch_tip.sh
#!/usr/bin/env bash

. "$( dirname "${BASH_SOURCE[0]}" )/common.sh"

: "${AIRFLOW_REPO:?Should be set}"
: "${AIRFLOW_BRANCH:?Should be set}"

function install_pnpm_dependencies_from_branch_tip() {
echo
echo "${COLOR_BLUE}Installing pnpm dependencies from ${AIRFLOW_BRANCH}. It is used to cache dependencies${COLOR_RESET}"
echo
local TEMP_AIRFLOW_DIR
TEMP_AIRFLOW_DIR=$(mktemp -d)

# Download the source code from the specified branch
set -e
set -x
curl -fsSL "https://github.com/${AIRFLOW_REPO}/archive/${AIRFLOW_BRANCH}.tar.gz" | \
tar xz -C "${TEMP_AIRFLOW_DIR}" --strip 1

# Install pnpm dependencies
cd "${TEMP_AIRFLOW_DIR}/airflow/ui"
pnpm install --frozen-lockfile --config.confirmModulesPurge=false
set +x
set +e
echo "${COLOR_BLUE}pnpm dependencies installed successfully${COLOR_RESET}"

# Copy pnpm packages to the .pnpm-cache directory
echo
echo "${COLOR_BLUE}Copying pnpm packages to the ${PNPM_CACHE_DIR} directory${COLOR_RESET}"
echo
set -e
set -x
cp -r ./node_modules $PNPM_CACHE_DIR/
echo "${COLOR_BLUE}pnpm packages copied successfully${COLOR_RESET}"
set +x

# Clean up
rm -rf "${TEMP_AIRFLOW_DIR}"
set +e
}

common::get_colors

install_pnpm_dependencies_from_branch_tip
EOF

# The content below is automatically copied from scripts/docker/install_npm_pnpm_yarn.sh
COPY <<"EOF" /install_npm_pnpm_yarn.sh
#!/usr/bin/env bash

. "$( dirname "${BASH_SOURCE[0]}" )/common.sh"

function install_npm_pnpm_yarn() {
echo
echo "${COLOR_BLUE}Installing npm, pnpm and yarn${COLOR_RESET}"
echo
set -e
set -x
# Install npm
apt-get update
apt-get install -y npm
# Install pnpm
npm install -g pnpm
# Install yarn
npm install -g yarn
echo "${COLOR_BLUE}npm, pnpm and yarn installed successfully${COLOR_RESET}"
set +x
set +e
}

common::get_colors

install_npm_pnpm_yarn
EOF

##############################################################################################
# This is the build image
##############################################################################################
FROM ${PYTHON_BASE_IMAGE} as main

# Nolog bash flag is currently ignored - but you can replace it with other flags (for example
Expand Down Expand Up @@ -1378,8 +1509,7 @@ RUN echo "Airflow version: ${AIRFLOW_VERSION}"

# Copy all scripts required for installation - changing any of those should lead to
# rebuilding from here
COPY --from=scripts install_packaging_tools.sh install_airflow_dependencies_from_branch_tip.sh \
common.sh /scripts/docker/
COPY --from=scripts install_packaging_tools.sh install_airflow_dependencies_from_branch_tip.sh common.sh /scripts/docker/

# We are first creating a venv where all python packages and .so binaries needed by those are
# installed.
Expand All @@ -1397,6 +1527,36 @@ RUN bash /scripts/docker/install_packaging_tools.sh; \
bash /scripts/docker/install_airflow_dependencies_from_branch_tip.sh; \
fi

#
# yarn and pnpm installation and caching
#
# Install npm, pnpm and yarn
COPY --from=scripts install_npm_pnpm_yarn.sh /scripts/docker/
RUN bash /scripts/docker/install_npm_pnpm_yarn.sh

# yarn cache
ARG AIRFLOW_PRE_CACHED_YARN_PACKAGES="true"
ENV AIRFLOW_PRE_CACHED_YARN_PACKAGES=${AIRFLOW_PRE_CACHED_YARN_PACKAGES}
COPY --from=scripts install_yarn_dependencies_from_branch_tip.sh /scripts/docker/

ENV YARN_CACHE_DIR="${AIRFLOW_SOURCES}/.yarn-cache"
# We are installing Yarn dependencies here to make sure they are cached in the layer
RUN mkdir -p "${YARN_CACHE_DIR}" && \
if [[ ${AIRFLOW_PRE_CACHED_YARN_PACKAGES} == "true" ]]; then \
bash /scripts/docker/install_yarn_dependencies_from_branch_tip.sh; \
fi

# pnpm cache
ARG AIRFLOW_PRE_CACHED_PNPM_PACKAGES="true"
ENV AIRFLOW_PRE_CACHED_PNPM_PACKAGES=${AIRFLOW_PRE_CACHED_PNPM_PACKAGES}
COPY --from=scripts install_pnpm_dependencies_from_branch_tip.sh /scripts/docker/

ENV PNPM_CACHE_DIR="${AIRFLOW_SOURCES}/.pnpm-cache"
# We are installing pnpm dependencies here to make sure they are cached in the layer
RUN mkdir -p "${PNPM_CACHE_DIR}" && \
if [[ ${AIRFLOW_PRE_CACHED_PNPM_PACKAGES} == "true" ]]; then \
bash /scripts/docker/install_pnpm_dependencies_from_branch_tip.sh; \
fi

# Here we fix the versions so all subsequent commands will use the versions
# from the sources
Expand Down
2 changes: 1 addition & 1 deletion dev/breeze/doc/ci/02_images.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ can be used for CI images:
| `PIP_PROGRESS_BAR` | `on` | Progress bar for PIP installation |


Here are some examples of how CI images can built manually. CI is always
Here are some examples of how CI images can be built manually. CI is always
built from local sources.

This builds the CI image in version 3.9 with default extras ("all").
Expand Down
3 changes: 3 additions & 0 deletions dev/breeze/src/airflow_breeze/global_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,9 @@ def get_airflow_extras():
"scripts/docker/install_airflow_dependencies_from_branch_tip.sh",
"scripts/docker/install_from_docker_context_files.sh",
"scripts/docker/install_mysql.sh",
"scripts/docker/install_npm_pnpm_yarn.sh",
"scripts/docker/install_pnpm_dependencies_from_branch_tip.sh",
"scripts/docker/install_yarn_dependencies_from_branch_tip.sh",
]

CURRENT_KUBERNETES_VERSIONS = ALLOWED_KUBERNETES_VERSIONS
Expand Down
13 changes: 9 additions & 4 deletions scripts/ci/pre_commit/compile_ui_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def get_directory_hash(directory: Path, skip_path_regexp: str | None = None) ->
ui_directory = AIRFLOW_SOURCES_PATH / "airflow" / "ui"
node_modules_directory = ui_directory / "node_modules"
dist_directory = ui_directory / "dist"
pnpm_node_modules_cache_directory = AIRFLOW_SOURCES_PATH / ".pnpm-cache" / "node_modules"
UI_HASH_FILE.parent.mkdir(exist_ok=True, parents=True)
if node_modules_directory.exists() and dist_directory.exists():
old_hash = UI_HASH_FILE.read_text() if UI_HASH_FILE.exists() else ""
Expand All @@ -68,10 +69,14 @@ def get_directory_hash(directory: Path, skip_path_regexp: str | None = None) ->
shutil.rmtree(dist_directory, ignore_errors=True)
env = os.environ.copy()
env["FORCE_COLOR"] = "true"
subprocess.check_call(
["pnpm", "install", "--frozen-lockfile", "--config.confirmModulesPurge=false"],
cwd=os.fspath(ui_directory),
)
if os.getenv("AIRFLOW_PRE_CACHED_PNPM_PACKAGES", "false") == "true":
# Copy pnpm-cache to node_modules from pnpm-cache
shutil.copytree(pnpm_node_modules_cache_directory, node_modules_directory, dirs_exist_ok=True)
else:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small tthing here. I think we should ALWAYS run the install step below. This covers case where the .lock file changed in the meantime and we just need to update it based on pre-cached node_modules

subprocess.check_call(
["pnpm", "install", "--frozen-lockfile", "--config.confirmModulesPurge=false"],
cwd=os.fspath(ui_directory),
)
subprocess.check_call(["pnpm", "run", "build"], cwd=os.fspath(ui_directory), env=env)
new_hash = get_directory_hash(ui_directory, skip_path_regexp=r".*node_modules.*")
UI_HASH_FILE.write_text(new_hash)
7 changes: 6 additions & 1 deletion scripts/ci/pre_commit/compile_www_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def get_directory_hash(directory: Path, skip_path_regexp: str | None = None) ->
www_directory = AIRFLOW_SOURCES_PATH / "airflow" / "www"
node_modules_directory = www_directory / "node_modules"
dist_directory = www_directory / "static" / "dist"
yarn_node_modules_cache_directory = AIRFLOW_SOURCES_PATH / ".yarn-cache" / "node_modules"
WWW_HASH_FILE.parent.mkdir(exist_ok=True, parents=True)
if node_modules_directory.exists() and dist_directory.exists():
old_hash = WWW_HASH_FILE.read_text() if WWW_HASH_FILE.exists() else ""
Expand All @@ -68,7 +69,11 @@ def get_directory_hash(directory: Path, skip_path_regexp: str | None = None) ->
shutil.rmtree(dist_directory, ignore_errors=True)
env = os.environ.copy()
env["FORCE_COLOR"] = "true"
subprocess.check_call(["yarn", "install", "--frozen-lockfile"], cwd=os.fspath(www_directory))
if os.getenv("AIRFLOW_PRE_CACHED_YARN_PACKAGES", "false") == "true":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

# Copy yarn-cache to node_modules from yarn-cache
shutil.copytree(yarn_node_modules_cache_directory, node_modules_directory, dirs_exist_ok=True)
else:
subprocess.check_call(["yarn", "install", "--frozen-lockfile"], cwd=os.fspath(www_directory))
subprocess.check_call(["yarn", "run", "build"], cwd=os.fspath(www_directory), env=env)
new_hash = get_directory_hash(www_directory, skip_path_regexp=r".*node_modules.*")
WWW_HASH_FILE.write_text(new_hash)
44 changes: 44 additions & 0 deletions scripts/docker/install_npm_pnpm_yarn.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# shellcheck shell=bash disable=SC2086

# shellcheck source=scripts/docker/common.sh
. "$( dirname "${BASH_SOURCE[0]}" )/common.sh"

# Install npm, pnpm and yarn function
function install_npm_pnpm_yarn() {
echo
echo "${COLOR_BLUE}Installing npm, pnpm and yarn${COLOR_RESET}"
echo
set -e
set -x
# Install npm
apt-get update
apt-get install -y npm
# Install pnpm
npm install -g pnpm
# Install yarn
npm install -g yarn
echo "${COLOR_BLUE}npm, pnpm and yarn installed successfully${COLOR_RESET}"
set +x
set +e
}

common::get_colors

install_npm_pnpm_yarn
70 changes: 70 additions & 0 deletions scripts/docker/install_pnpm_dependencies_from_branch_tip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# shellcheck shell=bash disable=SC2086

# Installs pnpm dependencies from $AIRFLOW_BRANCH tip. This is pure optimization. It is done because we do not want
# to reinstall all dependencies from scratch when package.json changes. Problem with Docker caching is that
# when a file is changed, when added to docker context, it invalidates the cache and it causes Docker
# build to reinstall all dependencies from scratch. This can take a loooooot of time. Therefore we install
# the dependencies first from main (and uninstall airflow right after) so that we can start installing
# deps from those pre-installed dependencies. It saves few minutes of build time when package.json changes.
#
# shellcheck source=scripts/docker/common.sh
. "$( dirname "${BASH_SOURCE[0]}" )/common.sh"

: "${AIRFLOW_REPO:?Should be set}"
: "${AIRFLOW_BRANCH:?Should be set}"

function install_pnpm_dependencies_from_branch_tip() {
echo
echo "${COLOR_BLUE}Installing pnpm dependencies from ${AIRFLOW_BRANCH}. It is used to cache dependencies${COLOR_RESET}"
echo
local TEMP_AIRFLOW_DIR
TEMP_AIRFLOW_DIR=$(mktemp -d)

# Download the source code from the specified branch
set -e
set -x
curl -fsSL "https://github.com/${AIRFLOW_REPO}/archive/${AIRFLOW_BRANCH}.tar.gz" | \
tar xz -C "${TEMP_AIRFLOW_DIR}" --strip 1

# Install pnpm dependencies
cd "${TEMP_AIRFLOW_DIR}/airflow/ui"
pnpm install --frozen-lockfile --config.confirmModulesPurge=false
set +x
set +e
echo "${COLOR_BLUE}pnpm dependencies installed successfully${COLOR_RESET}"

# Copy pnpm packages to the .pnpm-cache directory
echo
echo "${COLOR_BLUE}Copying pnpm packages to the ${PNPM_CACHE_DIR} directory${COLOR_RESET}"
echo
set -e
set -x
cp -r ./node_modules $PNPM_CACHE_DIR/
echo "${COLOR_BLUE}pnpm packages copied successfully${COLOR_RESET}"
set +x

# Clean up
rm -rf "${TEMP_AIRFLOW_DIR}"
set +e
}

common::get_colors

install_pnpm_dependencies_from_branch_tip
Loading