diff --git a/.github/workflows/release.yml b/.github/workflows/publish.yml similarity index 55% rename from .github/workflows/release.yml rename to .github/workflows/publish.yml index d67aa0e..2039ae2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/publish.yml @@ -1,28 +1,44 @@ -name: Release +name: Publish on: - release: - types: [published] + push: + branches: + - main + paths: + - 'package.json' permissions: contents: read id-token: write jobs: - publish: + version-check: runs-on: ubuntu-latest - environment: npm + outputs: + version_changed: ${{ steps.check.outputs.changed }} + version: ${{ steps.check.outputs.version }} steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 - - name: Validate version + - name: Detect package.json version bump + id: check run: | - TAG_VERSION="${GITHUB_REF_NAME#v}" - PKG_VERSION=$(node -p "require('./package.json').version") - if [ "$TAG_VERSION" != "$PKG_VERSION" ]; then - echo "::error::Tag version ($TAG_VERSION) does not match package.json version ($PKG_VERSION)" - exit 1 - fi + set -euo pipefail + NEW=$(node -p "require('./package.json').version") + echo "version=${NEW}" >> "$GITHUB_OUTPUT" + BEFORE="${{ github.event.before }}" + OLD=$(git show "${BEFORE}:package.json" 2>/dev/null | jq -r .version 2>/dev/null) || OLD="" + echo "changed=$([[ "$BEFORE" =~ ^0{40}$ || "$OLD" != "$NEW" ]] && echo true || echo false)" >> "$GITHUB_OUTPUT" + + publish: + needs: version-check + if: needs.version-check.outputs.version_changed == 'true' + runs-on: ubuntu-latest + environment: npm + steps: + - uses: actions/checkout@v4 - name: Setup Node.js uses: actions/setup-node@v4 @@ -40,7 +56,7 @@ jobs: # Runs after npm publish; failures here do not affect the published package (re-run this job only if needed). notify-vscode-motoko: - needs: publish + needs: [publish, version-check] runs-on: ubuntu-latest steps: - name: Generate GitHub App token @@ -59,4 +75,4 @@ jobs: token: ${{ steps.dispatch-token.outputs.token }} repository: caffeinelabs/vscode-motoko event-type: motoko-release - client-payload: '{"version":"${{ github.event.release.tag_name }}"}' + client-payload: '{"version":"v${{ needs.version-check.outputs.version }}"}' diff --git a/RELEASING.md b/RELEASING.md index 5b68951..cb87c5e 100644 --- a/RELEASING.md +++ b/RELEASING.md @@ -21,18 +21,16 @@ with optional `moc_version` and `core_version` inputs. Leave them empty to auto- ## Publishing to npm -After merging a moc update (or any version bump), create a **GitHub Release**: +After merging a moc update (or any version bump), bump the **`version`** field in `package.json` and merge to `main`. -1. Go to [Releases](https://github.com/caffeinelabs/node-motoko/releases/new) -2. Create a new tag matching the `package.json` version (e.g. `v4.1.0`) -3. Click "Publish release" +The [`release`](.github/workflows/release.yml) workflow runs when `package.json` changes on `main`. If the **version** field changed compared to the previous commit on `main`, it will: -The [`release`](.github/workflows/release.yml) workflow will automatically: -- Validate the tag matches `package.json` - Build and test - Publish to npm via [OIDC trusted publishing](https://docs.npmjs.com/trusted-publishers) (no tokens needed) - Notify [vscode-motoko](https://github.com/caffeinelabs/vscode-motoko) to open a PR bumping the `motoko` dependency (GitHub App must include the `vscode-motoko` repo; same app as `update-moc`) +Updating the version in `package.json` automatically publishes the package when merged into `main`. + ## Local development (generate) To regenerate files locally against a specific version: