Test PR for Rust toolchain update #3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Rust Toolchain Update Bot | |
| on: | |
| schedule: | |
| # Run weekly on Mondays at 00:00 UTC | |
| - cron: '0 0 * * 1' | |
| pull_request: | |
| jobs: | |
| check-and-update: | |
| name: Check for Rust toolchain updates | |
| runs-on: ubuntu-24.04 | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| steps: | |
| - name: Checkout Repository | |
| uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # 5.0.0 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Configure git | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| - name: Check for Rust toolchain update | |
| id: rust-check | |
| run: | | |
| set -e | |
| # Get latest stable Rust version | |
| curl -s "https://static.rust-lang.org/dist/channel-rust-stable.toml" > channel.toml | |
| LATEST_FULL=$(yq '.pkg.rust.version' channel.toml | sed 's/ (.*//') | |
| LATEST_MINOR=$(echo "$LATEST_FULL" | cut -d. -f1,2) | |
| # Get current version | |
| CURRENT=$(yq '.toolchain.channel' rust-toolchain.toml) | |
| echo "Current: $CURRENT, Latest: $LATEST_MINOR" | |
| if [ "$LATEST_MINOR" != "$CURRENT" ]; then | |
| echo "needs_update=true" >> $GITHUB_OUTPUT | |
| echo "new_version=$LATEST_MINOR" >> $GITHUB_OUTPUT | |
| echo "full_version=$LATEST_FULL" >> $GITHUB_OUTPUT | |
| echo "✅ Update available: $CURRENT → $LATEST_MINOR" | |
| else | |
| echo "needs_update=false" >> $GITHUB_OUTPUT | |
| echo "⭐ Already on latest version: $CURRENT" | |
| fi | |
| - name: Create pull request | |
| if: steps.rust-check.outputs.needs_update == 'true' | |
| uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # 8.0.0 | |
| with: | |
| script: | | |
| const newVersion = '${{ steps.rust-check.outputs.new_version }}'; | |
| const fullVersion = '${{ steps.rust-check.outputs.full_version }}'; | |
| const branchName = `rust-toolchain/update-to-${newVersion}`; | |
| // Update rust-toolchain.toml | |
| const fs = require('fs'); | |
| const content = fs.readFileSync('rust-toolchain.toml', 'utf8'); | |
| const updated = content.replace(/channel = "[\d.]+"/, `channel = "${newVersion}"`); | |
| fs.writeFileSync('rust-toolchain.toml', updated); | |
| // Create branch and commit | |
| await exec.exec('git', ['checkout', '-B', branchName]); | |
| await exec.exec('git', ['add', 'rust-toolchain.toml']); | |
| await exec.exec('git', ['commit', '-m', `build: Update Rust toolchain to ${newVersion}`]); | |
| await exec.exec('git', ['push', 'origin', branchName, '--force']); | |
| // Check if PR already exists | |
| try { | |
| const { data: existingPRs } = await github.rest.pulls.list({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| head: `${context.repo.owner}:${branchName}`, | |
| state: 'open' | |
| }); | |
| if (existingPRs.length > 0) { | |
| console.log(`PR already exists: #${existingPRs[0].number}`); | |
| return; | |
| } | |
| } catch (error) { | |
| console.log('No existing PR found, creating new one'); | |
| } | |
| // Create new PR | |
| const { data: pr } = await github.rest.pulls.create({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| title: `build(rust): Update Rust toolchain to ${newVersion}`, | |
| head: branchName, | |
| base: '${{ github.head_ref }}', | |
| body: `Updates Rust toolchain to ${newVersion} (${fullVersion}). | |
| **Changes:** | |
| - Update \`rust-toolchain.toml\` channel to \`${newVersion}\` | |
| **Release Notes:** https://github.com/rust-lang/rust/releases/tag/${fullVersion} | |
| --- | |
| 🤖 *This PR was created automatically by the Rust toolchain update bot.*` | |
| }); | |
| // Add labels | |
| await github.rest.issues.addLabels({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: pr.number, | |
| labels: ['dependencies', 'rust', 'automation'] | |
| }); | |
| console.log(`Created PR #${pr.number}: ${pr.title}`); |