diff --git a/.github/scripts/npm-publish.sh b/.github/scripts/npm-publish.sh new file mode 100755 index 0000000..c032745 --- /dev/null +++ b/.github/scripts/npm-publish.sh @@ -0,0 +1,157 @@ +#!/usr/bin/env bash +set -euo pipefail + +CURRENT_VERSION=$(node -p "require('./package.json').version") +BASE_VERSION="${CURRENT_VERSION%%-*}" +IFS='.' read -r MAJOR MINOR PATCH <<< "$BASE_VERSION" + +if [[ -z "${MAJOR:-}" || -z "${MINOR:-}" || -z "${PATCH:-}" ]]; then + echo "" + echo " ERROR: Invalid version: $CURRENT_VERSION" + echo " Expected valid semver (e.g. 1.2.3)" + echo "" + exit 1 +fi + +if [[ "$BRANCH_NAME" == "main" ]]; then + if [[ -z "${INPUT_VERSION:-}" ]]; then + echo "" + echo " ERROR: Version input is required on the main branch" + echo " Please provide a version (e.g. 1.2.3)" + echo "" + exit 1 + fi + FINAL_VERSION="$INPUT_VERSION" + DIST_TAG="latest" +else + FINAL_VERSION="${MAJOR}.${MINOR}.$((PATCH + 1))-alpha.$(date +%s)" + DIST_TAG="alpha" +fi + +TAG_NAME="v${FINAL_VERSION}" + +echo "" +echo " ========================================" +echo " Release Summary" +echo " ========================================" +echo " Branch: $BRANCH_NAME" +echo " Current version: $CURRENT_VERSION" +echo " New version: $FINAL_VERSION" +echo " Dist-tag: $DIST_TAG" +echo " Git tag: $TAG_NAME" +echo " ========================================" +echo "" + +echo " Writing version $FINAL_VERSION to package.json ..." + +export FINAL_VERSION +node -e " + const fs = require('fs'); + const path = './package.json'; + const pkg = JSON.parse(fs.readFileSync(path, 'utf8')); + pkg.version = process.env.FINAL_VERSION; + fs.writeFileSync(path, JSON.stringify(pkg, null, 2) + '\n'); +" + +echo " Version stamped!" + +TICKET=$(echo "$BRANCH_NAME" | tr '[:lower:]' '[:upper:]' | grep -Eo '[A-Z]+-[0-9]+' | head -n1 || true) +TICKET="${TICKET:-MOON-000}" + +echo "" +echo " Committing version bump ($TICKET) ..." + +git config user.name "github-actions[bot]" +git config user.email "github-actions[bot]@users.noreply.github.com" +git add package.json +git commit -m "chore($TICKET): release @moonit/sdk-evm v$FINAL_VERSION" + +echo " Committed!" + +echo "" +echo " Tagging release $TAG_NAME ..." + +git tag "$TAG_NAME" + +echo " Tagged $TAG_NAME!" + +echo "" +echo " Publishing @moonit/sdk-evm@$FINAL_VERSION to npm (tag: $DIST_TAG) ..." + +# Trusted Publishers on GHA -- no NPM_TOKEN needed. +npm publish --tag "$DIST_TAG" --access public + +echo "" +echo " Published @moonit/sdk-evm@$FINAL_VERSION -> npm ($DIST_TAG)!" + +if [[ "$BRANCH_NAME" == "main" ]]; then + echo "" + echo " Pushing version bump via PR to $BRANCH_NAME ..." + + BUMP_BRANCH="chore/version-bump-sdk-evm-v${FINAL_VERSION}" + git checkout -b "$BUMP_BRANCH" + git push -u origin "$BUMP_BRANCH" + git push origin "$TAG_NAME" + + echo " Pushed branch $BUMP_BRANCH!" + echo "" + echo " Creating PR to merge into $BRANCH_NAME ..." + + PR_URL=$(gh pr create \ + --base "$BRANCH_NAME" \ + --head "$BUMP_BRANCH" \ + --title "chore($TICKET): bump @moonit/sdk-evm to v$FINAL_VERSION" \ + --body "$(cat < Merge when ready. No code changes, only \`package.json\` version. +EOF +)") + + echo " PR created: $PR_URL" + + echo "" + echo " Creating GitHub Release $TAG_NAME ..." + + gh release create "$TAG_NAME" \ + --title "@moonit/sdk-evm v$FINAL_VERSION" \ + --generate-notes + + echo " GitHub Release created!" +else + echo "" + echo " Pushing version bump to $BRANCH_NAME ..." + + git push origin HEAD:"$BRANCH_NAME" + git push origin "$TAG_NAME" + + echo "" + echo " Creating GitHub pre-release $TAG_NAME ..." + + gh release create "$TAG_NAME" \ + --title "@moonit/sdk-evm v$FINAL_VERSION" \ + --prerelease \ + --generate-notes + + echo " GitHub pre-release created!" +fi + +echo "" +echo " ========================================" +echo " ALL DONE" +echo " ========================================" +echo "" + +if [[ -n "${PR_URL:-}" ]]; then + echo " Merge the version bump PR: $PR_URL" +fi diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 3841816..2d6dba2 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -23,29 +23,8 @@ jobs: - name: Run linting run: yarn lint - test: - needs: lint - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - - name: Setup Node.js - uses: actions/setup-node@v4 - with: - node-version: '18' - cache: 'yarn' - - - name: Install dependencies - run: yarn install --frozen-lockfile - - - name: Run tests - env: - RPC_URL: ${{ secrets.RPC_URL }} - TEST_DEV_WALLET: ${{ secrets.TEST_DEV_WALLET }} - run: yarn test - build: - needs: test + needs: lint runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml new file mode 100644 index 0000000..31808c8 --- /dev/null +++ b/.github/workflows/npm-publish.yml @@ -0,0 +1,44 @@ +name: NPM Publish + +on: + workflow_dispatch: + inputs: + version: + description: Required on main branch (e.g. 1.2.3) + required: false + +permissions: + contents: write + id-token: write + pull-requests: write + +jobs: + publish: + if: github.actor != 'github-actions[bot]' + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: '24' + cache: 'yarn' + registry-url: 'https://registry.npmjs.org' + + # Node 24 is required for trusted publishers (npm >= 10.9). + # The app itself may target an older version -- ignore engine checks. + - name: Install dependencies + run: yarn install --frozen-lockfile --ignore-engines + + - name: Build and publish + shell: bash + env: + INPUT_VERSION: ${{ inputs.version }} + BRANCH_NAME: ${{ github.ref_name }} + GH_TOKEN: ${{ github.token }} + run: .github/scripts/npm-publish.sh diff --git a/package.json b/package.json index 5ee53bc..1f83796 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moonit/sdk-evm", - "version": "0.0.1", + "version": "0.0.2-alpha.1770755050", "main": "dist/index.js", "types": "dist/types/index.d.ts", "scripts": { @@ -8,9 +8,10 @@ "watch": "tsc --watch", "test": "jest", "lint": "eslint 'src/**/*.{js,ts}'", - "format": "prettier --write 'src/**/*.{js,ts,json,md}'" + "format": "prettier --write 'src/**/*.{js,ts,json,md}'", + "prepublishOnly": "rm -rf dist && npm run build" }, - "repository": "git@github.com:@gomoonit/moonit-sdk-evm", + "repository": "https://github.com/gomoonit/moonit-sdk-evm", "author": "support@moonshot.cc", "license": "MIT", "devDependencies": {