|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + version_type: |
| 7 | + description: "Version bump type" |
| 8 | + required: true |
| 9 | + default: "patch" |
| 10 | + type: choice |
| 11 | + options: |
| 12 | + - patch |
| 13 | + - minor |
| 14 | + - major |
| 15 | + - custom |
| 16 | + custom_version: |
| 17 | + description: "Custom version (only used if type is 'custom')" |
| 18 | + required: false |
| 19 | + type: string |
| 20 | + |
| 21 | +jobs: |
| 22 | + release: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + |
| 25 | + permissions: |
| 26 | + contents: write |
| 27 | + id-token: write |
| 28 | + |
| 29 | + steps: |
| 30 | + - name: Checkout code |
| 31 | + uses: actions/checkout@v4 |
| 32 | + with: |
| 33 | + fetch-depth: 0 |
| 34 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 35 | + |
| 36 | + - name: Setup Node.js |
| 37 | + uses: actions/setup-node@v4 |
| 38 | + with: |
| 39 | + node-version: "22" |
| 40 | + registry-url: "https://registry.npmjs.org" |
| 41 | + |
| 42 | + - name: Install dependencies |
| 43 | + run: npm ci |
| 44 | + |
| 45 | + - name: Run tests |
| 46 | + run: npm test |
| 47 | + |
| 48 | + - name: Run lint check |
| 49 | + run: npm run lint:check |
| 50 | + |
| 51 | + - name: Determine version |
| 52 | + id: version |
| 53 | + run: | |
| 54 | + CURRENT_VERSION=$(node -p "require('./package.json').version") |
| 55 | + |
| 56 | + if [ "${{ github.event.inputs.version_type }}" == "custom" ]; then |
| 57 | + NEW_VERSION="${{ github.event.inputs.custom_version }}" |
| 58 | + else |
| 59 | + NEW_VERSION=$(node -p " |
| 60 | + const v = '${CURRENT_VERSION}'.split('.'); |
| 61 | + const type = '${{ github.event.inputs.version_type }}'; |
| 62 | + if (type === 'major') { |
| 63 | + v[0] = parseInt(v[0]) + 1; |
| 64 | + v[1] = 0; |
| 65 | + v[2] = 0; |
| 66 | + } else if (type === 'minor') { |
| 67 | + v[1] = parseInt(v[1]) + 1; |
| 68 | + v[2] = 0; |
| 69 | + } else { |
| 70 | + v[2] = parseInt(v[2]) + 1; |
| 71 | + } |
| 72 | + v.join('.'); |
| 73 | + ") |
| 74 | + fi |
| 75 | + |
| 76 | + echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT |
| 77 | + echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 78 | + echo "Current version: $CURRENT_VERSION" |
| 79 | + echo "New version: $NEW_VERSION" |
| 80 | +
|
| 81 | + - name: Validate version |
| 82 | + run: | |
| 83 | + if [ -z "${{ steps.version.outputs.new_version }}" ]; then |
| 84 | + echo "Error: New version is empty" |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | + |
| 88 | + if [ "${{ steps.version.outputs.current_version }}" == "${{ steps.version.outputs.new_version }}" ]; then |
| 89 | + echo "Error: New version is the same as current version" |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | +
|
| 93 | + - name: Update package.json version |
| 94 | + run: | |
| 95 | + node -e " |
| 96 | + const fs = require('fs'); |
| 97 | + const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8')); |
| 98 | + pkg.version = '${{ steps.version.outputs.new_version }}'; |
| 99 | + fs.writeFileSync('package.json', JSON.stringify(pkg, null, 4) + '\n'); |
| 100 | + console.log('Updated package.json to version', pkg.version); |
| 101 | + " |
| 102 | +
|
| 103 | + - name: Generate changelog |
| 104 | + run: | |
| 105 | + NEW_VERSION="${{ steps.version.outputs.new_version }}" |
| 106 | + DATE=$(date +%Y-%m-%d) |
| 107 | + |
| 108 | + if [ -f CHANGELOG.md ]; then |
| 109 | + # Update existing changelog |
| 110 | + TEMP_FILE=$(mktemp) |
| 111 | + |
| 112 | + # Extract header (first 6 lines) |
| 113 | + head -n 6 CHANGELOG.md > "$TEMP_FILE" |
| 114 | + |
| 115 | + # Add new version entry |
| 116 | + echo "" >> "$TEMP_FILE" |
| 117 | + echo "## [$NEW_VERSION] - $DATE" >> "$TEMP_FILE" |
| 118 | + echo "" >> "$TEMP_FILE" |
| 119 | + echo "### Added" >> "$TEMP_FILE" |
| 120 | + echo "- Release version $NEW_VERSION" >> "$TEMP_FILE" |
| 121 | + echo "" >> "$TEMP_FILE" |
| 122 | + |
| 123 | + # Append rest of changelog (skip header) |
| 124 | + tail -n +7 CHANGELOG.md >> "$TEMP_FILE" |
| 125 | + |
| 126 | + # Add version link |
| 127 | + echo "" >> "$TEMP_FILE" |
| 128 | + echo "[$NEW_VERSION]: https://github.com/renderorange/types-class/releases/tag/v$NEW_VERSION" >> "$TEMP_FILE" |
| 129 | + |
| 130 | + mv "$TEMP_FILE" CHANGELOG.md |
| 131 | + else |
| 132 | + # Create new changelog |
| 133 | + cat > CHANGELOG.md << 'EOF' |
| 134 | + # Changelog |
| 135 | +
|
| 136 | + All notable changes to this project will be documented in this file. |
| 137 | +
|
| 138 | + The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), |
| 139 | + and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). |
| 140 | +
|
| 141 | + ## [1.0.0] - 2025-03-04 |
| 142 | +
|
| 143 | + ### Added |
| 144 | + - Initial release with runtime type checking base class |
| 145 | + - Basic types: isaPositiveInt, isaEmailAddress, isaString |
| 146 | + - Standard types: isaNumber, isaBoolean, isaDate, isaURL, isaArray, isaObject |
| 147 | + - Compound types: isaAnyOf, isaAllOf, isaNoneOf |
| 148 | + - Utilities: validate(), maybe() |
| 149 | +
|
| 150 | + [1.0.0]: https://github.com/renderorange/types-class/releases/tag/v1.0.0 |
| 151 | + EOF |
| 152 | + sed -i "s/## \[1.0.0\]/## [$NEW_VERSION] - $DATE\n\n### Added\n- Release version $NEW_VERSION\n\n## [1.0.0]/" CHANGELOG.md |
| 153 | + sed -i "s|/v1.0.0|/v$NEW_VERSION|" CHANGELOG.md |
| 154 | + fi |
| 155 | + |
| 156 | + echo "Changelog updated" |
| 157 | +
|
| 158 | + - name: Configure git |
| 159 | + run: | |
| 160 | + git config user.name "github-actions[bot]" |
| 161 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 162 | +
|
| 163 | + - name: Commit changes |
| 164 | + run: | |
| 165 | + git add package.json CHANGELOG.md |
| 166 | + git commit -m "chore(release): v${{ steps.version.outputs.new_version }}" |
| 167 | +
|
| 168 | + - name: Push to main |
| 169 | + run: git push origin main |
| 170 | + |
| 171 | + - name: Create and push tag |
| 172 | + run: | |
| 173 | + git tag -a "v${{ steps.version.outputs.new_version }}" -m "Release v${{ steps.version.outputs.new_version }}" |
| 174 | + git push origin "v${{ steps.version.outputs.new_version }}" |
| 175 | +
|
| 176 | + - name: Create GitHub Release |
| 177 | + uses: softprops/action-gh-release@v1 |
| 178 | + with: |
| 179 | + tag_name: "v${{ steps.version.outputs.new_version }}" |
| 180 | + name: "v${{ steps.version.outputs.new_version }}" |
| 181 | + generate_release_notes: true |
| 182 | + env: |
| 183 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 184 | + |
| 185 | + - name: Publish to npm |
| 186 | + run: npm publish --provenance --access public |
| 187 | + env: |
| 188 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
0 commit comments