Skip to content

chore: update pyproject.toml to include src/cryptoservice in sdist (#27) #3

chore: update pyproject.toml to include src/cryptoservice in sdist (#27)

chore: update pyproject.toml to include src/cryptoservice in sdist (#27) #3

name: Auto Version and Tag
on:
push:
branches:
- main
permissions:
contents: write
pull-requests: write
jobs:
auto-version:
# 只在非 tag 推送且包含有效提交时运行
if: |
!startsWith(github.ref, 'refs/tags/') &&
!contains(github.event.head_commit.message, '[skip ci]') &&
!contains(github.event.head_commit.message, 'chore: update CHANGELOG')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- uses: astral-sh/setup-uv@v4
with:
python-version: "3.12"
- name: Determine version bump type
id: version_type
run: |
# 获取最近的 tag
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
echo "Last tag: $LAST_TAG"
# 获取从最后一个 tag 到当前的所有提交
COMMITS=$(git log ${LAST_TAG}..HEAD --pretty=format:"%s")
echo "Commits since last tag:"
echo "$COMMITS"
# 检查是否有 changes 目录下的变更文件
CHANGE_FILES=$(find changes -name "*.md" 2>/dev/null | wc -l || echo "0")
echo "Change files found: $CHANGE_FILES"
# 根据提交信息判断版本变更类型
BUMP_TYPE="none"
# 检查是否有 breaking change
if echo "$COMMITS" | grep -qiE "^(breaking|BREAKING CHANGE|!)"; then
BUMP_TYPE="major"
# 检查是否有新功能
elif echo "$COMMITS" | grep -qiE "^(feat|feature)(\(.*\))?:"; then
BUMP_TYPE="minor"
# 检查是否有修复
elif echo "$COMMITS" | grep -qiE "^(fix|bugfix|hotfix)(\(.*\))?:"; then
BUMP_TYPE="patch"
# 检查 changes 目录
elif [ "$CHANGE_FILES" -gt 0 ]; then
# 检查是否有 feature 或 breaking 类型
if find changes -name "*.feature.md" 2>/dev/null | grep -q .; then
BUMP_TYPE="minor"
elif find changes -name "*.breaking.md" 2>/dev/null | grep -q .; then
BUMP_TYPE="major"
else
BUMP_TYPE="patch"
fi
fi
echo "bump_type=$BUMP_TYPE" >> $GITHUB_OUTPUT
echo "Determined bump type: $BUMP_TYPE"
- name: Calculate new version
if: steps.version_type.outputs.bump_type != 'none'
id: new_version
run: |
# 获取当前版本
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
VERSION=${LAST_TAG#v}
# 解析版本号
IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION"
# 根据 bump type 计算新版本
BUMP_TYPE="${{ steps.version_type.outputs.bump_type }}"
if [ "$BUMP_TYPE" = "major" ]; then
MAJOR=$((MAJOR + 1))
MINOR=0
PATCH=0
elif [ "$BUMP_TYPE" = "minor" ]; then
MINOR=$((MINOR + 1))
PATCH=0
elif [ "$BUMP_TYPE" = "patch" ]; then
PATCH=$((PATCH + 1))
fi
NEW_VERSION="v${MAJOR}.${MINOR}.${PATCH}"
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "version_number=${MAJOR}.${MINOR}.${PATCH}" >> $GITHUB_OUTPUT
echo "New version: $NEW_VERSION"
- name: Build changelog with towncrier
if: steps.version_type.outputs.bump_type != 'none'
run: |
VERSION="${{ steps.new_version.outputs.version_number }}"
# 检查是否有 changes 目录和文件
if [ -d "changes" ] && [ "$(find changes -name '*.md' | wc -l)" -gt 0 ]; then
echo "Building changelog with towncrier..."
uvx towncrier build --yes --version "$VERSION"
else
echo "No change files found, skipping towncrier build"
fi
- name: Commit changelog and create tag
if: steps.version_type.outputs.bump_type != 'none'
env:
NEW_VERSION: ${{ steps.new_version.outputs.new_version }}
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# 如果 CHANGELOG 有变更,提交它
if [ -f "CHANGELOG.md" ] && git diff --quiet CHANGELOG.md; then
echo "No changelog changes to commit"
elif [ -f "CHANGELOG.md" ]; then
git add CHANGELOG.md
git add changes/ || true
git commit -m "chore: update CHANGELOG for ${NEW_VERSION} [skip ci]"
git push origin main
fi
# 创建并推送 tag
git tag -a "${NEW_VERSION}" -m "Release ${NEW_VERSION}"
git push origin "${NEW_VERSION}"
echo "✅ Created and pushed tag: ${NEW_VERSION}"
- name: Summary
if: steps.version_type.outputs.bump_type != 'none'
run: |
echo "## 🚀 Auto Release Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "- **Version Bump Type**: ${{ steps.version_type.outputs.bump_type }}" >> $GITHUB_STEP_SUMMARY
echo "- **New Version**: ${{ steps.new_version.outputs.new_version }}" >> $GITHUB_STEP_SUMMARY
echo "- **Next Step**: Release workflow will be triggered automatically" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "The release to PyPI will start shortly..." >> $GITHUB_STEP_SUMMARY
- name: No version bump needed
if: steps.version_type.outputs.bump_type == 'none'
run: |
echo "## ℹ️ No Version Bump Needed" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "No conventional commits or change files detected." >> $GITHUB_STEP_SUMMARY
echo "To trigger a release, use one of these commit formats:" >> $GITHUB_STEP_SUMMARY
echo "- \`feat: ...\` for minor version bump" >> $GITHUB_STEP_SUMMARY
echo "- \`fix: ...\` for patch version bump" >> $GITHUB_STEP_SUMMARY
echo "- \`BREAKING CHANGE: ...\` for major version bump" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "Or add change files in \`changes/\` directory." >> $GITHUB_STEP_SUMMARY