如何在travis中缓存apt-get下载的包?

How to cache the downloaded packages of the apt-get in travis?

我正在使用 travis-ci 测试我的 github 存储库,我发现 310 分钟用于下载 deb 包。它们只有 127 MB 大。

我检查了 travis-ci/Caching 依赖项和目录 但不支持 apt-get

如何操作?

可以通过将内容缓存在非 root 用户可以访问的另一个文件夹中,并将其下的 mv 所有 deb 放入 /var/cache/apt/archives/ 来实现。安装后,cp 他们回到那个文件夹。

注:

我建议你在 ~.

下的某处制作 YOUR_DIR_FOR_DEB_PACKAGES
# .travis.yml

sudo: required

cache:
  - directories:
    - $YOUR_DIR_FOR_DEB_PACKAGES # This must be accessible for non-root users

addons:
  apt:
    sources:
      # Whatever source you need

# Download the dependencies if it is not cached
# All the "echo" and "ls" in "before_script" can be remove since they are only used for debugging.
before_script:
  - echo "Print content of $YOUR_DIR_FOR_DEB_PACKAGES"
  - ls $YOUR_DIR_FOR_DEB_PACKAGES
  - echo "Check whether apt-get has no cache"
  - ls /var/cache/apt/archives
  -
  - echo "Start loading cache"
  - |
    exist() {
        [ -e "" ]
    }
  - |
    if exist ~/$YOUR_DIR_FOR_DEB_PACKAGES/*.deb
    then
        sudo mv ~/$YOUR_DIR_FOR_DEB_PACKAGES/*.deb /var/cache/apt/archives/
        ls /var/cache/apt/archives
    fi
  -
  - echo "Start to install software"
  - sudo apt-get update
  - sudo apt-get install -y --no-install-recommends --no-install-suggests $THE_PACKAGES_REQUIRED
  -
  - echo "Start updating the cache"
  - cp /var/cache/apt/archives/*deb ~/$YOUR_DIR_FOR_DEB_PACKAGES/

script:
  - # Do whatever you want here.

在我看来这不是推荐的做法。 根据official documentation

Large files that are quick to install but slow to download do not benefit from caching, as they take as long to download from the cache as from the original source:

  • Debian packages