# Caches and artifacts

> Keep dependency caches between runs, and hand built files from one job to the jobs that need it.
> **Available** in repositories branchd reads pipeline files for. It is switching repositories on one at a time: until yours is, it keeps running its `.forge-ci.yaml` or `.xeonr-ci.yaml`. See [What is available](https://docs.branchd.run/branchd/pipelines#what-is-available).

```yaml title=".branchd/pipelines/ci.yaml"
# .branchd/pipelines/ci.yaml
# A cache keeps Go's module and build caches between runs; artifacts carry
# the built binary from the build job to the jobs that need it.
name: CI
on: [push, pull_request]
jobs:
  build:
    image: golang:1.26
    cache:
      - key: go-mod
        files: [go.sum]
        paths: [.cache/mod]
      - key: go-build-${{ branch }}
        fallback-keys: [go-build-main]
        paths: [.cache/build]
    steps:
      # Steps share one shell, so these exports reach the next step.
      - run: |
          export GOMODCACHE="$PWD/.cache/mod"
          export GOCACHE="$PWD/.cache/build"
      - run: go build -o dist/server ./cmd/server
    artifacts:
      paths: [dist/server]
      when: success
      expire-in: 1 week
  test-binary:
    needs: [build]
    image: debian:stable-slim
    steps:
      - run: ./dist/server --version
  lint:
    # Only the order: this job does not download build's files.
    needs: [{job: build, artifacts: false}]
    image: golangci/golangci-lint:v2.1
    steps:
      - run: golangci-lint run
```

**A cache** is a best-effort copy of some paths, kept between runs and
looked up by key. A job may find it empty, so never depend on it for
correctness.

- `files: [go.sum]` keys the cache on that file's contents, with `key` as the
  prefix.
- `key: go-build-${{ branch }}` gives each branch its own cache;
  `fallback-keys` are tried, in order, when that key has none yet.
- `policy`: `pull-push` (the default) restores and saves, `pull` only
  restores, `push` only saves. `when`: `success` (the default) or `always`
  saves after a failure too.
- `cache:` takes one cache or a list.

**Artifacts** are files a job keeps after it ends. Every job that needs it
finds them in its own working directory, at the same paths.

- `artifacts: [dist]` keeps `dist`. The long form is
  `{paths, when, expire-in}`: `when` is `success` (the default), `failure` or
  `always`, and `expire-in` how long they are kept (`1 week`, `30 days`).
- `needs: [{job: build, artifacts: false}]` waits for `build` without
  downloading its artifacts.
- No artifact path may be, hold or match `.branchd/run/` (`.`, `*`, `**` and
  `.branchd` are refused): branchd keeps a run's own files there.

Source: https://docs.branchd.run/branchd/pipelines/caches-and-artifacts
