Search code examples
cachingtravis-ciapt-get

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


I am using travis-ci to test my github repository and I found out that 3 to 10 minutes are spent on downloading deb packages. They are only 127 MB big.

I checked travis-ci/Caching Dependencies and Directories but there is no support for apt-get.

How to do this?


Solution

  • It is possible to achieve this by caching the content in another folder that is accessible for non-root users, and mv all deb under it into /var/cache/apt/archives/. After installation, cp them back to that folder.

    Note:

    I recommand you to make YOUR_DIR_FOR_DEB_PACKAGES somewhere under ~.

    # .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 "$1" ]
        }
      - |
        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.