Files
payload-mailing/.github/workflows/version-and-publish.yml
Bas van den Aakster 5d4ba245f5 feat: integrate changelog directly into PR merge commit
🔧 **Improved Changelog Integration:**

- Changelog is now appended to the PR merge commit message instead of creating a separate commit
- Version bump changes are included in the same amended merge commit
- Uses git commit --amend to modify the merge commit with both changelog and version changes
- Updated force push with --force-with-lease for safer history rewriting
- Cleaner git history with single commit containing all release information

**Benefits:**
-  Single commit per release (cleaner history)
-  Changelog directly visible in merge commit
-  No additional 'chore: bump version' commits
-  All release info consolidated in one place
-  Safer force push with --force-with-lease
2025-09-13 13:03:00 +02:00

234 lines
8.4 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: Update merge commit with changelog
if: steps.version-type.outputs.type != 'none'
run: |
# Get the merge commit (HEAD)
MERGE_COMMIT=$(git rev-parse HEAD)
# Get original commit message
ORIGINAL_MSG=$(git log -1 --format=%B)
# Create new commit message with changelog
NEW_MSG="$ORIGINAL_MSG
${{ steps.changelog.outputs.changelog }}"
# Amend the merge commit with the new message
git commit --amend -m "$NEW_MSG"
- name: Version bump
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 merge 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 "Version bumped 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 }}"