Cache settings

更新时间:
复制 MD 格式

Configure pipeline cache directories to avoid repeated dependency downloads and speed up builds, and clean up cached files on private build clusters.

Each job node in a Flow pipeline creates a new build environment that is destroyed after the job completes. To avoid downloading dependencies every time, you can use Flow caching to persist and share dependency files across runs.

For example, running npm install in a Node.js build job creates ./node_modules in the project and stores the cache in /root/.npm. Persisting /root/.npm speeds up dependency downloads in later runs.

Configure cache directories

In the pipeline editor, select to configure the Cache Directory.

Important

Flow caching speeds up dependency downloads across multiple runs of the same job but does not support sharing caches between different jobs in a single pipeline run.

  • By default, Flow caches the following directories. You can enable, disable, or modify these default cache directories as needed.

    Management tool

    Cache directory

    Maven

    /root/.m2

    Gradle

    /root/.gradle/caches

    NPM

    /root/.npm

    Yarn

    /root/.yarn

    go mod

    /go/pkg/mod

    Other cache

    /root/.cache

  • You can also add custom cache directories. Custom directories must follow these rules:

    • Do not specify the root directory (/), /root, /root/workspace, or any subdirectories of these locations.

      To cache a file or directory within /root/workspace, copy it to another location under /root and set that location as the cache directory.

      The following figures show an example:

      image.png

      高的 (39)

      image.png

    • The path cannot contain ... For example, /root/abc/.. is invalid.

    • The path must be a valid directory path. For example, /root/%\&dfaf is invalid.

Using cache in build jobs

After you configure the cache directories, add parameters to your build commands to use the cache during pipeline runs.

  • For npm builds, use the following command to install dependencies, prioritizing packages from the local cache:

    npm install --prefer-offline --no-audit
  • For Yarn builds, use the following command to install dependencies, prioritizing packages from the local cache:

    yarn install --prefer-offline

Caching for a private build cluster

A private build cluster offers two caching methods:

  • Local cache: Dependency files are stored on the private build cluster. Using the local cache avoids network-related build slowdowns.

  • Alibaba Cloud DevOps-managed cache: Each build downloads cached files from the cloud. This works well for a cluster with multiple build agents, because they can reuse cached dependencies.

高的 (40)

Cache cleanup on build agents

Over time, a private build cluster accumulates cache files. To prevent disk space issues that can cause pipeline failures, clean up the cache periodically.

The Alibaba Cloud DevOps cache cleanup tool removes the following items from build agents:

  • Base environment cleanup:

    • Stopped containers.

    • Dangling images.

    • Intermediate images generated during custom environment builds.

  • Cleanup of the /root/yunxiao/*/runner/ directory:

    • Build job run logs that are older than 3 days.

    • Build job step logs that are older than 3 days.

    • Build job cache directories that have not been updated in 15 days.

Run the following command to use the cleanup tool:

docker run -v /var/run/docker.sock:/var/run/docker.sock -v /root/yunxiao:/root/yunxiao registry.cn-beijing.aliyuncs.com/build-steps/cache-cleaner:0.0.3-20230914183240
You can add this command to a cronjob to schedule regular cache cleanup.