|
| 1 | +name: Publish to npm |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + paths: |
| 7 | + - 'package.json' |
| 8 | + workflow_dispatch: |
| 9 | + |
| 10 | +jobs: |
| 11 | + check-version: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + outputs: |
| 14 | + should_publish: ${{ steps.check.outputs.should_publish }} |
| 15 | + version: ${{ steps.check.outputs.version }} |
| 16 | + steps: |
| 17 | + - uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + fetch-depth: 2 |
| 20 | + |
| 21 | + - name: Check version change |
| 22 | + id: check |
| 23 | + run: | |
| 24 | + VERSION=$(node -p "require('./package.json').version") |
| 25 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 26 | + |
| 27 | + if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then |
| 28 | + echo "should_publish=true" >> $GITHUB_OUTPUT |
| 29 | + exit 0 |
| 30 | + fi |
| 31 | + |
| 32 | + git show HEAD~1:package.json > /tmp/old.json 2>/dev/null || { |
| 33 | + echo "should_publish=true" >> $GITHUB_OUTPUT |
| 34 | + exit 0 |
| 35 | + } |
| 36 | + |
| 37 | + OLD_VERSION=$(node -p "require('/tmp/old.json').version" || echo "") |
| 38 | + |
| 39 | + if [ "$VERSION" != "$OLD_VERSION" ]; then |
| 40 | + echo "should_publish=true" >> $GITHUB_OUTPUT |
| 41 | + else |
| 42 | + echo "should_publish=false" >> $GITHUB_OUTPUT |
| 43 | + fi |
| 44 | +
|
| 45 | + publish: |
| 46 | + needs: check-version |
| 47 | + if: needs.check-version.outputs.should_publish == 'true' |
| 48 | + runs-on: ubuntu-latest |
| 49 | + permissions: |
| 50 | + contents: write |
| 51 | + environment: |
| 52 | + name: npm |
| 53 | + url: https://www.npmjs.com/package/@xdevplatform/xdk |
| 54 | + steps: |
| 55 | + - uses: actions/checkout@v4 |
| 56 | + |
| 57 | + - uses: actions/setup-node@v4 |
| 58 | + with: |
| 59 | + node-version: '20' |
| 60 | + registry-url: 'https://registry.npmjs.org' |
| 61 | + |
| 62 | + - run: npm ci |
| 63 | + - run: npm run build |
| 64 | + - run: npm test |
| 65 | + |
| 66 | + - name: Publish |
| 67 | + run: npm publish |
| 68 | + env: |
| 69 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 70 | + |
| 71 | + - name: Create tag |
| 72 | + run: | |
| 73 | + git config user.name "github-actions[bot]" |
| 74 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 75 | + git tag -a "v${{ needs.check-version.outputs.version }}" -m "v${{ needs.check-version.outputs.version }}" || true |
| 76 | + git push origin "v${{ needs.check-version.outputs.version }}" || true |
| 77 | + |
| 78 | + - name: Create Release |
| 79 | + uses: softprops/action-gh-release@v2 |
| 80 | + with: |
| 81 | + tag_name: v${{ needs.check-version.outputs.version }} |
| 82 | + generate_release_notes: true |
0 commit comments