|
| 1 | +name: Publish Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + tag: |
| 10 | + description: "Version tag (e.g. v0.1.0). Leave empty to reuse package.json." |
| 11 | + required: false |
| 12 | + type: string |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: write |
| 16 | + |
| 17 | +jobs: |
| 18 | + determine-version: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + outputs: |
| 21 | + should_publish: ${{ steps.detect.outputs.should_publish || steps.manual.outputs.should_publish }} |
| 22 | + version: ${{ steps.detect.outputs.version || steps.manual.outputs.version }} |
| 23 | + steps: |
| 24 | + - name: Checkout |
| 25 | + uses: actions/checkout@v5 |
| 26 | + with: |
| 27 | + fetch-depth: 0 |
| 28 | + |
| 29 | + - name: Manual version input |
| 30 | + if: github.event_name == 'workflow_dispatch' |
| 31 | + id: manual |
| 32 | + run: | |
| 33 | + VERSION="${{ inputs.tag }}" |
| 34 | + if [ -z "$VERSION" ]; then |
| 35 | + VERSION=$(jq -r .version package.json) |
| 36 | + fi |
| 37 | + VERSION="${VERSION#v}" |
| 38 | + if [ -z "$VERSION" ]; then |
| 39 | + echo "Failed to determine version for manual publish." |
| 40 | + exit 1 |
| 41 | + fi |
| 42 | + echo "Using manual version $VERSION" |
| 43 | + echo "should_publish=true" >> "$GITHUB_OUTPUT" |
| 44 | + echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 45 | +
|
| 46 | + - name: Detect version change on main |
| 47 | + if: github.event_name != 'workflow_dispatch' |
| 48 | + id: detect |
| 49 | + run: | |
| 50 | + if ! git rev-parse HEAD~1 >/dev/null 2>&1; then |
| 51 | + echo "Initial commit detected, skipping publish." |
| 52 | + echo "should_publish=false" >> "$GITHUB_OUTPUT" |
| 53 | + exit 0 |
| 54 | + fi |
| 55 | + CURRENT_VERSION=$(jq -r .version package.json) |
| 56 | + PREVIOUS_VERSION=$(git show HEAD~1:package.json | jq -r .version) |
| 57 | + if [ "$CURRENT_VERSION" = "$PREVIOUS_VERSION" ]; then |
| 58 | + echo "Version unchanged ($CURRENT_VERSION), skipping publish." |
| 59 | + echo "should_publish=false" >> "$GITHUB_OUTPUT" |
| 60 | + exit 0 |
| 61 | + fi |
| 62 | + echo "Detected version bump from $PREVIOUS_VERSION to $CURRENT_VERSION" |
| 63 | + echo "should_publish=true" >> "$GITHUB_OUTPUT" |
| 64 | + echo "version=$CURRENT_VERSION" >> "$GITHUB_OUTPUT" |
| 65 | +
|
| 66 | + publish: |
| 67 | + needs: determine-version |
| 68 | + if: needs.determine-version.outputs.should_publish == 'true' |
| 69 | + runs-on: macos-latest |
| 70 | + env: |
| 71 | + GH_TOKEN: ${{ secrets.GH_PUBLISH_TOKEN }} |
| 72 | + GITHUB_TOKEN: ${{ secrets.GH_PUBLISH_TOKEN }} |
| 73 | + NODE_ENV: production |
| 74 | + APP_VERSION: ${{ needs.determine-version.outputs.version }} |
| 75 | + steps: |
| 76 | + - name: Checkout |
| 77 | + uses: actions/checkout@v5 |
| 78 | + with: |
| 79 | + fetch-depth: 0 |
| 80 | + - name: Setup pnpm |
| 81 | + uses: pnpm/action-setup@v4 |
| 82 | + with: |
| 83 | + version: 10.14.0 |
| 84 | + - name: Setup Node.js |
| 85 | + uses: actions/setup-node@v4 |
| 86 | + with: |
| 87 | + node-version: 22 |
| 88 | + cache: "pnpm" |
| 89 | + - name: Install dependencies |
| 90 | + run: pnpm install --frozen-lockfile |
| 91 | + - name: Verify package version |
| 92 | + run: | |
| 93 | + PACKAGE_VERSION=$(jq -r .version package.json) |
| 94 | + if [ "$PACKAGE_VERSION" != "$APP_VERSION" ]; then |
| 95 | + echo "Package version $PACKAGE_VERSION does not match expected $APP_VERSION" |
| 96 | + exit 1 |
| 97 | + fi |
| 98 | + - name: Create or reuse tag |
| 99 | + run: | |
| 100 | + TAG="v$APP_VERSION" |
| 101 | + git fetch --tags |
| 102 | + if git rev-parse "refs/tags/$TAG" >/dev/null 2>&1; then |
| 103 | + echo "Tag $TAG already exists, reusing it." |
| 104 | + else |
| 105 | + git config user.name "posthog-bot" |
| 106 | + git config user.email "[email protected]" |
| 107 | + git tag -a "$TAG" -m "Release $TAG" |
| 108 | + git push https://x-access-token:${GH_TOKEN}@github.com/${{ github.repository }} "$TAG" |
| 109 | + fi |
| 110 | + - name: Build native modules |
| 111 | + run: pnpm run build-native |
| 112 | + - name: Publish with Electron Forge |
| 113 | + run: pnpm run publish |
0 commit comments