|
| 1 | +name: Build and Publish |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + |
| 7 | +jobs: |
| 8 | + build: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + outputs: |
| 11 | + should_publish: ${{ steps.version_check.outputs.should_publish }} |
| 12 | + steps: |
| 13 | + - name: Checkout |
| 14 | + uses: actions/checkout@v4 |
| 15 | + |
| 16 | + - name: Setup Node.js |
| 17 | + uses: actions/setup-node@v4 |
| 18 | + with: |
| 19 | + node-version: 20 |
| 20 | + registry-url: 'https://registry.npmjs.org' |
| 21 | + |
| 22 | + - name: Enable Corepack |
| 23 | + run: corepack enable |
| 24 | + |
| 25 | + - name: Install dependencies |
| 26 | + run: yarn install --immutable |
| 27 | + |
| 28 | + - name: Typecheck |
| 29 | + run: yarn typecheck |
| 30 | + |
| 31 | + - name: Lint |
| 32 | + run: yarn lint |
| 33 | + |
| 34 | + - name: Build |
| 35 | + run: yarn prepare |
| 36 | + |
| 37 | + - name: Check version |
| 38 | + id: version_check |
| 39 | + run: | |
| 40 | + PACKAGE_NAME=$(node -p "require('./package.json').name") |
| 41 | + LOCAL_VERSION=$(node -p "require('./package.json').version") |
| 42 | + NPM_VERSION=$(npm view "$PACKAGE_NAME" version 2>/dev/null || echo "0.0.0") |
| 43 | +
|
| 44 | + echo "Local version: $LOCAL_VERSION" |
| 45 | + echo "npm version: $NPM_VERSION" |
| 46 | +
|
| 47 | + if [ "$LOCAL_VERSION" != "$NPM_VERSION" ]; then |
| 48 | + echo "Version changed, will publish" |
| 49 | + echo "should_publish=true" >> $GITHUB_OUTPUT |
| 50 | + else |
| 51 | + echo "Version unchanged, skipping publish" |
| 52 | + echo "should_publish=false" >> $GITHUB_OUTPUT |
| 53 | + fi |
| 54 | +
|
| 55 | + publish: |
| 56 | + needs: build |
| 57 | + if: github.event_name == 'push' && github.ref == 'refs/heads/main' && needs.build.outputs.should_publish == 'true' |
| 58 | + runs-on: ubuntu-latest |
| 59 | + permissions: |
| 60 | + contents: read |
| 61 | + id-token: write |
| 62 | + steps: |
| 63 | + - name: Checkout |
| 64 | + uses: actions/checkout@v4 |
| 65 | + |
| 66 | + - name: Setup Node.js |
| 67 | + uses: actions/setup-node@v4 |
| 68 | + with: |
| 69 | + node-version: 20 |
| 70 | + registry-url: 'https://registry.npmjs.org' |
| 71 | + |
| 72 | + - name: Enable Corepack |
| 73 | + run: corepack enable |
| 74 | + |
| 75 | + - name: Install dependencies |
| 76 | + run: yarn install --immutable |
| 77 | + |
| 78 | + - name: Build |
| 79 | + run: yarn prepare |
| 80 | + |
| 81 | + - name: Publish to npm |
| 82 | + run: npm publish --provenance --access public |
| 83 | + env: |
| 84 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
0 commit comments