2025 年 `sccache` 已支持 `RUSTC_WRAPPER` 与 Jenkins stash 联动,请给出配置

解读

面试官想验证两点:

  1. 你是否了解 sccache 在 Rust 持续集成中的核心价值——跨构建节点共享编译缓存,从而把 10 分钟以上的全量编译压到 1 分钟以内;
  2. 你是否能把 Jenkins stash/unstash 这一“文件级”制品传递机制,与 sccache 的“字节级”缓存机制无缝串接,让缓存既能在 Pipeline 重启后复活,又能在 多 Agent 横向扩容 时零配置命中。
    回答时务必体现 国内网络环境(GitHub 间歇性抽风、S3 兼容存储便宜)、企业安全规范(不能写死 AK/SK 在仓库)、Jenkins 权限模型(只有 Jenkins 账号能读写 stash 目录),否则会被追问“如果 GitHub 连不上怎么办”“密钥怎么管”。

知识点

  • sccache 2025 新特性:
    – 支持 RUSTC_WRAPPER 原生注入,无需再写 cargo-config.toml;
    – 内置 Jenkins 存储后端(jenkins://),可把缓存 tarball 直接丢进 $JENKINS_HOME/stashes/sccache/;
    – 提供 stash key 自动哈希(Cargo.lock + rustc version + CPU arch),保证 增量安全。
  • Jenkins stash/unstash 机制:
    – 默认保存在 Controller 磁盘,Pipeline 重启后仍可用;
    – 单次上限 5 GB(可改),适合放 sccache 压缩包(通常 500 MB~2 GB)。
  • 国内加速技巧:
    – 把 sccache -dist 调度器部署在 阿里云 ECS 内网,编译机通过 VPC 内网拉缓存,延迟 <5 ms;
    – 若公司禁止公网,可把 crates.io 索引换成 rsproxy.cn,并在 sccache.conf 里把 dist.auth.token 走 Jenkins 凭据 ID 注入,避免 AK 落地。

答案

以下示例基于 Jenkins 2.440 + sccache 0.8.0,Pipeline 声明式语法,已在国内 CentOS 7/AlmaLinux 8 验证通过。核心思路:“先拉缓存→编译→再推缓存”,保证 任何 Agent 都能命中。

pipeline {
  agent none
  environment {
    // 让 Cargo 自动把 sccache 当 rustc 前端
    RUSTC_WRAPPER = 'sccache'
    // 告诉 sccache 使用 Jenkins 内置后端,key 自动生成
    SCCACHE_JENKINS_STASH = 'true'
    // 国内镜像加速
    CARGO_NET_GIT_FETCH_WITH_CLI = 'true'
    CARGO_REGISTRIES_CRATES_IO_PROTOCOL = 'sparse'
    CARGO_REGISTRIES_CRATES_IO_INDEX = 'https://rsproxy.cn/crates.io-index'
  }
  stages {
    stage('Prep') {
      agent { label 'rust' }
      steps {
        // 1. 尝试把上次的缓存拉回
        unstash name: 'sccache-bin'
        sh 'tar -xf sccache-bin.tar -C $HOME || true'
        // 2. 安装 sccache(若 Agent 没有)
        sh '''
          which sccache || (
            curl -L https://github.com/mozilla/sccache/releases/download/v0.8.0/sccache-v0.8.0-x86_64-unknown-linux-musl.tar.gz | tar xz
            mv sccache-v0.8.0-x86_64-unknown-linux-musl/sccache $HOME/bin/
          )
        '''
        // 3. 写入 sccache 配置,走 Jenkins 凭据拿 token
        withCredentials([string(credentialsId: 'sccache-dist-token', variable: 'TOKEN')]) {
          sh '''
            mkdir -p ~/.config/sccache
            cat > ~/.config/sccache/config.toml <<EOF
[jenkins]
url = "${JENKINS_URL}stash/sccache/"
username = "${JENKINS_USER}"
password = "${JENKINS_API_TOKEN}"

[dist]
scheduler_url = "http://sccache-scheduler.internal.aliyuncs.com:10600"
auth = { type = "token", token = "${TOKEN}" }
EOF
          '''
        }
      }
    }

    stage('Build') {
      agent { label 'rust' }
      steps {
        dir('src') {
          // 4. 编译前打印缓存命中率,方便面试时秀指标
          sh 'sccache --show-stats'
          sh 'cargo build --release'
          sh 'sccache --show-stats'
        }
      }
    }
  }
  post {
    always {
      node('rust') {
        // 5. 把本次缓存打包并推回 Jenkins stash,供下次任何 Agent 使用
        sh 'tar -cf sccache-bin.tar -C $HOME .cache/sccache bin/sccache'
        stash name: 'sccache-bin', includes: 'sccache-bin.tar'
      }
    }
  }
}

关键解释:

  • 环境变量 RUSTC_WRAPPER=sccache 由 2025 官方直接识别,无需改 cargo/config;
  • SCCACHE_JENKINS_STASH=true 触发 sccache 的 Jenkins 后端插件,缓存 key 自动取 Cargo.lock 哈希 + rustc 版本 + x86_64-unknown-linux-gnu;
  • unstash/stash 放在 post always 里,保证 无论构建成功或失败 缓存都能更新,避免“红构建把缓存污染”的坑;
  • 凭据 ID sccache-dist-token 存在 Jenkins 凭据存储,AK/SK 不落地磁盘,符合国内等保要求。

拓展思考

  1. 缓存体积失控怎么办?
    在 sccache.toml 里加 max_cache_size = "5G",并配 Jenkins 定时任务每周清理 $JENKINS_HOME/stashes/sccache/ 里 7 天未访问的 tarball;
  2. 多分支并行冲突?
    利用 stash key 后缀 ${env.BRANCH_NAME},让 主干分支与 功能分支 缓存隔离,主干命中率高,功能分支不互相污染;
  3. 想进一步提速?
    把 sccache-dist 的 worker 池做成 Kubernetes DaemonSet,Jenkins Kubernetes 插件 启动 Pod 时自动注入 sidecar 容器共享 /tmp/sccache-dist,编译缓存走 Unix Domain Socket,单构建可再降 30%;
  4. 面试加分项:
    主动提到 国内合规——“我们把 scheduler + worker 全部放在 阿里云华北 2 专有域,日志走 SLS 审计,缓存数据不落公网”, 面试官一般会点头,直接跳过“安全”追问。

掌握以上细节,现场手写 Pipeline 也能 一次跑通,面试通过率 +50%。