Skip to content

Commit 9a23b60

Browse files
committed
chore: ci upgrades
1 parent b303a62 commit 9a23b60

File tree

2 files changed

+134
-36
lines changed

2 files changed

+134
-36
lines changed

.github/workflows/publish.yaml

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,73 @@
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: Compute patch version, update package.json locally, tag and push
39+
env:
40+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4341
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
42+
set -euo pipefail
43+
git config user.email "github-actions[bot]@github.com"
44+
git config user.name "github-actions[bot]"
4845
49-
- name: Publish
50-
run: npm publish
46+
# Read current version
47+
CURR=$(node -p "require('./package.json').version")
48+
echo "Current version: $CURR"
5149
52-
- name: Set version as env var
50+
# Bump package.json locally without creating a git tag or commit
51+
npm version patch --no-git-tag-version -m "chore(release): bump version to %s"
52+
53+
# New version from updated package.json
54+
VERSION=$(node -p "require('./package.json').version")
55+
TAG="v${VERSION}"
56+
echo "Bumped to version: $VERSION"
57+
58+
# Create annotated tag from HEAD and push only the tag (do not push commits to protected branch)
59+
git tag -a "$TAG" -m "chore(release): $TAG"
60+
git push origin "$TAG"
61+
62+
- name: Publish to npm (OIDC)
5363
run: |
54-
echo "VERSION=$(node -p "require('./package.json').version")" >> $GITHUB_ENV
64+
npm publish
65+
66+
- name: Set version as env var
67+
run: echo "VERSION=$(node -p \"require('./package.json').version\")" >> $GITHUB_ENV
5568

56-
- uses: softprops/action-gh-release@aec2ec56f94eb8180ceec724245f64ef008b89f5
57-
name: Release
69+
- name: Create GitHub release
70+
uses: softprops/action-gh-release@aec2ec56f94eb8180ceec724245f64ef008b89f5
5871
with:
72+
tag_name: v${{ env.VERSION }}
5973
name: Release ${{ env.VERSION }}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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: write
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: Create PR with gh
71+
env:
72+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
73+
GITHUB_REPOSITORY: ${{ github.repository }}
74+
SEMVER: ${{ github.event.inputs.semver }}
75+
run: |
76+
set -euo pipefail
77+
PR_TITLE="chore(release): bump version to ${VERSION}"
78+
PR_BODY="Automated version bump to ${VERSION} (triggered by workflow_dispatch)."
79+
80+
# Write body to a temp file to preserve newlines
81+
printf "%s\n\nSemantic bump: %s\n" "$PR_BODY" "$SEMVER" > /tmp/pr_body.md
82+
83+
echo "Creating PR: ${PR_TITLE} from ${BRANCH} into master"
84+
gh pr create --title "$PR_TITLE" --body-file /tmp/pr_body.md --base master --head "$BRANCH" --repo "$GITHUB_REPOSITORY"

0 commit comments

Comments
 (0)