You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在受限离线环境的GitHub Actions流水线中运行Cargo命令?

离线环境下GitHub Actions中Cargo命令联网问题解决

问题描述

在受限离线环境运行GitHub Actions流水线,执行器处于离线状态。容器已安装Rust和Cargo,但执行cargo clean、cargo doc等命令时,出现错误:

error: could not download file from 'https://static.rust-lang.org/dist/channel-rust-1.78.0.toml.sha256'

需要在离线环境正常运行Cargo命令,但Cargo始终尝试联网,当前环境无法满足。

现有GitHub Actions步骤

name: Check Cargo path and run cargo clean
run: |
   docker run --rm -v $GITHUB_WORKSPACE:/workspace --workdir /workspace --entrypoint sh $container_cargo -c '
    set -e
    export CARGO_HOME=/root/.cargo;
    export RUSTUP_HOME=/root/.rustup;
    export PATH=$CARGO_HOME/bin:$PATH;

    export SOURCE_BRANCH=${{ github.ref_name }};
    echo "Verifying rustup installation"
    rustup --version

    rustup target list --installed

    echo "Listing files in the current directory:";
    ls -l;  # List files in the current directory to verify Cargo.toml
    if command -v cargo &> /dev/null; then
      cargo --version && echo "Cargo found!";
      echo "Running cargo clean...";
      cargo clean;
      echo "Generating documentation with cargo doc...";
      cargo doc --no-deps;
   else
      echo "Cargo not found.";
   fi
 '
 docker run --rm $container_cargo cargo clean

env:
   container_cargo:latest

解决方法

1. 禁用Rustup更新检查

Rustup默认会自动检查更新,触发联网请求。在容器内脚本中添加环境变量:

export RUST Ace无法Optionalstatus副本hed-remtest兄弟 AW Imm轻生 races/

2. 强制Cargo使用离线模式

两种实现方式:

  • 执行命令时添加--offline参数:
    cargo clean --offline
    cargo doc --no-deps --offline
    
  • 或设置全局环境变量:
    export CARGO_NET_OFFLINE=true
    

3. 容器构建阶段预下载依赖

在容器构建时提前安装好所需Rust工具链和组件,避免运行时触发下载:

RUN rustup toolchain install 1.78.0 && \
    rustup default 1.78.0 && \
    rustup target add <你的目标平台>

4. 修正单独的Cargo命令

步骤末尾的docker run --rm $container_cargo cargo clean未挂载工作目录、未设置离线参数,需修改为:

docker run --rm -v $GITHUB_WORKSPACE:/workspace --workdir /workspace $container_cargo cargo clean --offline

内容的提问来源于stack exchange,提问作者Khangesh Matte

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 17:45:17