diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index 60159c2..c68a834 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -6,9 +6,8 @@ name: Publish Package to npmjs # The version is never edited by hand. GitVersion works it out from git (GitVersion.yml) and # scripts/version/updateVersion.sh stamps it into package.json: # release / tag 0.2.0 -> 0.2.0 (the tag wins) -# run from main -> a prerelease of the next version, e.g. 0.1.1-3 (3 commits after tag -# 0.1.0), published under the `next` dist-tag; a commit message saying -# "+semver: minor" / "+semver: major" raises the base to 0.2.0 / 1.0.0 +# run from main -> MAJOR.MINOR.(newest tag patch + pull requests merged since that tag), +# e.g. tag 0.1.0 + 4 merges -> 0.1.4 (scripts/version/versionRules.sh) # `version` input -> exactly that on: release: @@ -51,6 +50,23 @@ jobs: with: fetch-depth: 0 + # Publishing, dry runs included, is only allowed from main, dev or master. A manual run must + # be started on one of those branches; a release's tag must point at a commit on one of them. + - name: Only main, dev or master may publish + run: | + allowed='^(main|dev|master)$' + if [[ "$GITHUB_EVENT_NAME" == "release" ]]; then + branches="$(git branch -r --contains "$GITHUB_SHA" --format='%(refname:short)' | sed 's|^origin/||')" + echo "Tag $GITHUB_REF_NAME is on: $(echo $branches)" + if ! echo "$branches" | grep -Eq "$allowed"; then + echo "::error::Release tag $GITHUB_REF_NAME is not on main, dev or master." + exit 1 + fi + elif ! [[ "$GITHUB_REF_NAME" =~ $allowed ]]; then + echo "::error::Publishing only runs from main, dev or master (got '$GITHUB_REF_NAME')." + exit 1 + fi + - uses: actions/setup-node@v4 with: node-version: 24.x @@ -87,11 +103,6 @@ jobs: echo "version=$version" >> "$GITHUB_OUTPUT" echo "Publishing version: $version" - - name: Real publishes only come from a release or main - if: env.DRY_RUN != 'true' && github.event_name == 'workflow_dispatch' && github.ref != 'refs/heads/main' - run: | - echo "::error::Run this workflow from main (or tick dry_run). Got $GITHUB_REF." - exit 1 - run: npm run lint - run: npm test diff --git a/GitVersion.yml b/GitVersion.yml index 87309c9..0724ca4 100644 --- a/GitVersion.yml +++ b/GitVersion.yml @@ -6,7 +6,7 @@ # - tag it: git tag v0.2.0 && git push --tags # - commit message: "... +semver: minor" (or +semver: major) # (commit-message-incrementing is enabled) -# feature branch / PR -> -