Auto bump version #5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publish JS SDK | |
| on: | |
| push: | |
| branches: | |
| - main | |
| paths: | |
| - "**" | |
| - ".github/workflows/publish-npm.yml" | |
| workflow_dispatch: {} | |
| jobs: | |
| publish: | |
| if: ${{ github.actor != 'github-actions[bot]' }} | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| id-token: write | |
| steps: | |
| - name: Check out repo | |
| uses: actions/checkout@v4 | |
| - name: Set up Node | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: "24" | |
| registry-url: "https://registry.npmjs.org" | |
| - name: Upgrade npm for trusted publishing | |
| run: npm install -g npm@11.5.1 | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Decide version | |
| id: version | |
| run: | | |
| current=$(node -p "require('./package.json').version") | |
| published=$(npm view diffio version 2>/dev/null || true) | |
| echo "current=$current" | |
| echo "published=$published" | |
| CURRENT="$current" PUBLISHED="$published" node - <<'NODE' | |
| const fs = require("fs"); | |
| const current = process.env.CURRENT; | |
| const published = process.env.PUBLISHED; | |
| const parse = (value) => { | |
| const core = value.split("-")[0].split("+")[0]; | |
| const parts = core.split(".").map((item) => parseInt(item, 10)); | |
| while (parts.length < 3) { | |
| parts.push(0); | |
| } | |
| return parts.slice(0, 3); | |
| }; | |
| const compare = (left, right) => { | |
| const a = parse(left); | |
| const b = parse(right); | |
| for (let i = 0; i < 3; i += 1) { | |
| if (a[i] > b[i]) return 1; | |
| if (a[i] < b[i]) return -1; | |
| } | |
| return 0; | |
| }; | |
| const bumpPatch = (value) => { | |
| const [major, minor, patch] = parse(value); | |
| return `${major}.${minor}.${patch + 1}`; | |
| }; | |
| let nextVersion = current; | |
| let bumpNeeded = false; | |
| if (published) { | |
| const comparison = compare(current, published); | |
| if (comparison <= 0) { | |
| nextVersion = bumpPatch(comparison === 0 ? current : published); | |
| bumpNeeded = true; | |
| } | |
| } | |
| const publish = | |
| !published || (published && compare(nextVersion, published) > 0); | |
| const output = process.env.GITHUB_OUTPUT; | |
| if (output) { | |
| fs.appendFileSync(output, `next_version=${nextVersion}\n`); | |
| fs.appendFileSync( | |
| output, | |
| `bump_needed=${bumpNeeded ? "true" : "false"}\n` | |
| ); | |
| fs.appendFileSync( | |
| output, | |
| `publish=${publish ? "true" : "false"}\n` | |
| ); | |
| } | |
| NODE | |
| - name: Bump package.json version | |
| if: ${{ steps.version.outputs.bump_needed == 'true' }} | |
| run: npm version "${{ steps.version.outputs.next_version }}" --no-git-tag-version | |
| - name: Sync SDK version constant | |
| id: sync_version | |
| env: | |
| NEXT_VERSION: ${{ steps.version.outputs.next_version }} | |
| run: | | |
| node - <<'NODE' | |
| const fs = require("fs"); | |
| const path = "src/version.ts"; | |
| const nextVersion = process.env.NEXT_VERSION; | |
| if (!nextVersion) { | |
| process.exit(0); | |
| } | |
| const contents = fs.readFileSync(path, "utf8"); | |
| const updated = contents.replace( | |
| /DIFFIO_SDK_VERSION\s*=\s*["'][^"']+["']/, | |
| `DIFFIO_SDK_VERSION = "${nextVersion}"` | |
| ); | |
| if (updated !== contents) { | |
| fs.writeFileSync(path, updated); | |
| } | |
| NODE | |
| if git diff --quiet -- package.json package-lock.json src/version.ts; then | |
| echo "changed=false" >> "$GITHUB_OUTPUT" | |
| else | |
| echo "changed=true" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Commit version update | |
| if: ${{ steps.sync_version.outputs.changed == 'true' }} | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| git add package.json package-lock.json src/version.ts | |
| git commit -m "chore(release): bump JS SDK to ${{ steps.version.outputs.next_version }}" | |
| git push origin "HEAD:${GITHUB_REF_NAME}" | |
| - name: Build | |
| if: ${{ steps.version.outputs.publish == 'true' }} | |
| run: npm run build | |
| - name: Publish to npm | |
| if: ${{ steps.version.outputs.publish == 'true' }} | |
| run: npm publish --access public |