Skip to content

Merge pull request #111 from homebysix/dev #2

Merge pull request #111 from homebysix/dev

Merge pull request #111 from homebysix/dev #2

Workflow file for this run

---
name: Release
on:
push:
branches:
- main
paths:
- "setup.py"
- ".github/workflows/release.yml"
jobs:
release:
name: Create GitHub Release
runs-on: ubuntu-latest
steps:
- name: Checkout source code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Read version from setup.py
id: getversion
run: |
echo "version=$(python ./setup.py --version)" >> $GITHUB_OUTPUT
echo "Version detected: $(python ./setup.py --version)"
- name: Validate version alignment
run: |
VERSION="${{ steps.getversion.outputs.version }}"
ERRORS=0
# Check CHANGELOG.md has the version section
if ! grep -q "^## \[$VERSION\]" CHANGELOG.md; then
echo "::error file=CHANGELOG.md::CHANGELOG.md is missing a section for version [$VERSION]. The workflow extracts release notes from the CHANGELOG, so this section must exist before releasing. Expected format: '## [$VERSION] - YYYY-MM-DD'"
ERRORS=1
fi
# Check README.md has the version
if ! grep -q "rev: v$VERSION" README.md; then
CURRENT_README_VERSION=$(grep -m 1 "rev: v" README.md | sed -n 's/.*rev: v\([0-9.]*\).*/\1/p')
echo "::error file=README.md::README.md version (v$CURRENT_README_VERSION) does not match setup.py version ($VERSION). Update all 'rev: v' references in README.md to 'rev: v$VERSION' before releasing."
ERRORS=1
fi
# Check version link exists in CHANGELOG.md
if ! grep -q "^\[$VERSION\]:" CHANGELOG.md; then
echo "::error file=CHANGELOG.md::CHANGELOG.md is missing a version comparison link for [$VERSION]. Add '[$VERSION]: https://github.com/homebysix/pre-commit-macadmin/compare/vPREVIOUS...v$VERSION' at the bottom of CHANGELOG.md."
ERRORS=1
fi
if [ $ERRORS -gt 0 ]; then
echo ""
echo "❌ Version alignment check failed. Please ensure:"
echo " 1. CHANGELOG.md has a '## [$VERSION]' section with release notes"
echo " 2. README.md has 'rev: v$VERSION' in all examples"
echo " 3. CHANGELOG.md has a '[$VERSION]:' comparison link at the bottom"
echo ""
exit 1
fi
echo "✅ Version $VERSION is properly aligned across all files"
- name: Fetch tags
run: git fetch --tags origin
- name: Check if tag already exists
id: tagcheck
run: |
if git rev-parse "v${{ steps.getversion.outputs.version }}" >/dev/null 2>&1; then
echo "Tag v${{ steps.getversion.outputs.version }} already exists"
echo "should_release=false" >> $GITHUB_OUTPUT
else
echo "Tag v${{ steps.getversion.outputs.version }} does not exist - will proceed with release"
echo "should_release=true" >> $GITHUB_OUTPUT
fi
- name: Extract changelog for version
if: steps.tagcheck.outputs.should_release == 'true'
id: changelog
run: |
VERSION="${{ steps.getversion.outputs.version }}"
# Extract content between ## [VERSION] and the next ## [
CHANGELOG=$(sed -n "/## \[$VERSION\]/,/## \[/p" CHANGELOG.md | sed '$d' | tail -n +2)
if [ -z "$CHANGELOG" ]; then
echo "No changelog entry found for version $VERSION"
CHANGELOG="No changelog entry found. Please update CHANGELOG.md."
fi
# Use EOF delimiter for multi-line output
echo "notes<<EOF" >> $GITHUB_OUTPUT
echo "$CHANGELOG" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Create GitHub Release
if: steps.tagcheck.outputs.should_release == 'true'
uses: softprops/action-gh-release@v2
with:
tag_name: v${{ steps.getversion.outputs.version }}
name: Release v${{ steps.getversion.outputs.version }}
draft: false
prerelease: false
body: ${{ steps.changelog.outputs.notes }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Calculate next version
if: steps.tagcheck.outputs.should_release == 'true'
id: nextversion
run: |
CURRENT="${{ steps.getversion.outputs.version }}"
# Split version into parts (assumes X.Y.Z format)
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT"
# Bump patch version
NEXT_PATCH=$((PATCH + 1))
NEXT_VERSION="${MAJOR}.${MINOR}.${NEXT_PATCH}"
echo "next=$NEXT_VERSION" >> $GITHUB_OUTPUT
echo "Next version will be: $NEXT_VERSION"
- name: Merge main to dev
if: steps.tagcheck.outputs.should_release == 'true'
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git fetch origin dev
git checkout dev
git merge origin/main --no-edit
- name: Update setup.py version on dev
if: steps.tagcheck.outputs.should_release == 'true'
run: |
sed -i 's/version="${{ steps.getversion.outputs.version }}"/version="${{ steps.nextversion.outputs.next }}"/' setup.py
- name: Update README.md version on dev
if: steps.tagcheck.outputs.should_release == 'true'
run: |
sed -i 's/rev: v${{ steps.getversion.outputs.version }}/rev: v${{ steps.nextversion.outputs.next }}/g' README.md
- name: Commit version bump on dev
if: steps.tagcheck.outputs.should_release == 'true'
run: |
git add setup.py README.md
git commit -m "Bump to ${{ steps.nextversion.outputs.next }}"
- name: Push dev branch
if: steps.tagcheck.outputs.should_release == 'true'
run: |
git push origin dev