Skip to content

Auto Release

Auto Release #26

Workflow file for this run

name: Auto Release
on:
push:
branches: [main]
paths:
- "package.json"
workflow_dispatch:
# Allow manual trigger for testing
permissions:
contents: write
jobs:
check-version:
runs-on: ubuntu-latest
outputs:
version_changed: ${{ steps.version_check.outputs.changed }}
new_version: ${{ steps.version_check.outputs.version }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check version change
id: version_check
run: |
# Debug information
echo "=== DEBUG INFO ==="
echo "Event name: ${{ github.event_name }}"
echo "Ref: ${{ github.ref }}"
echo "Repository: ${{ github.repository }}"
# Get current version from package.json
CURRENT_VERSION=$(node -p "require('./package.json').version")
echo "Current version: $CURRENT_VERSION"
# Get previous version from git history
echo "=== FETCHING GIT HISTORY ==="
git fetch origin main || echo "No remote main branch yet"
# Debug git status
echo "=== GIT STATUS ==="
git log --oneline -5 || echo "No git history available"
# Try to get previous version from git history
PREVIOUS_VERSION=""
echo "=== CHECKING PREVIOUS PACKAGE.JSON ==="
if git show origin/main:package.json >/dev/null 2>&1; then
PREVIOUS_VERSION=$(git show origin/main:package.json | node -p "JSON.parse(require('fs').readFileSync(0)).version")
echo "Previous version: $PREVIOUS_VERSION"
else
echo "No previous version found in git history"
# Check if this is the first commit by looking for existing tags
echo "=== CHECKING EXISTING TAGS ==="
if git describe --tags --abbrev=0 >/dev/null 2>&1; then
# There are existing tags, so this is not the first release
echo "Found existing tags, but no previous package.json version"
echo "changed=false" >> $GITHUB_OUTPUT
exit 0
else
echo "No existing tags found - this might be the first release"
# For first release, we'll create a tag if there are no tags at all
PREVIOUS_VERSION=""
fi
fi
# Compare versions
echo "=== VERSION COMPARISON ==="
if [ -z "$PREVIOUS_VERSION" ] || [ "$CURRENT_VERSION" != "$PREVIOUS_VERSION" ]; then
if [ -n "$PREVIOUS_VERSION" ]; then
echo "Version changed from $PREVIOUS_VERSION to $CURRENT_VERSION"
else
echo "Creating first release with version $CURRENT_VERSION"
fi
echo "changed=true" >> $GITHUB_OUTPUT
echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
else
echo "Version unchanged ($CURRENT_VERSION)"
echo "changed=false" >> $GITHUB_OUTPUT
fi
create-release:
needs: check-version
if: needs.check-version.outputs.version_changed == 'true'
runs-on: ubuntu-latest
steps:
- name: Debug outputs
run: |
echo "Version changed: ${{ needs.check-version.outputs.version_changed }}"
echo "New version: ${{ needs.check-version.outputs.new_version }}"
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Get release notes
id: release_notes
run: |
VERSION="${{ needs.check-version.outputs.new_version }}"
# Try to get changelog entry for this version
if [ -f "CHANGELOG.md" ]; then
# Extract changelog section for this version
NOTES=$(awk "/^## \\[$VERSION\\]/ {flag=1; next} /^## \\[/ {flag=0} flag" CHANGELOG.md)
if [ -n "$NOTES" ]; then
echo "notes<<EOF" >> $GITHUB_OUTPUT
echo "$NOTES" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
fi
fi
# Fallback to commit messages if no changelog
if [ -z "$NOTES" ]; then
PREVIOUS_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -n "$PREVIOUS_TAG" ]; then
COMMITS=$(git log $PREVIOUS_TAG..HEAD --pretty=format:"- %s")
echo "notes<<EOF" >> $GITHUB_OUTPUT
echo "## What's Changed" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo "$COMMITS" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
else
echo "notes=Release version $VERSION" >> $GITHUB_OUTPUT
fi
fi
- name: Create Git Tag
run: |
VERSION="${{ needs.check-version.outputs.new_version }}"
echo "Creating tag for version: v$VERSION"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Check if tag already exists
if git rev-parse "v$VERSION" >/dev/null 2>&1; then
echo "Tag v$VERSION already exists, skipping tag creation"
else
echo "Creating tag v$VERSION"
git tag -a "v$VERSION" -m "Release version $VERSION"
echo "Pushing tag to origin"
git push origin "v$VERSION" || echo "Failed to push tag - this might be expected in some cases"
fi
- name: Create GitHub Release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
with:
tag_name: v${{ needs.check-version.outputs.new_version }}
release_name: Release v${{ needs.check-version.outputs.new_version }}
body: ${{ steps.release_notes.outputs.notes }}
draft: false
prerelease: false
manual-release:
if: github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Get current version
id: get_version
run: |
VERSION=$(node -p "require('./package.json').version")
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Check if tag exists
id: check_tag
run: |
VERSION="${{ steps.get_version.outputs.version }}"
if git rev-parse "v$VERSION" >/dev/null 2>&1; then
echo "Tag v$VERSION already exists"
echo "tag_exists=true" >> $GITHUB_OUTPUT
else
echo "Tag v$VERSION does not exist"
echo "tag_exists=false" >> $GITHUB_OUTPUT
fi
- name: Manual release
if: steps.check_tag.outputs.tag_exists == 'false'
run: |
VERSION="${{ steps.get_version.outputs.version }}"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag -a "v$VERSION" -m "Manual release version $VERSION"
git push origin "v$VERSION"
- name: Create GitHub Release (Manual)
if: steps.check_tag.outputs.tag_exists == 'false'
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
with:
tag_name: v${{ steps.get_version.outputs.version }}
release_name: Release v${{ steps.get_version.outputs.version }}
body: "Manual release triggered via workflow_dispatch"
draft: false
prerelease: false