|
| 1 | +name: Publish to NPM |
| 2 | +on: |
| 3 | + pull_request: |
| 4 | + types: |
| 5 | + - closed |
| 6 | + branches: |
| 7 | + - main |
| 8 | + - rel/** |
| 9 | + |
| 10 | +jobs: |
| 11 | + publish-to-npm: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + if: | |
| 14 | + github.event.pull_request.merged == true && |
| 15 | + contains(github.event.pull_request.title, 'chore: Release') |
| 16 | +
|
| 17 | + steps: |
| 18 | + - name: Checkout |
| 19 | + uses: actions/checkout@v5 |
| 20 | + with: |
| 21 | + fetch-depth: 0 |
| 22 | + fetch-tags: true |
| 23 | + |
| 24 | + - name: Setup Bun |
| 25 | + uses: oven-sh/setup-bun@v2 |
| 26 | + with: |
| 27 | + bun-version: latest |
| 28 | + |
| 29 | + - name: Install |
| 30 | + run: bun install --frozen-lockfile |
| 31 | + |
| 32 | + - name: Get version |
| 33 | + id: get_version |
| 34 | + run: | |
| 35 | + CURRENT_VERSION=$(bun -e "console.log(require('./package.json').version)") |
| 36 | + if git rev-parse "$CURRENT_VERSION" >/dev/null 2>&1; then |
| 37 | + echo "Tag $CURRENT_VERSION already exists, nothing to do" |
| 38 | + exit 0 |
| 39 | + else |
| 40 | + echo "Tag $CURRENT_VERSION does not exist, proceeding with release creation" |
| 41 | + fi |
| 42 | + echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT |
| 43 | +
|
| 44 | + - name: Build & Publish |
| 45 | + run: | |
| 46 | + bun run build |
| 47 | +
|
| 48 | + VERSION='${{ steps.get_version.outputs.version }}' |
| 49 | + if [[ $VERSION =~ alpha ]]; then |
| 50 | + bun publish --tag alpha |
| 51 | + elif [[ $VERSION =~ beta ]]; then |
| 52 | + bun publish --tag beta |
| 53 | + else |
| 54 | + bun publish |
| 55 | + fi |
| 56 | + env: |
| 57 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 58 | + |
| 59 | + - name: Create release notes |
| 60 | + uses: actions/github-script@v8 |
| 61 | + id: release_notes |
| 62 | + with: |
| 63 | + script: | |
| 64 | + // Use the PR that triggered this workflow |
| 65 | + const releasePr = context.payload.pull_request; |
| 66 | + const releaseNotes = releasePr.body.split('<!--')[0].trim(); |
| 67 | + core.setOutput('notes', releaseNotes); |
| 68 | +
|
| 69 | + - name: Create GitHub Release |
| 70 | + uses: actions/github-script@v8 |
| 71 | + env: |
| 72 | + RELEASE_NOTES: ${{ steps.release_notes.outputs.notes }} |
| 73 | + with: |
| 74 | + script: | |
| 75 | + const notes = process.env.RELEASE_NOTES; |
| 76 | + const version = '${{ steps.get_version.outputs.version }}'; |
| 77 | + const isPrerelease = /alpha|beta/i.test(version); |
| 78 | +
|
| 79 | + await github.rest.repos.createRelease({ |
| 80 | + owner: context.repo.owner, |
| 81 | + repo: context.repo.repo, |
| 82 | + tag_name: version, |
| 83 | + name: `Release ${version}`, |
| 84 | + body: notes, |
| 85 | + draft: false, |
| 86 | + prerelease: isPrerelease |
| 87 | + }); |
0 commit comments