|
| 1 | +name: Test and Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + pull_request: |
| 8 | + branches: |
| 9 | + - main |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: write |
| 13 | + pull-requests: read |
| 14 | + |
| 15 | +jobs: |
| 16 | + test: |
| 17 | + name: Run Tests |
| 18 | + runs-on: ubuntu-latest |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: Checkout code |
| 22 | + uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + fetch-depth: 0 # Fetch all history for changelog generation |
| 25 | + |
| 26 | + - name: Setup Bun |
| 27 | + uses: oven-sh/setup-bun@v2 |
| 28 | + with: |
| 29 | + bun-version: latest |
| 30 | + |
| 31 | + - name: Install dependencies |
| 32 | + run: bun install |
| 33 | + |
| 34 | + - name: Run tests |
| 35 | + run: bun test |
| 36 | + |
| 37 | + - name: Build project |
| 38 | + run: bun run build |
| 39 | + |
| 40 | + release: |
| 41 | + name: Create Release |
| 42 | + runs-on: ubuntu-latest |
| 43 | + needs: test |
| 44 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 45 | + |
| 46 | + steps: |
| 47 | + - name: Checkout code |
| 48 | + uses: actions/checkout@v4 |
| 49 | + with: |
| 50 | + fetch-depth: 0 # Fetch all history for version comparison |
| 51 | + fetch-tags: true |
| 52 | + |
| 53 | + - name: Setup Bun |
| 54 | + uses: oven-sh/setup-bun@v2 |
| 55 | + with: |
| 56 | + bun-version: latest |
| 57 | + |
| 58 | + - name: Get current version |
| 59 | + id: current_version |
| 60 | + run: | |
| 61 | + VERSION=$(node -p "require('./package.json').version") |
| 62 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 63 | + echo "Current version: $VERSION" |
| 64 | +
|
| 65 | + - name: Check if version tag exists |
| 66 | + id: check_tag |
| 67 | + run: | |
| 68 | + if git rev-parse "v${{ steps.current_version.outputs.version }}" >/dev/null 2>&1; then |
| 69 | + echo "exists=true" >> $GITHUB_OUTPUT |
| 70 | + echo "Version tag v${{ steps.current_version.outputs.version }} already exists" |
| 71 | + else |
| 72 | + echo "exists=false" >> $GITHUB_OUTPUT |
| 73 | + echo "Version tag v${{ steps.current_version.outputs.version }} does not exist" |
| 74 | + fi |
| 75 | +
|
| 76 | + - name: Get previous version tag |
| 77 | + id: previous_version |
| 78 | + if: steps.check_tag.outputs.exists == 'false' |
| 79 | + run: | |
| 80 | + PREV_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "") |
| 81 | + if [ -z "$PREV_TAG" ]; then |
| 82 | + echo "tag=" >> $GITHUB_OUTPUT |
| 83 | + echo "No previous version tag found" |
| 84 | + else |
| 85 | + echo "tag=$PREV_TAG" >> $GITHUB_OUTPUT |
| 86 | + echo "Previous version tag: $PREV_TAG" |
| 87 | + fi |
| 88 | +
|
| 89 | + - name: Generate changelog |
| 90 | + id: changelog |
| 91 | + if: steps.check_tag.outputs.exists == 'false' |
| 92 | + run: | |
| 93 | + VERSION="${{ steps.current_version.outputs.version }}" |
| 94 | + PREV_TAG="${{ steps.previous_version.outputs.tag }}" |
| 95 | +
|
| 96 | + echo "Generating changelog from $PREV_TAG to HEAD" |
| 97 | +
|
| 98 | + # Create changelog file |
| 99 | + CHANGELOG_FILE=$(mktemp) |
| 100 | +
|
| 101 | + echo "## 🚀 What's Changed in v$VERSION" >> $CHANGELOG_FILE |
| 102 | + echo "" >> $CHANGELOG_FILE |
| 103 | +
|
| 104 | + if [ -z "$PREV_TAG" ]; then |
| 105 | + # First release - list all commits |
| 106 | + echo "### All Changes" >> $CHANGELOG_FILE |
| 107 | + echo "" >> $CHANGELOG_FILE |
| 108 | + git log --pretty=format:"- %s (%h)" --reverse >> $CHANGELOG_FILE |
| 109 | + else |
| 110 | + # Generate changelog from previous tag |
| 111 | + COMMITS=$(git log $PREV_TAG..HEAD --pretty=format:"%s|%h|%an" --reverse) |
| 112 | + |
| 113 | + # Categorize commits |
| 114 | + FEATURES="" |
| 115 | + FIXES="" |
| 116 | + DOCS="" |
| 117 | + TESTS="" |
| 118 | + CHORES="" |
| 119 | + OTHER="" |
| 120 | + |
| 121 | + while IFS='|' read -r message hash author; do |
| 122 | + # Skip merge commits |
| 123 | + if [[ $message == Merge* ]]; then |
| 124 | + continue |
| 125 | + fi |
| 126 | + |
| 127 | + COMMIT_LINE="- $message ([${hash}](https://github.com/${{ github.repository }}/commit/${hash}))" |
| 128 | + |
| 129 | + if [[ $message == feat:* ]] || [[ $message == feature:* ]]; then |
| 130 | + FEATURES="$FEATURES$COMMIT_LINE"$'\n' |
| 131 | + elif [[ $message == fix:* ]]; then |
| 132 | + FIXES="$FIXES$COMMIT_LINE"$'\n' |
| 133 | + elif [[ $message == docs:* ]]; then |
| 134 | + DOCS="$DOCS$COMMIT_LINE"$'\n' |
| 135 | + elif [[ $message == test:* ]]; then |
| 136 | + TESTS="$TESTS$COMMIT_LINE"$'\n' |
| 137 | + elif [[ $message == chore:* ]] || [[ $message == build:* ]] || [[ $message == ci:* ]]; then |
| 138 | + CHORES="$CHORES$COMMIT_LINE"$'\n' |
| 139 | + else |
| 140 | + OTHER="$OTHER$COMMIT_LINE"$'\n' |
| 141 | + fi |
| 142 | + done <<< "$COMMITS" |
| 143 | + |
| 144 | + # Add sections to changelog if they have content |
| 145 | + if [ ! -z "$FEATURES" ]; then |
| 146 | + echo "### ✨ Features" >> $CHANGELOG_FILE |
| 147 | + echo "" >> $CHANGELOG_FILE |
| 148 | + echo "$FEATURES" >> $CHANGELOG_FILE |
| 149 | + fi |
| 150 | + |
| 151 | + if [ ! -z "$FIXES" ]; then |
| 152 | + echo "### 🐛 Bug Fixes" >> $CHANGELOG_FILE |
| 153 | + echo "" >> $CHANGELOG_FILE |
| 154 | + echo "$FIXES" >> $CHANGELOG_FILE |
| 155 | + fi |
| 156 | + |
| 157 | + if [ ! -z "$TESTS" ]; then |
| 158 | + echo "### 🧪 Tests" >> $CHANGELOG_FILE |
| 159 | + echo "" >> $CHANGELOG_FILE |
| 160 | + echo "$TESTS" >> $CHANGELOG_FILE |
| 161 | + fi |
| 162 | + |
| 163 | + if [ ! -z "$DOCS" ]; then |
| 164 | + echo "### 📚 Documentation" >> $CHANGELOG_FILE |
| 165 | + echo "" >> $CHANGELOG_FILE |
| 166 | + echo "$DOCS" >> $CHANGELOG_FILE |
| 167 | + fi |
| 168 | + |
| 169 | + if [ ! -z "$OTHER" ]; then |
| 170 | + echo "### 🔄 Other Changes" >> $CHANGELOG_FILE |
| 171 | + echo "" >> $CHANGELOG_FILE |
| 172 | + echo "$OTHER" >> $CHANGELOG_FILE |
| 173 | + fi |
| 174 | + |
| 175 | + if [ ! -z "$CHORES" ]; then |
| 176 | + echo "### 🔧 Maintenance" >> $CHANGELOG_FILE |
| 177 | + echo "" >> $CHANGELOG_FILE |
| 178 | + echo "$CHORES" >> $CHANGELOG_FILE |
| 179 | + fi |
| 180 | + fi |
| 181 | +
|
| 182 | + echo "" >> $CHANGELOG_FILE |
| 183 | + echo "---" >> $CHANGELOG_FILE |
| 184 | + echo "" >> $CHANGELOG_FILE |
| 185 | + echo "**Full Changelog**: https://github.com/${{ github.repository }}/compare/$PREV_TAG...v$VERSION" >> $CHANGELOG_FILE |
| 186 | +
|
| 187 | + # Output for GitHub Actions |
| 188 | + { |
| 189 | + echo 'body<<EOF' |
| 190 | + cat $CHANGELOG_FILE |
| 191 | + echo EOF |
| 192 | + } >> $GITHUB_OUTPUT |
| 193 | +
|
| 194 | + echo "Changelog:" |
| 195 | + cat $CHANGELOG_FILE |
| 196 | +
|
| 197 | + - name: Create Release |
| 198 | + if: steps.check_tag.outputs.exists == 'false' |
| 199 | + uses: softprops/action-gh-release@v1 |
| 200 | + with: |
| 201 | + tag_name: v${{ steps.current_version.outputs.version }} |
| 202 | + name: v${{ steps.current_version.outputs.version }} |
| 203 | + body: ${{ steps.changelog.outputs.body }} |
| 204 | + draft: false |
| 205 | + prerelease: false |
| 206 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 207 | + |
| 208 | + - name: Skip release |
| 209 | + if: steps.check_tag.outputs.exists == 'true' |
| 210 | + run: | |
| 211 | + echo "⏭️ Skipping release - version v${{ steps.current_version.outputs.version }} already exists" |
0 commit comments