Skip to content

Commit 8fdcceb

Browse files
committed
chore: ci upgrades
1 parent b303a62 commit 8fdcceb

File tree

2 files changed

+150
-36
lines changed

2 files changed

+150
-36
lines changed

.github/workflows/publish.yaml

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,90 @@
1-
name: Publish to NPM
1+
name: Publish NPM and Release (on merged PR)
22

33
on:
4-
workflow_dispatch:
5-
inputs:
6-
semver:
7-
description: "The semantic version to bump"
8-
required: true
9-
type: choice
10-
options:
11-
- patch
12-
- minor
13-
- major
14-
default: "patch"
15-
nodeVersion:
16-
description: "The Node.js version to use"
17-
required: true
18-
type: choice
19-
options:
20-
- "18.x"
21-
- "20.x"
22-
- "22.x"
23-
default: "18.x"
4+
pull_request:
5+
types:
6+
- closed
247

258
jobs:
269
release:
10+
if: ${{ github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'master' }}
2711
permissions:
2812
contents: write
13+
packages: write
2914
id-token: write
3015

31-
# Use the semver as the job name
32-
name: "Release ${{ github.event.inputs.semver }}"
16+
name: Publish on master (patch)
3317
runs-on: ubuntu-latest
18+
concurrency:
19+
group: publish-pr-${{ github.event.pull_request.number }}
20+
cancel-in-progress: true
3421
steps:
3522
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
23+
with:
24+
fetch-depth: 0
25+
persist-credentials: true
3626

3727
- uses: actions/setup-node@3235b876344d2a9aa001b8d1453c930bba69e610
3828
with:
39-
node-version: ${{ github.event.inputs.nodeVersion }}
40-
registry-url: "https://registry.npmjs.org"
29+
node-version: '22'
30+
registry-url: 'https://registry.npmjs.org'
31+
32+
- name: Install dependencies
33+
run: npm ci
34+
35+
- name: Run tests
36+
run: npm test
4137

42-
- name: Bump and commit version
38+
- name: Detect version bump in merged PR
39+
id: detect
4340
run: |
44-
git config --global user.email "github-actions[bot]@github.com"
45-
git config --global user.name "github-actions[bot]"
46-
npm version ${{ github.event.inputs.semver }} --message "chore(release): bump version to %s"
47-
git push --follow-tags
41+
set -euo pipefail
42+
VERSION=$(node -p "require('./package.json').version")
43+
PREV_JSON=$(git show HEAD^:package.json 2>/dev/null || true)
44+
if [ -z "$PREV_JSON" ]; then
45+
echo "No previous package.json found; treating as release"
46+
echo "release=true" >> $GITHUB_OUTPUT
47+
else
48+
PREV_VERSION=$(printf "%s" "$PREV_JSON" | node -e "let s=''; process.stdin.on('data',d=>s+=d); process.stdin.on('end',()=>console.log(JSON.parse(s).version));")
49+
echo "current: $VERSION, previous: $PREV_VERSION"
50+
if [ "$VERSION" != "$PREV_VERSION" ]; then
51+
echo "release=true" >> $GITHUB_OUTPUT
52+
else
53+
echo "release=false" >> $GITHUB_OUTPUT
54+
fi
55+
fi
56+
echo "VERSION=$VERSION" >> $GITHUB_ENV
4857
49-
- name: Publish
50-
run: npm publish
58+
- name: Create annotated tag from package.json and push
59+
if: ${{ steps.detect.outputs.release == 'true' }}
60+
env:
61+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
62+
run: |
63+
set -euo pipefail
64+
git config user.email "github-actions[bot]@github.com"
65+
git config user.name "github-actions[bot]"
5166
52-
- name: Set version as env var
67+
TAG="v${VERSION}"
68+
echo "Creating annotated tag $TAG from current HEAD"
69+
git tag -a "$TAG" -m "chore(release): $TAG"
70+
git push origin "$TAG"
71+
72+
- name: Publish to npm (OIDC)
73+
if: ${{ steps.detect.outputs.release == 'true' }}
5374
run: |
54-
echo "VERSION=$(node -p "require('./package.json').version")" >> $GITHUB_ENV
75+
npm publish
76+
77+
- name: Set version as env var
78+
run: echo "VERSION=$(node -p \"require('./package.json').version\")" >> $GITHUB_ENV
5579

