Skip to content

Instantly share code, notes, and snippets.

@liuzhen9320
Created June 25, 2025 22:38
Show Gist options
  • Select an option

  • Save liuzhen9320/4261ac5d156ee2877016c8de785fee4f to your computer and use it in GitHub Desktop.

Select an option

Save liuzhen9320/4261ac5d156ee2877016c8de785fee4f to your computer and use it in GitHub Desktop.
Cargo Auto Fix @ Github Actions
# eslint-disable yml/no-empty-mapping-value
name: Cargo Auto Fix
on:
push:
branches: [ "main" ]
workflow_dispatch:
env:
CARGO_TERM_COLOR: always
jobs:
# Job ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
build:
name: job
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
# ─────────────────────────────────────────────────────
- name: Bootstrap ❯❯ actions/checkout@v4
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Bootstrap ❯❯ Setup Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
components: 'rustfmt'
- name: Bootstrap ❯❯ Debug ❯❯ Show version information (Rust, cargo, GCC)
run: |
gcc --version || true
rustup -V
rustup toolchain list
rustup default
cargo -V
rustc -V
- name: Bootstrap ❯❯ Preinstall & Prepare Environment
run: |
echo "PROJECT_NAME=$(sed -n 's/^name = "\(.*\)"/\1/p' Cargo.toml)" >> "$GITHUB_ENV"
echo "PROJECT_VERSION=$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | head -n1)" >> "$GITHUB_ENV"
- name: Bootstrap ❯❯ Restore Rust Cache
uses: Swatinem/rust-cache@v2
- name: Rust ❯❯ Test & Fix
run: |
echo -e "\033[1;34m📘 Starting cargo operations...\033[0m"
echo -e "\n🛠️ Testing project structure..."
cargo run -- --help || true
echo -e "\n🔧 Applying automatic fixes..."
cargo fix --bin "${PROJECT_NAME}" --allow-dirty || true
echo -e "\n✅ \033[1;32mOperation complete\033[0m"
- name: Bash ❯❯ Push & Open pull requests
env:
GH_TOKEN: ${{ github.token }}
run: |
THE_RANDOM=$(( (RANDOM % 1000) + 1 ))
BRANCH_NAME="cargo/fix-${THE_RANDOM}"
echo -e "\033[1;34m✨ Checking out branch...\033[0m"
git checkout -b $BRANCH_NAME
if [[ -n $(git status --porcelain) ]]; then
echo -e "\n📌 Changes detected:"
git diff --stat | sed 's/^/| /'
CHANGES=$(git diff --stat)
echo -e "\033[1;34m✨ Setting up git...\033[0m"
git config user.name "Cargo AutoFix Bot"
git config user.email "[email protected]"
echo -e "\n📦 Committing changes..."
git add .
git commit -m "fix: auto-fix change"
echo -e "\n📡 Pushing branch..."
git push origin $BRANCH_NAME 2>&1
echo -e "\n💖 Pull request creation:"
gh pr create \
--title "[auto-fix] Applies cargo recommendations" \
--body "> Generated by cargo-auto-fix" \
--base ${{ github.event.repository.default_branch }} \
--head $BRANCH_NAME
echo -e "\n✅ \033[1;32mPR created successfully!\033[0m"
else
echo -e "\n✅ \033[1;32mNo changes required\033[0m"
echo -e "ℹ️ Project already up to date"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment