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

如何用GitHub Action定期更新Rust工具链版本并自动提交PR?

实现自动更新Rust工具链并提交PR的GitHub Action方案

当然可以通过GitHub Actions实现定期自动更新Rust工具链版本、运行测试并提交PR的流程,以下是具体实现步骤:

1. 完善更新脚本

补充脚本逻辑,添加获取最新稳定版、更新Cargo.toml的功能,保存为scripts/update-toolchain.sh(路径可自行调整):

#!/bin/bash
set -euo pipefail

# 获取最新Rust稳定版版本号
LATEST_STABLE=$(rustup update stable --print nightly 2>&1 | grep -oP 'stable updated to \K[^ ]+' || rustup show stable | grep -oP 'stable-x86_64-unknown-linux-gnu \K[^ ]+')

# 更新Cargo.toml中pinned-stable字段的值
sed -i "s/pinned-stable = \".*\"/pinned-stable = \"$LATEST_STABLE\"/" Cargo.toml

# 提取更新后的版本(用于后续测试和提交信息)
VERSION=$(cargo metadata --format-version 1 | jq -r '.packages[] | select(.name == "zerocopy").metadata.ci.pinned-stable')

# 运行全量测试并覆盖trybuild预期结果
TRYBUILD=overwrite cargo +$VERSION test --all

# 配置Git身份(用于提交)
git config --global user.name "GitHub Actions Bot"
git config --global user.email "actions@github.com"

# 提交更改(包含Cargo.toml和trybuild生成的预期文件)
git add Cargo.toml
git add tests/**/*.stderr
git commit -m "[CI] Update pinned stable toolchain to $VERSION"

2. 创建GitHub Action工作流

在仓库的.github/workflows目录下新建update-rust-toolchain.yml文件,内容如下:

name: Update Rust Stable Toolchain
on:
  # 每周一UTC凌晨1点自动运行,可根据需求调整cron表达式
  schedule:
    - cron: '0 1 * * 1'
  # 支持手动触发,方便测试
  workflow_dispatch:

jobs:
  update-toolchain:
    runs-on: ubuntu-latest
    steps:
      - name: 拉取代码
        uses: actions/checkout@v4
        with:
          fetch-depth: 0 # 需要完整Git历史以提交更改

      - name: 安装Rustup
        run: |
          curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
          source $HOME/.cargo/env

      - name: 安装依赖工具
        run: sudo apt-get update && sudo apt-get install -y jq

      - name: 执行更新脚本
        run: bash scripts/update-toolchain.sh

      - name: 创建Pull Request
        uses: peter-evans/create-pull-request@v5
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: "[CI] Update pinned stable toolchain"
          title: "[CI] Update pinned stable Rust toolchain to latest version"
          body: |
            此PR将固定的Rust稳定工具链版本更新至最新稳定版:
            - 已通过`TRYBUILD=overwrite`运行全量测试,更新了测试预期输出文件
            - 等待手动审核
          branch: update-rust-stable-toolchain

3. 关键注意事项

  • GITHUB_TOKEN默认拥有仓库的PR创建权限,无需额外配置
  • 脚本中的sed命令适配Ubuntu环境(工作流使用ubuntu-latest runner),若更换runner需调整语法
  • 可修改cron表达式调整自动运行频率(比如0 0 * * *表示每天UTC凌晨0点运行)
  • 若trybuild生成的预期文件路径不同,需对应调整git add的路径

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 15:41:12