|
| 1 | +name: release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +concurrency: |
| 9 | + group: ${{ github.workflow }}-main |
| 10 | + cancel-in-progress: false |
| 11 | + |
| 12 | +jobs: |
| 13 | + release: |
| 14 | + name: Release |
| 15 | + runs-on: ubuntu-latest |
| 16 | + permissions: |
| 17 | + contents: write |
| 18 | + id-token: write |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: Checkout |
| 22 | + uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + fetch-depth: 0 |
| 25 | + fetch-tags: true |
| 26 | + |
| 27 | + - name: Setup Node.js |
| 28 | + uses: actions/setup-node@v4 |
| 29 | + with: |
| 30 | + node-version: 24.x |
| 31 | + cache: npm |
| 32 | + |
| 33 | + - name: Install dependencies |
| 34 | + run: npm ci |
| 35 | + |
| 36 | + - name: Get package.json version |
| 37 | + id: pkg_version |
| 38 | + run: | |
| 39 | + VERSION=$(node -p "require('./package.json').version") |
| 40 | + echo "version=${VERSION}" >> $GITHUB_OUTPUT |
| 41 | + echo "tag=v${VERSION}" >> $GITHUB_OUTPUT |
| 42 | + echo "Package version: ${VERSION}" |
| 43 | +
|
| 44 | + - name: Get latest git tag |
| 45 | + id: latest_tag |
| 46 | + run: | |
| 47 | + git fetch --tags origin |
| 48 | + LATEST_TAG=$(git tag --sort=-version:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+' | head -1 || echo "") |
| 49 | + echo "tag=${LATEST_TAG}" >> $GITHUB_OUTPUT |
| 50 | + echo "Latest tag: ${LATEST_TAG:-none}" |
| 51 | +
|
| 52 | + - name: Check if release is needed |
| 53 | + id: should_release |
| 54 | + run: | |
| 55 | + PKG_VERSION="${{ steps.pkg_version.outputs.version }}" |
| 56 | + LATEST_TAG="${{ steps.latest_tag.outputs.tag }}" |
| 57 | +
|
| 58 | + if [ -z "$LATEST_TAG" ]; then |
| 59 | + echo "No existing tags found, proceeding with first release" |
| 60 | + echo "needed=true" >> $GITHUB_OUTPUT |
| 61 | + exit 0 |
| 62 | + fi |
| 63 | +
|
| 64 | + LATEST_VERSION="${LATEST_TAG#v}" |
| 65 | +
|
| 66 | + IS_GREATER=$(node -e " |
| 67 | + const { gt, valid } = require('semver'); |
| 68 | + const pkg = '${PKG_VERSION}'; |
| 69 | + const latest = '${LATEST_VERSION}'; |
| 70 | + if (!valid(pkg) || !valid(latest)) { |
| 71 | + console.error('Invalid semver: pkg=' + pkg + ' latest=' + latest); |
| 72 | + process.exit(1); |
| 73 | + } |
| 74 | + console.log(gt(pkg, latest) ? 'true' : 'false'); |
| 75 | + ") |
| 76 | +
|
| 77 | + echo "needed=${IS_GREATER}" >> $GITHUB_OUTPUT |
| 78 | +
|
| 79 | + if [ "$IS_GREATER" = "true" ]; then |
| 80 | + echo "Version ${PKG_VERSION} > ${LATEST_VERSION}, release needed" |
| 81 | + else |
| 82 | + echo "Version ${PKG_VERSION} <= ${LATEST_VERSION}, skipping release" |
| 83 | + fi |
| 84 | +
|
| 85 | + - name: Validate version format |
| 86 | + if: steps.should_release.outputs.needed == 'true' |
| 87 | + env: |
| 88 | + TAG_NAME: ${{ steps.pkg_version.outputs.tag }} |
| 89 | + run: | |
| 90 | + if ! printf "%s\n" "$TAG_NAME" | grep -qP '^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-(alpha|beta|rc)\.(0|[1-9][0-9]*))?$'; then |
| 91 | + printf '[ERROR]: Invalid version tag format (%s)\n' "$TAG_NAME" |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | + echo "Version tag format is valid: $TAG_NAME" |
| 95 | +
|
| 96 | + - name: Determine if prerelease |
| 97 | + if: steps.should_release.outputs.needed == 'true' |
| 98 | + id: prerelease |
| 99 | + env: |
| 100 | + TAG_NAME: ${{ steps.pkg_version.outputs.tag }} |
| 101 | + run: | |
| 102 | + if printf "%s\n" "$TAG_NAME" | grep -qP '^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$'; then |
| 103 | + echo "value=false" >> $GITHUB_OUTPUT |
| 104 | + else |
| 105 | + echo "value=true" >> $GITHUB_OUTPUT |
| 106 | + fi |
| 107 | +
|
| 108 | + - name: Collect changelog |
| 109 | + if: steps.should_release.outputs.needed == 'true' |
| 110 | + id: changelog |
| 111 | + run: | |
| 112 | + LATEST_TAG="${{ steps.latest_tag.outputs.tag }}" |
| 113 | +
|
| 114 | + if [ -z "$LATEST_TAG" ]; then |
| 115 | + git log HEAD --pretty=format:"- %s" --no-merges > /tmp/commits.txt |
| 116 | + else |
| 117 | + git log ${LATEST_TAG}..HEAD --pretty=format:"- %s" --no-merges > /tmp/commits.txt |
| 118 | + fi |
| 119 | +
|
| 120 | + COMMITS_B64=$(base64 -w 0 /tmp/commits.txt) |
| 121 | + echo "commits_b64=${COMMITS_B64}" >> $GITHUB_OUTPUT |
| 122 | +
|
| 123 | + - name: Configure git |
| 124 | + if: steps.should_release.outputs.needed == 'true' |
| 125 | + run: | |
| 126 | + git config --local user.email "action@github.com" |
| 127 | + git config --local user.name "GitHub Action" |
| 128 | +
|
| 129 | + - name: Create and push tag |
| 130 | + if: steps.should_release.outputs.needed == 'true' |
| 131 | + run: | |
| 132 | + TAG="${{ steps.pkg_version.outputs.tag }}" |
| 133 | + COMMIT_MESSAGES=$(echo "${{ steps.changelog.outputs.commits_b64 }}" | base64 -d) |
| 134 | + TAG_MESSAGE=$(printf "Release %s\n\nChanges in this release:\n%s" "${TAG}" "${COMMIT_MESSAGES}") |
| 135 | +
|
| 136 | + git tag -a "${TAG}" -m "${TAG_MESSAGE}" |
| 137 | + git push origin "${TAG}" |
| 138 | +
|
| 139 | + - name: Create GitHub Release |
| 140 | + if: steps.should_release.outputs.needed == 'true' |
| 141 | + env: |
| 142 | + GH_TOKEN: ${{ github.token }} |
| 143 | + COMMITS_B64: ${{ steps.changelog.outputs.commits_b64 }} |
| 144 | + run: | |
| 145 | + TAG="${{ steps.pkg_version.outputs.tag }}" |
| 146 | + COMMIT_MESSAGES=$(echo "$COMMITS_B64" | base64 -d) |
| 147 | + RELEASE_BODY=$(printf "## Changes in this release:\n\n%s" "${COMMIT_MESSAGES}") |
| 148 | +
|
| 149 | + PRERELEASE_FLAG="" |
| 150 | + if [ "${{ steps.prerelease.outputs.value }}" = "true" ]; then |
| 151 | + PRERELEASE_FLAG="--prerelease" |
| 152 | + fi |
| 153 | +
|
| 154 | + gh release create "${TAG}" \ |
| 155 | + --title "${TAG}" \ |
| 156 | + --notes "${RELEASE_BODY}" \ |
| 157 | + ${PRERELEASE_FLAG} |
| 158 | +
|
| 159 | + - name: Setup Node.js for publishing |
| 160 | + if: steps.should_release.outputs.needed == 'true' |
| 161 | + uses: actions/setup-node@v4 |
| 162 | + with: |
| 163 | + node-version: 24.x |
| 164 | + registry-url: https://registry.npmjs.org |
| 165 | + |
| 166 | + - name: Build package |
| 167 | + if: steps.should_release.outputs.needed == 'true' |
| 168 | + run: npm run build |
| 169 | + |
| 170 | + - name: Publish to npm |
| 171 | + if: steps.should_release.outputs.needed == 'true' |
| 172 | + env: |
| 173 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} |
| 174 | + run: | |
| 175 | + NPM_TAG="latest" |
| 176 | + if [ "${{ steps.prerelease.outputs.value }}" = "true" ]; then |
| 177 | + NPM_TAG="next" |
| 178 | + fi |
| 179 | +
|
| 180 | + npm publish --ignore-scripts --access public --tag "$NPM_TAG" |
| 181 | +
|
| 182 | + - name: Summary |
| 183 | + if: steps.should_release.outputs.needed == 'true' |
| 184 | + run: | |
| 185 | + echo "## Release Summary" >> $GITHUB_STEP_SUMMARY |
| 186 | + echo "- **Version**: ${{ steps.pkg_version.outputs.tag }}" >> $GITHUB_STEP_SUMMARY |
| 187 | + echo "- **Prerelease**: ${{ steps.prerelease.outputs.value }}" >> $GITHUB_STEP_SUMMARY |
| 188 | + echo "- **npm tag**: ${{ steps.prerelease.outputs.value == 'true' && 'next' || 'latest' }}" >> $GITHUB_STEP_SUMMARY |
| 189 | +
|
| 190 | + - name: No release needed |
| 191 | + if: steps.should_release.outputs.needed != 'true' |
| 192 | + run: | |
| 193 | + echo "## No Release" >> $GITHUB_STEP_SUMMARY |
| 194 | + echo "- **Package version**: ${{ steps.pkg_version.outputs.version }}" >> $GITHUB_STEP_SUMMARY |
| 195 | + echo "- **Latest tag**: ${{ steps.latest_tag.outputs.tag || 'none' }}" >> $GITHUB_STEP_SUMMARY |
| 196 | + echo "- **Reason**: Version not greater than latest tag, skipping release" >> $GITHUB_STEP_SUMMARY |
0 commit comments