Cache source code fetched via CMake FetchContent in GitHub Actions

I’ve got a CMake-based project in which I heavily rely on FetchContent to retrive multiple libraries (source code). Downloading all the libraries may take time which I want to save when compiling on a CI runner (GitHub Actions).

I saw there’s a cache action, how I can use that to cache the fetched source code?

As a bonus, It might be helpful to cache the compiled code of these fetched libraries as well. It that possible somehow?

@galah92,

To cache dependencies to speed up workflows, generally you need to do like as below:

  1. At the first time run, download and install the dependencies via CMake FetchContent to the runner machine.

  2. Determine the correct installation directory of the downloaded dependencies.

  3. Use the cache action to save all the dependencies from the installation directory and specify the keys.

  4. In the next runs, check if the cache exists with the specified keys:

    • if can find cache, restore the dependencies from the cache.
    • if the can’t find cache, do steps 1 to 3.

A simple demo of the workflow:

steps:
  . . .

  - name: Cache dependencies
    id: cache
    uses: actions/cache@v2
    with:
      path: path/to/dependencies
      key: key-for-cache
      restore-keys: |
        list-of-alternative-keys

  - name: Install dependencies
    if: steps.cache.outputs.cache-hit != 'true'
    run: |
      command lines to download and install 
      dependencies via CMake FetchContent

  . . .

To view more details, you can see “Caching dependencies to speed up workflows”.