Files
miner/.github/workflows/release-proposal.yml
T
2025-09-23 15:57:03 +08:00

186 lines
5.8 KiB
YAML

---
name: Release Proposal
env:
CARGO_TERM_COLOR: always
on:
workflow_dispatch:
inputs:
target_branch:
description: 'Target branch for the PR (default: main)'
required: false
type: string
default: 'main'
version_type:
description: 'Type of version bump'
required: true
default: 'patch'
type: choice
options:
- patch
- minor
- major
- custom
custom_version:
description: 'Custom version string (e.g., 1.2.3). Only used if version_type is "custom".'
required: false
is_draft:
description: 'Is this a draft release?'
required: true
type: boolean
default: false
jobs:
calculate-next-version:
name: 🧮 Calculate Next Version
runs-on: ubuntu-latest
outputs:
new_version: ${{ steps.versioner.outputs.new_version }}
new_tag: ${{ steps.versioner.outputs.new_tag }}
commit_sha_short: ${{ steps.vars.outputs.commit_sha_short }}
source_branch: ${{ steps.vars.outputs.source_branch }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true
- name: Get current branch and commit SHA
id: vars
run: |
echo "commit_sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
echo "source_branch=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_OUTPUT
- name: Get current version from Cargo.toml
id: current_version
run: |
current=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
echo "current=$current" >> $GITHUB_OUTPUT
echo "Current version: $current"
- name: Calculate new version
id: versioner
env:
CURRENT_VERSION: ${{ steps.current_version.outputs.current }}
VERSION_TYPE: ${{ github.event.inputs.version_type }}
CUSTOM_VERSION: ${{ github.event.inputs.custom_version }}
run: |
if [[ "$VERSION_TYPE" == "custom" ]]; then
if [[ -z "$CUSTOM_VERSION" ]]; then
echo "Error: Custom version is selected but no custom_version string provided."
exit 1
fi
new_version="$CUSTOM_VERSION"
else
# Parse current version
IFS='.' read -ra VERSION_PARTS <<< "$CURRENT_VERSION"
major=${VERSION_PARTS[0]}
minor=${VERSION_PARTS[1]}
patch=${VERSION_PARTS[2]}
# Calculate new version
case $VERSION_TYPE in
major)
major=$((major + 1))
minor=0
patch=0
;;
minor)
minor=$((minor + 1))
patch=0
;;
patch)
patch=$((patch + 1))
;;
esac
new_version="$major.$minor.$patch"
fi
new_tag="v$new_version"
echo "new_version=$new_version" >> $GITHUB_OUTPUT
echo "new_tag=$new_tag" >> $GITHUB_OUTPUT
echo "New version: $new_version"
echo "New tag: $new_tag"
update-cargo-toml:
name: 📝 Update version files
needs: calculate-next-version
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup rust
uses: actions-rust-lang/setup-rust-toolchain@v1
- name: Install cargo-edit
run: cargo install cargo-edit --locked
- name: Create version bump branch and PR
env:
NEW_VERSION: ${{ needs.calculate-next-version.outputs.new_version }}
NEW_TAG: ${{ needs.calculate-next-version.outputs.new_tag }}
GITHUB_TOKEN: ${{ secrets.ADMIN_PAT }}
SOURCE_BRANCH: ${{ needs.calculate-next-version.outputs.source_branch }}
TARGET_BRANCH: ${{ github.event.inputs.target_branch }}
run: |
set -ex
branch_name="release/${NEW_TAG}"
# Create new branch from source branch
git checkout "$SOURCE_BRANCH"
git checkout -b "$branch_name"
# Update version in Cargo.toml
echo "Updating Cargo.toml to version: $NEW_VERSION"
cargo set-version "$NEW_VERSION"
# Commit changes
git config user.name "${{ github.actor }}"
git config user.email "${{ github.actor }}@users.noreply.github.com"
git add Cargo.toml Cargo.lock
git commit -m "bump version to $NEW_TAG"
git push origin "$branch_name"
# Prepare PR title and body
PR_TITLE="Release $NEW_TAG"
PR_BODY="Automated version bump for release $NEW_TAG.
## Overview
- Version bump: ${{ github.event.inputs.version_type }}
- Type: ${{ github.event.inputs.version_type }}
- Draft: ${{ github.event.inputs.is_draft }}
## What changed
- Updated version in Cargo.toml and Cargo.lock
## Validation
- [ ] Version number is correct
- [ ] All tests pass
- [ ] Ready for release
Triggered by workflow run: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
# Prepare labels
PR_LABELS="release-proposal"
if [[ "${{ github.event.inputs.is_draft }}" == "true" ]]; then
PR_LABELS="$PR_LABELS,draft-release"
fi
gh pr create \
--title "$PR_TITLE" \
--body "$PR_BODY" \
--base "$TARGET_BRANCH" \
--head "$branch_name" \
--label "$PR_LABELS"