Skip to content

Commit b974c71

Browse files
ci(rust): Auto-update rust-toolchain.toml
1 parent 7f18c47 commit b974c71

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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: Update toolchain file and create branch
54+
id: rust-update
55+
if: steps.rust-check.outputs.needs_update == 'true'
56+
env:
57+
NEW_VERSION: ${{ steps.rust-check.outputs.new_version }}
58+
run: |
59+
# Define commit title
60+
COMMIT_TITLE="build(rust): Update Rust toolchain to $NEW_VERSION"
61+
62+
# Update rust-toolchain.toml
63+
sed -i 's/channel = "[0-9.]*"/channel = "'$NEW_VERSION'"/' rust-toolchain.toml
64+
65+
# Create branch and commit
66+
BRANCH_NAME="rust-toolchain/update-to-$NEW_VERSION"
67+
git checkout -B "$BRANCH_NAME"
68+
git add rust-toolchain.toml
69+
git commit -m "$COMMIT_TITLE"
70+
git push origin "$BRANCH_NAME" --force
71+
72+
# Set outputs for next step
73+
echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT
74+
echo "commit_title=$COMMIT_TITLE" >> $GITHUB_OUTPUT
75+
76+
- name: Create pull request
77+
if: steps.rust-check.outputs.needs_update == 'true'
78+
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # 8.0.0
79+
with:
80+
script: |
81+
const newVersion = '${{ steps.rust-check.outputs.new_version }}';
82+
const fullVersion = '${{ steps.rust-check.outputs.full_version }}';
83+
const branchName = '${{ steps.rust-update.outputs.branch_name }}';
84+
const commitTitle = '${{ steps.rust-update.outputs.commit_title }}';
85+
86+
// Check if PR already exists
87+
try {
88+
const { data: existingPRs } = await github.rest.pulls.list({
89+
owner: context.repo.owner,
90+
repo: context.repo.repo,
91+
head: `${context.repo.owner}:${branchName}`,
92+
state: 'open'
93+
});
94+
95+
if (existingPRs.length > 0) {
96+
console.log(`PR already exists: #${existingPRs[0].number}`);
97+
return;
98+
}
99+
} catch (error) {
100+
console.log('No existing PR found, creating new one');
101+
}
102+
103+
// Create new PR
104+
const { data: pr } = await github.rest.pulls.create({
105+
owner: context.repo.owner,
106+
repo: context.repo.repo,
107+
title: commitTitle,
108+
head: branchName,
109+
base: '${{ github.ref_name }}',
110+
body: `Update Rust toolchain to ${newVersion} (${fullVersion}).
111+
112+
**Changes:**
113+
- Update \`rust-toolchain.toml\` channel to \`${newVersion}\`
114+
115+
**Release Notes:** https://github.com/rust-lang/rust/releases/tag/${fullVersion}
116+
117+
---
118+
🤖 *This PR was created automatically by the Rust toolchain update bot.*`
119+
});
120+
121+
// Add labels
122+
await github.rest.issues.addLabels({
123+
owner: context.repo.owner,
124+
repo: context.repo.repo,
125+
issue_number: pr.number,
126+
labels: ['github_actions']
127+
});
128+
129+
console.log(`Created PR #${pr.number}: ${pr.title}`);

0 commit comments

Comments
 (0)