Files
payload-mailing/.github/workflows/version-and-publish.yml
Bas van den Aakster 53de251421 feat: implement single squashed commit per release
🔄 **Ultra-Clean Git History:**

Now creates one beautiful commit per release instead of multiple merge commits:

**Before:**
```
abc123 Merge pull request #45 from version/minor
def456 feat: add email scheduling
ghi789 fix: validation bug
jkl012 docs: update readme
mno345 test: add unit tests
```

**After:**
```
abc123  Minor Release

## Changes
### 🚀 Features
- Add email scheduling feature
### 🐛 Bug Fixes
- Fix validation error handling
### 📚 Documentation
- Update readme with new examples
```

**How it works:**
1. Analyzes commits since last release tag
2. Squashes all PR commits into single commit
3. Uses semantic emoji titles (🚀🔧🐛)
4. Includes AI-generated changelog with categorized changes
5. Adds version bump changes to same commit

**Result:** Perfect git history with one meaningful commit per release! 🎉
2025-09-13 13:10:23 +02:00

250 lines
9.2 KiB
YAML

name: Version and Publish
on:
push:
branches:
- main
pull_request:
branches:
- main
types: [closed]
jobs:
version-and-publish:
if: github.event_name == 'push' || (github.event.pull_request.merged == true && (contains(github.event.pull_request.head.ref, 'version/major') || contains(github.event.pull_request.head.ref, 'version/minor') || contains(github.event.pull_request.head.ref, 'version/patch')))
runs-on: ubuntu-latest
outputs:
new-version: ${{ steps.version-bump.outputs.new-version }}
current-version: ${{ steps.version-bump.outputs.current-version }}
version-type: ${{ steps.version-type.outputs.type }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
registry-url: 'https://registry.npmjs.org'
- name: Setup pnpm
uses: pnpm/action-setup@v4
with:
version: 8
- name: Get pnpm store directory
shell: bash
run: echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- name: Setup pnpm cache
uses: actions/cache@v4
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Run tests
run: pnpm test
- name: Run build
run: pnpm build
- name: Determine version bump type
id: version-type
run: |
if [[ "${{ github.event.pull_request.head.ref }}" =~ version/major ]]; then
echo "type=major" >> $GITHUB_OUTPUT
elif [[ "${{ github.event.pull_request.head.ref }}" =~ version/minor ]]; then
echo "type=minor" >> $GITHUB_OUTPUT
elif [[ "${{ github.event.pull_request.head.ref }}" =~ version/patch ]]; then
echo "type=patch" >> $GITHUB_OUTPUT
elif [[ "${{ github.event_name }}" == "push" ]]; then
# Default to patch for direct pushes to main
echo "type=patch" >> $GITHUB_OUTPUT
else
echo "type=none" >> $GITHUB_OUTPUT
fi
- name: Configure git
if: steps.version-type.outputs.type != 'none'
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
- name: Install Claude Code CLI
if: steps.version-type.outputs.type != 'none'
run: |
curl -fsSL https://claude.ai/cli/install.sh | bash
echo "$HOME/.claude/bin" >> $GITHUB_PATH
- name: Generate changelog with Claude
if: steps.version-type.outputs.type != 'none'
id: changelog
run: |
# Get commits since last tag
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -z "$LAST_TAG" ]; then
COMMITS=$(git log --oneline --no-merges --since="1 week ago")
else
COMMITS=$(git log --oneline --no-merges ${LAST_TAG}..HEAD)
fi
# Generate changelog using Claude
CHANGELOG=$(claude-code << EOF
Please analyze the following git commits and generate a concise changelog in this exact format:
## Changes
### 🚀 Features
- Brief description of new features
### 🐛 Bug Fixes
- Brief description of bug fixes
### 🔧 Improvements
- Brief description of improvements/refactoring
### 📚 Documentation
- Brief description of documentation changes
### ⚡ Performance
- Brief description of performance improvements
Only include sections that have actual changes. Keep each bullet point concise and user-focused.
Git commits to analyze:
$COMMITS
EOF
)
# Save changelog to output
echo "changelog<<EOF" >> $GITHUB_OUTPUT
echo "$CHANGELOG" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Create single squashed commit with changelog
if: steps.version-type.outputs.type != 'none'
run: |
# Extract version type for commit title
VERSION_TYPE="${{ steps.version-type.outputs.type }}"
# Create clean commit message with just the changelog
if [ "$VERSION_TYPE" = "major" ]; then
COMMIT_TITLE="🚀 Major Release"
elif [ "$VERSION_TYPE" = "minor" ]; then
COMMIT_TITLE="✨ Minor Release"
else
COMMIT_TITLE="🐛 Patch Release"
fi
NEW_MSG="$COMMIT_TITLE
${{ steps.changelog.outputs.changelog }}"
# Get the previous commit (before the merge/PR commits)
# This assumes we want to squash everything since the last release
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -n "$LAST_TAG" ]; then
BASE_COMMIT=$LAST_TAG
else
# If no tags, use the commit before this PR
BASE_COMMIT=$(git log --oneline --skip=10 -1 --format="%H" 2>/dev/null || git log --max-parents=0 --format="%H")
fi
# Reset to the base and create a single squashed commit
git reset --soft $BASE_COMMIT
git commit -m "$NEW_MSG"
- name: Version bump and finalize commit
if: steps.version-type.outputs.type != 'none'
id: version-bump
run: |
# Get current version
CURRENT_VERSION=$(node -p "require('./package.json').version")
echo "current-version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
# Bump version (npm is used for version command as pnpm doesn't have equivalent)
npm version ${{ steps.version-type.outputs.type }} --no-git-tag-version
# Get new version
NEW_VERSION=$(node -p "require('./package.json').version")
echo "new-version=$NEW_VERSION" >> $GITHUB_OUTPUT
# Stage version changes
git add package.json
# Add lockfile if it exists (npm version might create package-lock.json)
if [ -f package-lock.json ]; then
git add package-lock.json
fi
if [ -f pnpm-lock.yaml ]; then
git add pnpm-lock.yaml
fi
# Amend the squashed commit to include version changes
git commit --amend --no-edit
# Create git tag
git tag -a "v$NEW_VERSION" -m "Version $NEW_VERSION
${{ steps.changelog.outputs.changelog }}"
echo "Created single squashed commit with version bump from $CURRENT_VERSION to $NEW_VERSION"
- name: Push version changes
if: steps.version-type.outputs.type != 'none'
run: |
git push --force-with-lease origin main
git push origin --tags
- name: Publish to NPM
if: steps.version-type.outputs.type != 'none'
run: pnpm publish --access public --no-git-checks
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Create GitHub Release
if: steps.version-type.outputs.type != 'none'
run: |
gh release create "v${{ steps.version-bump.outputs.new-version }}" \
--title "Release v${{ steps.version-bump.outputs.new-version }}" \
--notes "# Release v${{ steps.version-bump.outputs.new-version }}
${{ steps.changelog.outputs.changelog }}
---
**Version Info**: ${{ steps.version-type.outputs.type }} release (v${{ steps.version-bump.outputs.current-version }} → v${{ steps.version-bump.outputs.new-version }})
### Installation
\`\`\`bash
npm install @xtr-dev/payload-mailing@${{ steps.version-bump.outputs.new-version }}
\`\`\`
### Documentation
See the [README](https://github.com/xtr-dev/payload-mailing#readme) for usage instructions and full documentation."
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
notify-success:
if: github.event_name == 'push' || (github.event.pull_request.merged == true && (contains(github.event.pull_request.head.ref, 'version/major') || contains(github.event.pull_request.head.ref, 'version/minor') || contains(github.event.pull_request.head.ref, 'version/patch')))
needs: version-and-publish
runs-on: ubuntu-latest
steps:
- name: Success notification
if: needs.version-and-publish.outputs.new-version != ''
run: |
echo "🎉 Successfully published version ${{ needs.version-and-publish.outputs.new-version }} to NPM!"
echo "📦 Package: https://www.npmjs.com/package/@xtr-dev/payload-mailing"
echo "🏷️ GitHub Release: https://github.com/xtr-dev/payload-mailing/releases/tag/v${{ needs.version-and-publish.outputs.new-version }}"
echo "🔄 Version Type: ${{ needs.version-and-publish.outputs.version-type }}"
echo "📈 Version Change: v${{ needs.version-and-publish.outputs.current-version }} → v${{ needs.version-and-publish.outputs.new-version }}"