|
56 | 56 | git pull |
57 | 57 | npm install |
58 | 58 | pm2 startOrReload ecosystem.config.json --env production |
| 59 | + release: |
| 60 | + needs: deploy |
| 61 | + runs-on: ubuntu-latest |
| 62 | + permissions: |
| 63 | + contents: write |
| 64 | + steps: |
| 65 | + - name: Checkout (full history + tags) |
| 66 | + uses: actions/checkout@v4 |
| 67 | + with: |
| 68 | + fetch-depth: 0 |
| 69 | + |
| 70 | + - name: Setup Node.js |
| 71 | + uses: actions/setup-node@v4 |
| 72 | + with: |
| 73 | + node-version: "24" |
| 74 | + |
| 75 | + - name: Derive next version and create the release |
| 76 | + shell: bash |
| 77 | + env: |
| 78 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 79 | + # Untrusted input — read from env, never inline into the script. |
| 80 | + HEAD_COMMIT_MSG: ${{ github.event.head_commit.message }} |
| 81 | + run: | |
| 82 | + set -e |
| 83 | +
|
| 84 | + # Never release the same commit twice (idempotent re-runs). |
| 85 | + EXISTING_TAG="$(git tag -l 'v*' --points-at HEAD | head -n1)" |
| 86 | + if [ -n "$EXISTING_TAG" ]; then |
| 87 | + echo "Commit already released as ${EXISTING_TAG} — nothing to do." |
| 88 | + exit 0 |
| 89 | + fi |
| 90 | +
|
| 91 | + # Resolve the bump level from the commit message; default patch. |
| 92 | + LEVEL="patch" |
| 93 | + if printf '%s' "$HEAD_COMMIT_MSG" | grep -qiE 'BREAKING CHANGE|\[major\]|(^|[^a-z])[a-z]+(\([^)]*\))?!:'; then |
| 94 | + LEVEL="major" |
| 95 | + elif printf '%s' "$HEAD_COMMIT_MSG" | grep -qiE '\[minor\]|(^|[^a-z])feat(\([^)]*\))?:'; then |
| 96 | + LEVEL="minor" |
| 97 | + fi |
| 98 | + echo "Bump level resolved from commit message: ${LEVEL}" |
| 99 | +
|
| 100 | + # Find the highest existing release tag (version-aware sort). |
| 101 | + LATEST_TAG="$(git tag -l 'v*' --sort=-v:refname | head -n1)" |
| 102 | +
|
| 103 | + if [ -z "$LATEST_TAG" ]; then |
| 104 | + # First release: seed from package.json (1.1.0 -> v1.1.0). |
| 105 | + NEXT="$(node -p "require('./package.json').version")" |
| 106 | + echo "No prior release tag — seeding first release from package.json: ${NEXT}" |
| 107 | + else |
| 108 | + # Bump the latest tag with npm's semver engine. |
| 109 | + BASE="${LATEST_TAG#v}" |
| 110 | + TMP="$(mktemp -d)" |
| 111 | + printf '{"name":"x","version":"%s"}\n' "$BASE" > "$TMP/package.json" |
| 112 | + ( cd "$TMP" && npm version "$LEVEL" --no-git-tag-version >/dev/null ) |
| 113 | + NEXT="$(node -p "require('${TMP}/package.json').version")" |
| 114 | + rm -rf "$TMP" |
| 115 | + echo "Latest release ${LATEST_TAG} -> next version ${NEXT}" |
| 116 | + fi |
| 117 | +
|
| 118 | + TAG="v${NEXT}" |
| 119 | +
|
| 120 | + if gh release view "$TAG" >/dev/null 2>&1; then |
| 121 | + echo "Release $TAG already exists — nothing to do." |
| 122 | + exit 0 |
| 123 | + fi |
| 124 | +
|
| 125 | + gh release create "$TAG" \ |
| 126 | + --target "$GITHUB_SHA" \ |
| 127 | + --title "$TAG" \ |
| 128 | + --generate-notes \ |
| 129 | + --latest |
| 130 | + echo "Published release $TAG" |
0 commit comments