Skip to content

Commit 2484d23

Browse files
ci(rust): Auto-update rust-toolchain.toml
1 parent bcdc20b commit 2484d23

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
name: Rust Toolchain Update Bot
2+
3+
on:
4+
schedule:
5+
# Run weekly on Mondays at 00:00 UTC
6+
- cron: '0 0 * * 1'
7+
8+
jobs:
9+
check-and-update:
10+
name: Check for Rust toolchain updates
11+
runs-on: ubuntu-24.04
12+
13+
permissions:
14+
contents: write
15+
pull-requests: write
16+
17+
steps:
18+
- name: Checkout Repository
19+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # 5.0.0
20+
with:
21+
token: ${{ secrets.GITHUB_TOKEN }}
22+
23+
- name: Configure git
24+
run: |
25+
git config user.name "github-actions[bot]"
26+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
27+
28+
- name: Check for Rust toolchain update
29+
id: rust-check
30+
run: |
31+
set -e
32+
33+
# Get latest stable Rust version
34+
curl -s "https://static.rust-lang.org/dist/channel-rust-stable.toml" > channel.toml
35+
LATEST_FULL=$(yq '.pkg.rust.version' channel.toml | sed 's/ (.*//')
36+
LATEST_MINOR=$(echo "$LATEST_FULL" | cut -d. -f1,2)
37+
38+
# Get current version
39+
CURRENT=$(yq '.toolchain.channel' rust-toolchain.toml)
40+
41+
echo "Current: $CURRENT, Latest: $LATEST_MINOR"
42+
43+
if [ "$LATEST_MINOR" != "$CURRENT" ]; then
44+
echo "needs_update=true" >> $GITHUB_OUTPUT
45+
echo "new_version=$LATEST_MINOR" >> $GITHUB_OUTPUT
46+
echo "full_version=$LATEST_FULL" >> $GITHUB_OUTPUT
47+
echo "✅ Update available: $CURRENT → $LATEST_MINOR"
48+
else
49+
echo "needs_update=false" >> $GITHUB_OUTPUT
50+
echo "⭐ Already on latest version: $CURRENT"
51+
fi
52+
53+
- name: Create pull request
54+
if: steps.rust-check.outputs.needs_update == 'true'
55+
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # 8.0.0
56+
with:
57+
script: |
58+
const newVersion = '${{ steps.rust-check.outputs.new_version }}';
59+
const fullVersion = '${{ steps.rust-check.outputs.full_version }}';
60+
const branchName = `rust-toolchain/update-to-${newVersion}`;
61+
62+
// Update rust-toolchain.toml
63+
const fs = require('fs');
64+
const content = fs.readFileSync('rust-toolchain.toml', 'utf8');
65+
const updated = content.replace(/channel = "[\d.]+"/, `channel = "${newVersion}"`);
66+
fs.writeFileSync('rust-toolchain.toml', updated);
67+
68+
// Create branch and commit
69+
await exec.exec('git', ['checkout', '-B', branchName]);
70+
await exec.exec('git', ['add', 'rust-toolchain.toml']);
71+
await exec.exec('git', ['commit', '-m', `build: Update Rust toolchain to ${newVersion}`]);
72+
await exec.exec('git', ['push', 'origin', branchName, '--force']);
73+
74+
// Check if PR already exists
75+
try {
76+
const { data: existingPRs } = await github.rest.pulls.list({
77+
owner: context.repo.owner,
78+
repo: context.repo.repo,
79+
head: `${context.repo.owner}:${branchName}`,
80+
state: 'open'
81+
});
82+
83+
if (existingPRs.length > 0) {
84+
console.log(`PR already exists: #${existingPRs[0].number}`);
85+
return;
86+
}
87+
} catch (error) {
88+
console.log('No existing PR found, creating new one');
89+
}
90+
91+
// Create new PR
92+
const { data: pr } = await github.rest.pulls.create({
93+
owner: context.repo.owner,
94+
repo: context.repo.repo,
95+
title: `build(rust): Update Rust toolchain to ${newVersion}`,
96+
head: branchName,
97+
base: '${{ github.ref_name }}',
98+
body: `Updates Rust toolchain to ${newVersion} (${fullVersion}).
99+
100+
**Changes:**
101+
- Update \`rust-toolchain.toml\` channel to \`${newVersion}\`
102+
103+
**Release Notes:** https://github.com/rust-lang/rust/releases/tag/${fullVersion}
104+
105+
---
106+
🤖 *This PR was created automatically by the Rust toolchain update bot.*`
107+
});
108+
109+
// Add labels
110+
await github.rest.issues.addLabels({
111+
owner: context.repo.owner,
112+
repo: context.repo.repo,
113+
issue_number: pr.number,
114+
labels: ['github_actions']
115+
});
116+
117+
console.log(`Created PR #${pr.number}: ${pr.title}`);

0 commit comments

Comments
 (0)