[Guides](https://checklane.sh/guides) · Checked on 28 November 2026

# How do I cache Go and Rust builds in GitHub Actions?

Cache [GOCACHE](https://pkg.go.dev/cmd/go#hdr-Build_and_test_caching) for compiled Go packages and [GOMODCACHE](https://go.dev/ref/mod#module-cache) for downloaded modules. For Rust, keep Cargo’s [target directory](https://doc.rust-lang.org/cargo/reference/build-cache.html) for compiled crates and its [registry](https://doc.rust-lang.org/cargo/guide/cargo-home.html) for downloads. Run the build after a hit: the compiler still checks what it can reuse.

## Our run of Blackbox Exporter v0.27.0 and miniserve v0.32.0 on 28 November 2026

One cold run per runner. Warm times are the median of three runs, with the range in parentheses.

Time in seconds by workload and runner
| Workload | GitHub-hosted (2 vCPU) | Checklane (2 vCPU) |
| --- | --- | --- |
| blackbox\_exporter · cold · whole job | 65 s | 31 s |
| blackbox\_exporter · cold · build step | 42 s | 20 s |
| blackbox\_exporter · warm · whole job | 23 s (20–28) | 12 s (12–13) |
| blackbox\_exporter · warm · build step | 1 s (1–1) | 0 s (0–1) |
| miniserve · cold · whole job | 115 s | 57 s |
| miniserve · cold · build step | 98 s | 46 s |
| miniserve · warm · whole job | 25 s (24–27) | 16 s (14–16) |
| miniserve · warm · build step | 6 s (5–7) | 2 s (2–2) |

Measured on 28 November 2026. Each label reported two CPUs. The Go build linked blackbox\_exporter with CGO disabled. The Rust build compiled miniserve in the dev profile with debug info and incremental compilation off. Times are seconds from GitHub’s job and step timestamps. A 0 s step started and finished within the same timestamp second. Whole jobs include checkout, toolchain setup, restore and save, but not queue time. The build step includes downloads on a miss. These results cover these revisions and settings only.

## Compare a cold build with three warm builds

1.  The workflow checks out two services without modifying them: Prometheus blackbox\_exporter v0.27.0 ([Apache-2.0](https://github.com/prometheus/blackbox_exporter/blob/v0.27.0/LICENSE)) and the miniserve HTTP file server v0.32.0 ([MIT](https://github.com/svenstaro/miniserve/blob/v0.32.0/LICENSE)). It pins the source and toolchain for each.
    
2.  In a private repository with both runner labels available, push to the branch in the trigger. After the first run finishes, [rerun all jobs](https://docs.github.com/en/actions/how-tos/manage-workflow-runs/re-run-workflows-and-jobs) three times, letting each attempt finish. The run ID stays the same. Each service and runner has its own cache key, and a warm job fails if it misses.
    

### Tested on 28 November 2026

Show the workflow YAML

```yaml
name: Compare Go and Rust build caches
on:
  push:
    branches: [batch2-3-services-v1]
permissions:
  contents: read
jobs:
  build:
    name: ${{ matrix.service }} on ${{ matrix.runner }}
    runs-on: ${{ matrix.runner }}
    timeout-minutes: 25
    strategy:
      fail-fast: false
      matrix:
        runner: [ubuntu-latest, checklane-2vcpu]
        service: [blackbox_exporter, miniserve]
        include:
          - service: blackbox_exporter
            repo: prometheus/blackbox_exporter
            ref: 46ea0224f57b708fdff317f8872765a77ef64034
          - service: miniserve
            repo: svenstaro/miniserve
            ref: 757207b76966ee3db3091a3b493714d188e55bd1
    steps:
      - run: nproc && test "$(nproc)" = 2
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          repository: ${{ matrix.repo }}
          ref: ${{ matrix.ref }}
          persist-credentials: false
      - if: matrix.service == 'blackbox_exporter'
        uses: actions/setup-go@d8cbb8045d7f43706b9177cceb36c1f91a6644d5 # v7.1.0
        with:
          go-version: '1.27.2'
          cache: false
      - if: matrix.service == 'miniserve'
        run: rustup toolchain install 1.94.0 --profile minimal
      - name: Restore build cache
        id: cache
        uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
        with:
          path: .build-cache
          key: build-${{ github.run_id }}-${{ matrix.service }}-${{ matrix.runner }}
      - name: Verify cache state
        env:
          HIT: ${{ steps.cache.outputs.cache-hit }}
        run: |
          if [ "$GITHUB_RUN_ATTEMPT" = 1 ]; then
            test "$HIT" != true && test ! -e .build-cache
          else
            test "$HIT" = true && test -d .build-cache
          fi
      - name: Build Go service
        if: matrix.service == 'blackbox_exporter'
        env:
          GOCACHE: ${{ github.workspace }}/.build-cache/go-build
          GOMODCACHE: ${{ github.workspace }}/.build-cache/go-mod
          CGO_ENABLED: '0'
        run: go build -mod=readonly -buildvcs=false -v -o /tmp/blackbox_exporter .
      - name: Build Rust service
        if: matrix.service == 'miniserve'
        env:
          CARGO_HOME: ${{ github.workspace }}/.build-cache/cargo
          CARGO_TARGET_DIR: ${{ github.workspace }}/.build-cache/target
          CARGO_PROFILE_DEV_DEBUG: '0'
          CARGO_INCREMENTAL: '0'
        run: cargo +1.94.0 build --locked --verbose
```

Run on 28 November 2026 on checklane-2vcpu: the warm Go builds listed no packages to compile. Cargo marked 285 crates Fresh on every warm build and recompiled miniserve itself. Both runner labels showed the same pattern.

Related guide: [How do I cache pnpm in GitHub Actions?](https://checklane.sh/guides/github-actions-pnpm-cache)

## Frequently asked questions

### Do I need actions/cache as well as setup-go?

Usually not. setup-go can save both module downloads and build outputs. The example turns off its cache so it can measure a deliberately empty directory on the first attempt.

[GitHub: setup-go caching](https://github.com/actions/setup-go#caching-dependency-files-and-build-outputs)

### Can I keep the Go build cache but always rerun integration tests?

Yes. Use go test -count=1 ./... to disable test-result reuse without discarding compiled packages. That matters when a test reads state from a service such as Postgres.

[Go: test packages](https://pkg.go.dev/cmd/go#hdr-Test_packages)

### Does a warm Rust dev build mean the release build is warm too?

No. Cargo stores dev and release outputs separately because their compiler settings differ. Compare the profile you ship, and include the profile and pinned toolchain in a cache key you use across commits.

[Cargo: build cache](https://doc.rust-lang.org/cargo/reference/build-cache.html)

## Run this job on Checklane

To try your own build on Checklane, change the job’s runner label to [`runs-on: checklane`](https://checklane.sh/docs/runners).

[Connect your repository](https://checklane.sh/docs/moving), then set the job’s runner label:

```
runs-on: checklane
```

---

From https://checklane.sh/guides/github-actions-go-rust-build-cache
