在 GitLab CI 中没有命中 ccache

No hits in GitLab CI for ccache

ccache 在 GitLab CI 中的缓存命中率为零,即使源未更改且缓存持久化也是如此。

此外,每次构建运行时缓存的大小都会增加,这意味着它会被一遍又一遍地重建。

问题是 ccache 检查编译器是否相同的默认方式 - 通过它的时间戳。由于每个 GitLab CI 实例运行一个全新的 Docker 容器,这个时间戳总是不同的,因此总是建立新的缓存。

要解决此问题,请将 CCACHE_COMPILERCHECK 设置为 content 而不是默认的 mtime

来自 ccache 文档:

compiler_check (CCACHE_COMPILERCHECK)
By default, ccache includes the modification time (“mtime”) and size of the compiler in the hash to ensure that results retrieved from the cache are accurate. This setting can be used to select another strategy. Possible values are:
    content
        Hash the content of the compiler binary. This makes ccache very slightly slower compared to the mtime setting, but makes it cope better with compiler upgrades during a build bootstrapping process.
    mtime
        Hash the compiler’s mtime and size, which is fast. This is the default.