|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + bump-type: |
| 9 | + description: 'Version bump type' |
| 10 | + required: true |
| 11 | + default: 'patch' |
| 12 | + type: choice |
| 13 | + options: |
| 14 | + - patch |
| 15 | + - minor |
| 16 | + - major |
| 17 | + |
| 18 | +permissions: |
| 19 | + contents: write |
| 20 | + |
| 21 | +jobs: |
| 22 | + release: |
| 23 | + name: Create Release |
| 24 | + runs-on: ubuntu-latest |
| 25 | + steps: |
| 26 | + - name: Checkout |
| 27 | + uses: actions/checkout@v6 |
| 28 | + with: |
| 29 | + fetch-depth: 0 |
| 30 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 31 | + |
| 32 | + - name: Get latest tag |
| 33 | + id: latest-tag |
| 34 | + run: | |
| 35 | + # Get the latest semver tag |
| 36 | + LATEST=$(git tag -l 'v*.*.*' | sort -V | tail -n1) |
| 37 | + if [[ -z "$LATEST" ]]; then |
| 38 | + LATEST="v0.0.0" |
| 39 | + fi |
| 40 | + echo "tag=$LATEST" >> $GITHUB_OUTPUT |
| 41 | + echo "Latest tag: $LATEST" |
| 42 | +
|
| 43 | + - name: Determine bump type from commits |
| 44 | + id: bump-type |
| 45 | + if: github.event_name == 'push' |
| 46 | + run: | |
| 47 | + LATEST_TAG="${{ steps.latest-tag.outputs.tag }}" |
| 48 | +
|
| 49 | + # Get commits since last tag |
| 50 | + if [[ "$LATEST_TAG" == "v0.0.0" ]]; then |
| 51 | + COMMITS=$(git log --oneline) |
| 52 | + else |
| 53 | + COMMITS=$(git log --oneline ${LATEST_TAG}..HEAD) |
| 54 | + fi |
| 55 | +
|
| 56 | + echo "Commits since $LATEST_TAG:" |
| 57 | + echo "$COMMITS" |
| 58 | + echo "" |
| 59 | +
|
| 60 | + # Determine bump type from conventional commits |
| 61 | + if echo "$COMMITS" | grep -qE '^[a-f0-9]+ (feat|fix|docs|style|refactor|perf|test|chore)(\(.+\))?!:|BREAKING CHANGE'; then |
| 62 | + BUMP="major" |
| 63 | + elif echo "$COMMITS" | grep -qE '^[a-f0-9]+ feat(\(.+\))?:'; then |
| 64 | + BUMP="minor" |
| 65 | + elif echo "$COMMITS" | grep -qE '^[a-f0-9]+ (fix|docs|style|refactor|perf|test|chore)(\(.+\))?:'; then |
| 66 | + BUMP="patch" |
| 67 | + else |
| 68 | + # Default to patch for non-conventional commits |
| 69 | + BUMP="patch" |
| 70 | + fi |
| 71 | +
|
| 72 | + echo "Determined bump type: $BUMP" |
| 73 | + echo "type=$BUMP" >> $GITHUB_OUTPUT |
| 74 | +
|
| 75 | + - name: Set bump type |
| 76 | + id: final-bump |
| 77 | + run: | |
| 78 | + if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then |
| 79 | + echo "type=${{ inputs.bump-type }}" >> $GITHUB_OUTPUT |
| 80 | + else |
| 81 | + echo "type=${{ steps.bump-type.outputs.type }}" >> $GITHUB_OUTPUT |
| 82 | + fi |
| 83 | +
|
| 84 | + - name: Calculate new version |
| 85 | + id: new-version |
| 86 | + run: | |
| 87 | + CURRENT="${{ steps.latest-tag.outputs.tag }}" |
| 88 | + BUMP="${{ steps.final-bump.outputs.type }}" |
| 89 | +
|
| 90 | + # Remove 'v' prefix |
| 91 | + VERSION=${CURRENT#v} |
| 92 | +
|
| 93 | + # Split into parts |
| 94 | + IFS='.' read -r MAJOR MINOR PATCH <<< "$VERSION" |
| 95 | +
|
| 96 | + case $BUMP in |
| 97 | + major) |
| 98 | + MAJOR=$((MAJOR + 1)) |
| 99 | + MINOR=0 |
| 100 | + PATCH=0 |
| 101 | + ;; |
| 102 | + minor) |
| 103 | + MINOR=$((MINOR + 1)) |
| 104 | + PATCH=0 |
| 105 | + ;; |
| 106 | + patch) |
| 107 | + PATCH=$((PATCH + 1)) |
| 108 | + ;; |
| 109 | + esac |
| 110 | +
|
| 111 | + NEW_VERSION="v${MAJOR}.${MINOR}.${PATCH}" |
| 112 | + MAJOR_TAG="v${MAJOR}" |
| 113 | +
|
| 114 | + echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 115 | + echo "major-tag=$MAJOR_TAG" >> $GITHUB_OUTPUT |
| 116 | + echo "New version: $NEW_VERSION (major tag: $MAJOR_TAG)" |
| 117 | +
|
| 118 | + - name: Generate changelog |
| 119 | + id: changelog |
| 120 | + run: | |
| 121 | + LATEST_TAG="${{ steps.latest-tag.outputs.tag }}" |
| 122 | + NEW_VERSION="${{ steps.new-version.outputs.version }}" |
| 123 | +
|
| 124 | + echo "## What's Changed in $NEW_VERSION" > changelog.md |
| 125 | + echo "" >> changelog.md |
| 126 | +
|
| 127 | + # Get commits since last tag |
| 128 | + if [[ "$LATEST_TAG" == "v0.0.0" ]]; then |
| 129 | + COMMITS=$(git log --pretty=format:"- %s (%h)" --reverse) |
| 130 | + else |
| 131 | + COMMITS=$(git log --pretty=format:"- %s (%h)" --reverse ${LATEST_TAG}..HEAD) |
| 132 | + fi |
| 133 | +
|
| 134 | + # Categorize commits |
| 135 | + FEATURES=$(echo "$COMMITS" | grep -E "^- feat" || true) |
| 136 | + FIXES=$(echo "$COMMITS" | grep -E "^- fix" || true) |
| 137 | + OTHER=$(echo "$COMMITS" | grep -vE "^- (feat|fix)" || true) |
| 138 | +
|
| 139 | + if [[ -n "$FEATURES" ]]; then |
| 140 | + echo "### Features" >> changelog.md |
| 141 | + echo "$FEATURES" | sed 's/^- feat[^:]*: /- /' >> changelog.md |
| 142 | + echo "" >> changelog.md |
| 143 | + fi |
| 144 | +
|
| 145 | + if [[ -n "$FIXES" ]]; then |
| 146 | + echo "### Bug Fixes" >> changelog.md |
| 147 | + echo "$FIXES" | sed 's/^- fix[^:]*: /- /' >> changelog.md |
| 148 | + echo "" >> changelog.md |
| 149 | + fi |
| 150 | +
|
| 151 | + if [[ -n "$OTHER" ]]; then |
| 152 | + echo "### Other Changes" >> changelog.md |
| 153 | + echo "$OTHER" >> changelog.md |
| 154 | + echo "" >> changelog.md |
| 155 | + fi |
| 156 | +
|
| 157 | + echo "" >> changelog.md |
| 158 | + echo "**Full Changelog**: https://github.com/${{ github.repository }}/compare/${LATEST_TAG}...${NEW_VERSION}" >> changelog.md |
| 159 | +
|
| 160 | + cat changelog.md |
| 161 | +
|
| 162 | + - name: Create exact version tag |
| 163 | + run: | |
| 164 | + NEW_VERSION="${{ steps.new-version.outputs.version }}" |
| 165 | + git config user.name "github-actions[bot]" |
| 166 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 167 | + git tag -a "$NEW_VERSION" -m "Release $NEW_VERSION" |
| 168 | + git push origin "$NEW_VERSION" |
| 169 | +
|
| 170 | + - name: Update major version tag |
| 171 | + run: | |
| 172 | + MAJOR_TAG="${{ steps.new-version.outputs.major-tag }}" |
| 173 | + git config user.name "github-actions[bot]" |
| 174 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 175 | + git tag -fa "$MAJOR_TAG" -m "Update $MAJOR_TAG to ${{ steps.new-version.outputs.version }}" |
| 176 | + git push origin "$MAJOR_TAG" --force |
| 177 | +
|
| 178 | + - name: Create GitHub Release |
| 179 | + uses: softprops/action-gh-release@v2 |
| 180 | + with: |
| 181 | + tag_name: ${{ steps.new-version.outputs.version }} |
| 182 | + name: ${{ steps.new-version.outputs.version }} |
| 183 | + body_path: changelog.md |
| 184 | + draft: false |
| 185 | + prerelease: false |
| 186 | + env: |
| 187 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 188 | + |
| 189 | + - name: Summary |
| 190 | + run: | |
| 191 | + echo "## 🚀 Release Created" >> $GITHUB_STEP_SUMMARY |
| 192 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 193 | + echo "- **Version**: ${{ steps.new-version.outputs.version }}" >> $GITHUB_STEP_SUMMARY |
| 194 | + echo "- **Major Tag**: ${{ steps.new-version.outputs.major-tag }}" >> $GITHUB_STEP_SUMMARY |
| 195 | + echo "- **Bump Type**: ${{ steps.final-bump.outputs.type }}" >> $GITHUB_STEP_SUMMARY |
| 196 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 197 | + echo "### Usage" >> $GITHUB_STEP_SUMMARY |
| 198 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 199 | + echo "\`\`\`yaml" >> $GITHUB_STEP_SUMMARY |
| 200 | + echo "# Recommended: Use major version tag" >> $GITHUB_STEP_SUMMARY |
| 201 | + echo "uses: ${{ github.repository }}/actions/eslint-js-prefix@${{ steps.new-version.outputs.major-tag }}" >> $GITHUB_STEP_SUMMARY |
| 202 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 203 | + echo "# Pinned: Use exact version" >> $GITHUB_STEP_SUMMARY |
| 204 | + echo "uses: ${{ github.repository }}/actions/eslint-js-prefix@${{ steps.new-version.outputs.version }}" >> $GITHUB_STEP_SUMMARY |
| 205 | + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY |
0 commit comments