56-
- uses: softprops/action-gh-release@aec2ec56f94eb8180ceec724245f64ef008b89f5
57-
name: Release
80+
- name: Create GitHub release
81+
if: ${{ steps.detect.outputs.release == 'true' }}
82+
uses: softprops/action-gh-release@aec2ec56f94eb8180ceec724245f64ef008b89f5
5883
with:
84+
tag_name: v${{ env.VERSION }}
5985
name: Release ${{ env.VERSION }}
86+
87+
- name: No version bump detected — skipping publish
88+
if: ${{ steps.detect.outputs.release != 'true' }}
89+
run: |
90+
echo "No package.json version bump detected in merged PR; skipping tagging and publish."
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: Update Version
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
semver:
7+
description: "The semantic version to bump"
8+
required: true
9+
type: choice
10+
options:
11+
- patch
12+
- minor
13+
- major
14+
default: "patch"
15+
nodeVersion:
16+
description: "The Node.js version to use"
17+
required: true
18+
type: choice
19+
options:
20+
- "18.x"
21+
- "20.x"
22+
- "22.x"
23+
default: "18.x"
24+
25+
jobs:
26+
release:
27+
permissions:
28+
contents: write
29+
pull-requests: read
30+
31+
name: Update and publish version ${{ github.event.inputs.semver }}
32+
runs-on: ubuntu-latest
33+
steps:
34+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
35+
with:
36+
fetch-depth: 0
37+
persist-credentials: true
38+
39+
- uses: actions/setup-node@3235b876344d2a9aa001b8d1453c930bba69e610
40+
with:
41+
node-version: ${{ github.event.inputs.nodeVersion }}
42+
registry-url: "https://registry.npmjs.org"
43+
44+
- name: Create release branch, bump version and push
45+
env:
46+
SEMVER: ${{ github.event.inputs.semver }}
47+
run: |
48+
set -euo pipefail
49+
git config --global user.email "github-actions[bot]@github.com"
50+
git config --global user.name "github-actions[bot]"
51+
52+
# Create a unique release branch so the bump commit and tag are isolated
53+
BRANCH="release-${SEMVER}-$(date +%s)"
54+
git checkout -b "$BRANCH"
55+
56+
57+
# Bump version but do NOT create a git tag on the branch
58+
npm version "$SEMVER" --no-git-tag-version --allow-same-version --message "chore(release): bump version to %s"
59+
60+
# Commit package.json and package-lock.json (if present) and push branch
61+
git add package.json package-lock.json || true
62+
git commit -m "chore(release): bump version" || true
63+
git push --set-upstream origin "$BRANCH"
64+
65+
# Read the new version for PR title/body and persist values for next step
66+
VERSION=$(node -p "require('./package.json').version")
67+
echo "BRANCH=$BRANCH" >> $GITHUB_ENV
68+
echo "VERSION=$VERSION" >> $GITHUB_ENV
69+
70+
- name: Provide PR creation link
71+
env:
72+
GITHUB_REPOSITORY: ${{ github.repository }}
73+
run: |
74+
set -euo pipefail
75+
PR_LINK="https://github.com/${GITHUB_REPOSITORY}/compare/master...${BRANCH}?expand=1"
76+
echo "Open this URL to create the PR: ${PR_LINK}"
77+
{
78+
echo "## Version bump branch created";
79+
echo "Branch: ${BRANCH}";
80+
echo "Version: ${VERSION}";
81+
echo "";
82+
echo "[Create the pull request now](${PR_LINK})";
83+
} >> "$GITHUB_STEP_SUMMARY"

0 commit comments

Comments
 (0)