GitHub Action 定期更新工具链版本?

标签 github continuous-integration github-actions

我有一个 GitHub 项目 pins Rust toolchain versions用于 CI 中。到目前为止,我一直在定期手动更新这些版本。

从脚本编写的角度来看,这是一项死记硬背的任务,自动化起来很简单。就像这样,它实际上就是这么简单:

# After updating Cargo.toml
VERSION=$(cargo metadata --format-version 1 |\
    jq -r ".packages[] | select(.name == \"zerocopy\").metadata.ci.pinned-stable)

TRYBUILD=overwrite cargo +$VERSION test --all
git commit -am '[CI] Update pinned stable toolchain version'

但是,有没有办法可以配置 GitHub 操作来定期运行此脚本并自动提交 PR?当然,我仍然会手动审核 PR。

最佳答案

我终于弄清楚了。这是 GitHub Actions 配置文件:

name: Roll pinned toolchain versions
on:
  schedule:
    - cron: '29 15 * * *'

# TODO: Once we confirm this works, try changing this to `read-all` to see if
# the job still works.
permissions: write-all

jobs:
  roll:
    name: Roll pinned toolchain versions
    runs-on: ubuntu-latest
    steps:
      - name: "Checkout code"
        uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
        with:
          ref: main
      - name: "Calculate target version"
        # Use yesterday's date (`-d '-1 day'`) so we're sure the nightly for that
        # date has actually been published yet. This allows us to not worry
        # about what time of day this job runs.
        run: echo "ZC_TARGET_NIGHTLY=nightly-$(date -d '-1 day' +%Y-%m-%d)" >> $GITHUB_ENV
      - name: Install Rust with ${{ env.ZC_TARGET_NIGHTLY }} toolchain
        uses: dtolnay/rust-toolchain@00b49be78f40fba4e87296b2ead62868750bdd83 # stable
        with:
            toolchain: ${{ env.ZC_TARGET_NIGHTLY }}
      - name: "Update files"
        run: |
          set -eox pipefail
          # Confirm that `Cargo.toml` lists the pinned nightly version in the
          # expected format. This is a prerequisite for the subsequent `sed`
          # command. If this fails, the preceding `set -eo pipefail` will cause
          # the script to exit with an error.
          REGEX='^pinned-nightly = "[a-z0-9-]*"$'
          grep "$REGEX" Cargo.toml >/dev/null
          sed -i -e "s/$REGEX/pinned-nightly = \"$ZC_TARGET_NIGHTLY\"/" Cargo.toml
          # Confirm that the update didn't bork `Cargo.toml`.
          grep "$REGEX" Cargo.toml >/dev/null
          # Update `.stderr` files as needed for the new nightly version.
          TRYBUILD=overwrite ./cargo.sh +nightly test --all-features --package zerocopy
          TRYBUILD=overwrite ./cargo.sh +nightly test --all-features --package zerocopy-derive
      
      - name: "Submit PR"
        uses: peter-evans/create-pull-request@153407881ec5c347639a548ade7d8ad1d6740e38 # v5.0.2
        with:
          commit-message: "[ci] Roll pinned nightly toolchain"
          branch: roll-pinned-toolchain-to-${{ env.ZC_TARGET_NIGHTLY }}

关于GitHub Action 定期更新工具链版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77062450/

相关文章:

GitHub wiki 和 Markdown

macos - Git 推送用户名错误的文件到github

css - WebCompiler 在 CI 构建中跳过 scss 文件

svn - Hudson Subversion 发布管理器插件

unit-testing - 使用 Visual Studio Team Services VSTS 在 Visual Studio 测试中包含或排除测试程序集的正确语法是什么

GitHub:术语:创建拉取请求与打开拉取请求

github - 从 github 拉取请求进行 npm 安装

amazon-web-services - 通过 Github 操作部署 AWS ECS 任务定义时出现授权错误

google-cloud-platform - 使用上传云存储操作替换文件夹

github-actions - 如何在 GitHub Actions config yaml 中集中(DRY)字符串?