Skip to content

Commit c8d5dd7

Browse files
committed
add semver
1 parent c989a8c commit c8d5dd7

File tree

4 files changed

+74
-45
lines changed

4 files changed

+74
-45
lines changed

.github/workflows/publish-release.yaml

Lines changed: 66 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
name: Build And Publish Package Update
2+
name: Publish New Package Version
33

44
on:
55
workflow_dispatch:
@@ -12,6 +12,16 @@ on:
1212
- addresses
1313
- indexer
1414
- mint-manager
15+
version:
16+
type: string
17+
description: "version"
18+
publish:
19+
type: boolean
20+
description: Publish to npm
21+
default: false
22+
type: string
23+
description: "Changelog (required if publishing)"
24+
default: ""
1525

1626
jobs:
1727
build-and-publish:
@@ -30,71 +40,89 @@ jobs:
3040
node-version: 24
3141
registry-url: 'https://registry.npmjs.org/'
3242

33-
# - name: Authenticate with NmpJS
34-
# run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc
35-
36-
- name: Install dependencies (root)
37-
run: npm install
38-
39-
- name: Build selected package
43+
- name: Validate version format and uniqueness
4044
working-directory: packages/${{ inputs.package }}
41-
run: npm run build
45+
id: validate-version
46+
env:
47+
INPUT_VERSION: ${{ inputs.version }}
48+
run: |
49+
echo "Validating version: $INPUT_VERSION"
4250
51+
# Ensure semver format
52+
if [[ ! "$INPUT_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.-]+)?$ ]]; then
53+
echo "❌ Invalid version format. Must follow semantic versioning (e.g., 1.2.3 or 1.2.3-suffix)"
54+
exit 1
55+
fi
4356
44-
- name: Determine new version
45-
id: versioning
46-
run: |
47-
PACKAGE="${{ inputs.package }}"
57+
# Extract base semver (strip suffix if present)
58+
BASE_VERSION=$(echo "$INPUT_VERSION" | grep -oE '^[0-9]+\.[0-9]+\.[0-9]+')
4859
49-
LATEST_TAG=$(git tag --list "*-${PACKAGE}" --sort=-v:refname | head -n 1)
60+
# Read current version from package.json
61+
CURRENT_VERSION=$(grep '"version":' package.json | head -1 | sed -E 's/.*"([0-9]+\.[0-9]+\.[0-9]+).*"/\1/')
5062
51-
52-
if [ -z "$LATEST_TAG" ]; then
53-
echo "No version found"
54-
NEW_VERSION="1.0.0-$PACKAGE"
55-
else
56-
echo "Version found: $LATEST_TAG"
57-
BASE_VERSION=${LATEST_TAG%-${PACKAGE}}
63+
echo "📦 Current: $CURRENT_VERSION"
64+
echo "🚀 New: $BASE_VERSION"
5865
59-
IFS='.' read -r MAJOR MINOR PATCH <<< "$BASE_VERSION"
60-
PATCH=$((PATCH + 1))
61-
NEW_VERSION="${MAJOR}.${MINOR}.${PATCH}-$PACKAGE"
66+
npx --yes semver "$BASE_VERSION" -gt "$CURRENT_VERSION"
67+
if [[ $? -ne 0 ]]; then
68+
echo "❌ Provided version ($BASE_VERSION) must be greater than current version ($CURRENT_VERSION)"
69+
exit 1
6270
fi
6371
64-
echo "New version: $NEW_VERSION"
72+
echo "✅ Version is valid and properly incremented."
73+
74+
- name: Install dependencies
75+
run: npm install
76+
77+
- name: Build selected package
78+
working-directory: packages/${{ inputs.package }}
79+
run: npm run build
6580

66-
# Set the version as output
67-
echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
6881

6982
- name: Update package version in package.json
7083
working-directory: packages/${{ inputs.package }}
71-
env:
72-
version: ${{ steps.versioning.outputs.version }}
7384
run: |
74-
PURE_VERSION=${version%-${package}}
75-
76-
# Show for debug
77-
echo "Original version: $version"
78-
echo "Pure version for npm: $PURE_VERSION"
85+
npm version ${{ inputs.version }} --no-git-tag-version
7986
80-
# Run npm version with the pure semver
81-
npm version $PURE_VERSION --no-git-tag-version
87+
- name: Validate changelog if publishing
88+
if: ${{ inputs.publish == true }}
89+
run: |
90+
if [ -z "${{ inputs.changelog }}" ]; then
91+
echo "❌ Changelog is required when publishing."
92+
exit 1
93+
fi
8294
8395
- name: Publish package to npm registry
96+
if: ${{ inputs.publish == true }}
8497
working-directory: packages/${{ inputs.package }}
8598
run: npm publish --provenance --access public
8699
env:
87100
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
88101

89102
- name: Commit and push version bump and tag
103+
if: ${{ inputs.publish == true }}
90104
env:
91-
version: ${{ steps.versioning.outputs.version }}
92-
package: ${{ inputs.package }}
105+
VERSION: ${{ inputs.version }}
106+
CHANGELOG: ${{ inputs.changelog }}
107+
PACKAGE: ${{ inputs.package }}
93108
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
94109
run: |
95110
git config user.name "github-actions[bot]"
96111
git config user.email "github-actions[bot]@users.noreply.github.com"
97112
113+
echo "📝 Prepending changelog for $VERSION"
114+
115+
FILE="changelog/${PACKAGE}-changelog.md"
116+
TEMP_FILE="${FILE}.tmp"
117+
118+
echo -e "${VERSION} - ${CHANGELOG}\n" > "$TEMP_FILE"
119+
120+
if [ -f "$FILE" ]; then
121+
cat "$FILE" >> "$TEMP_FILE"
122+
fi
123+
124+
mv "$TEMP_FILE" "$FILE"
125+
98126
# Commit the updated package.json with new version
99127
git add packages/$package/package.json
100128
git commit -m "chore($package): bump version to $version"

changelog/addresses-changelog.md

Whitespace-only changes.

package-lock.json

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"workspaces": [
88
"packages/*"
99
],
10-
"dependencies": {},
1110
"devDependencies": {
12-
"lerna": "^8.1.9"
11+
"lerna": "^8.1.9",
12+
"semver": "^7.7.2"
1313
}
1414
}

0 commit comments

Comments
 (0)