Search code examples
cachingrustazure-pipelinesazure-blob-storagerust-cargo

How to effectively cache cargo/Rust projects in an Azure Build Pipeline


I have a set of Azure Build Pipelines that compile rust projects and currently use blob storage to store the .cargo and target folders as a cache.

When compiling locally, once a binary is compiled the first time, subsequent cargo build's don't compile the dependent libraries/crates again, just the local binary, however with my current pipeline system, after downloading the cache and using the correct target folder to build into, the pipeline still downloads and builds crates.

This is my config.toml for the cache and any pipeline builds.

[build]
target-dir = "./target"
dep-info-basedir = "."
incremental = true

It has reduced compilation times in some cases but not nearly as much as I expect. Can I cache more folders to increase speed? Is there some cache identifier that cargo is checking and fouling the cache over?

The pipelines run a custom xtask binary which performs many tasks including running cargo build --release could this be causing issues?


Solution

  • You need to cache target and ~/.cargo/registry as mentioned by Caesar in the comments above.

    The following worked for me (docs):

    - task: Cache@2
      inputs:
        key: '"cargo" | "$(Agent.OS)" | Cargo.lock'
        path: $(Build.SourcesDirectory)\target
      displayName: cache cargo build
    
    - task: Cache@2
      inputs:
        key: '"cargo-registry" | "$(Agent.OS)" | Cargo.lock'
        path: $(UserProfile)\.cargo\registry
      displayName: cache cargo registry