|
| 1 | +name: Publish Package |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - 1.x |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +jobs: |
| 10 | + verify_version: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + outputs: |
| 13 | + should_publish: ${{ steps.check.outputs.should_publish }} |
| 14 | + version: ${{ steps.check.outputs.version }} |
| 15 | + steps: |
| 16 | + - name: Checkout repository |
| 17 | + uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + fetch-depth: 0 |
| 20 | + |
| 21 | + - name: Check if package.json version changed |
| 22 | + id: check |
| 23 | + run: | |
| 24 | + echo "Current branch: ${{ github.ref }}" |
| 25 | +
|
| 26 | + # Get current version |
| 27 | + CURRENT_VERSION=$(jq -r .version package.json) |
| 28 | + echo "Current version: $CURRENT_VERSION" |
| 29 | +
|
| 30 | + # Get previous commit hash |
| 31 | + git rev-parse HEAD~1 || git rev-parse HEAD |
| 32 | + PREV_COMMIT=$(git rev-parse HEAD~1 2>/dev/null || git rev-parse HEAD) |
| 33 | +
|
| 34 | + # Check if package.json changed |
| 35 | + if git diff --name-only HEAD~1 HEAD | grep "package.json"; then |
| 36 | + echo "Package.json was changed in this commit" |
| 37 | + |
| 38 | + # Get previous version if possible |
| 39 | + if git show "$PREV_COMMIT:package.json" 2>/dev/null; then |
| 40 | + PREV_VERSION=$(git show "$PREV_COMMIT:package.json" | jq -r .version) |
| 41 | + echo "Previous version: $PREV_VERSION" |
| 42 | + |
| 43 | + if [ "$CURRENT_VERSION" != "$PREV_VERSION" ]; then |
| 44 | + echo "Version changed from $PREV_VERSION to $CURRENT_VERSION" |
| 45 | + echo "should_publish=true" >> $GITHUB_OUTPUT |
| 46 | + else |
| 47 | + echo "Version unchanged" |
| 48 | + echo "should_publish=false" >> $GITHUB_OUTPUT |
| 49 | + fi |
| 50 | + else |
| 51 | + echo "First commit with package.json, will publish" |
| 52 | + echo "should_publish=true" >> $GITHUB_OUTPUT |
| 53 | + fi |
| 54 | + else |
| 55 | + echo "Package.json not changed in this commit" |
| 56 | + echo "should_publish=false" >> $GITHUB_OUTPUT |
| 57 | + fi |
| 58 | +
|
| 59 | + echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT |
| 60 | +
|
| 61 | + publish: |
| 62 | + needs: verify_version |
| 63 | + if: needs.verify_version.outputs.should_publish == 'true' |
| 64 | + runs-on: ubuntu-latest |
| 65 | + permissions: |
| 66 | + contents: write |
| 67 | + steps: |
| 68 | + - name: Checkout repository |
| 69 | + uses: actions/checkout@v4 |
| 70 | + with: |
| 71 | + fetch-depth: 0 |
| 72 | + |
| 73 | + - name: Create Git tag |
| 74 | + run: | |
| 75 | + git config user.name "github-actions[bot]" |
| 76 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 77 | + git tag -a "v${{ needs.verify_version.outputs.version }}" -m "Release v${{ needs.verify_version.outputs.version }}" |
| 78 | + git push origin "v${{ needs.verify_version.outputs.version }}" |
| 79 | + env: |
| 80 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 81 | + |
| 82 | + - name: Setup Bun |
| 83 | + uses: oven-sh/setup-bun@v2 |
| 84 | + |
| 85 | + - name: Install dependencies |
| 86 | + run: bun install --frozen-lockfile |
| 87 | + |
| 88 | + - name: Build package |
| 89 | + run: bun run build |
| 90 | + |
| 91 | + - name: Publish to npm |
| 92 | + run: | |
| 93 | + # strip “refs/heads/” prefix |
| 94 | + BRANCH=${GITHUB_REF#refs/heads/} |
| 95 | + if [[ "$BRANCH" == "1.x" ]]; then |
| 96 | + echo "Publishing to npm under the 'beta' tag" |
| 97 | + bun publish --tag beta |
| 98 | + else |
| 99 | + echo "Publishing to npm under the 'latest' tag" |
| 100 | + bun publish |
| 101 | + fi |
| 102 | + env: |
| 103 | + NPM_CONFIG_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 104 | + |
| 105 | + create_release: |
| 106 | + needs: [verify_version, publish] |
| 107 | + if: needs.verify_version.outputs.should_publish == 'true' |
| 108 | + runs-on: ubuntu-latest |
| 109 | + permissions: |
| 110 | + contents: write |
| 111 | + steps: |
| 112 | + - name: Create GitHub Release |
| 113 | + uses: actions/create-release@v1 |
| 114 | + env: |
| 115 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 116 | + with: |
| 117 | + tag_name: "v${{ needs.verify_version.outputs.version }}" |
| 118 | + release_name: "v${{ needs.verify_version.outputs.version }}" |
| 119 | + body: "Release v${{ needs.verify_version.outputs.version }}" |
| 120 | + draft: false |
| 121 | + prerelease: false |
0 commit comments