|
| 1 | +name: Publish to NPM |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + - main |
| 8 | + tags: |
| 9 | + - 'v*' |
| 10 | + workflow_dispatch: |
| 11 | + inputs: |
| 12 | + version_bump: |
| 13 | + description: 'Version bump type' |
| 14 | + required: true |
| 15 | + default: 'patch' |
| 16 | + type: choice |
| 17 | + options: |
| 18 | + - patch |
| 19 | + - minor |
| 20 | + - major |
| 21 | + |
| 22 | +jobs: |
| 23 | + publish: |
| 24 | + runs-on: ubuntu-latest |
| 25 | + permissions: |
| 26 | + contents: write |
| 27 | + packages: write |
| 28 | + steps: |
| 29 | + - name: Checkout code |
| 30 | + uses: actions/checkout@v6 |
| 31 | + |
| 32 | + - name: Setup Node.js for npm |
| 33 | + uses: actions/setup-node@v6 |
| 34 | + with: |
| 35 | + node-version: '20' |
| 36 | + registry-url: 'https://registry.npmjs.org' |
| 37 | + scope: '@laravilt' |
| 38 | + |
| 39 | + - name: Configure npm authentication |
| 40 | + run: | |
| 41 | + echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" >> ~/.npmrc |
| 42 | + echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" >> ~/.npmrc |
| 43 | +
|
| 44 | + - name: Install dependencies |
| 45 | + run: npm install |
| 46 | + |
| 47 | + - name: Bump version (if manual trigger) |
| 48 | + if: github.event_name == 'workflow_dispatch' |
| 49 | + run: | |
| 50 | + git config user.name "GitHub Actions" |
| 51 | + git config user.email "actions@github.com" |
| 52 | + npm version ${{ github.event.inputs.version_bump }} --no-git-tag-version |
| 53 | + git add package.json |
| 54 | + git commit -m "chore: bump version to $(node -p "require('./package.json').version")" |
| 55 | + git push origin HEAD:${{ github.ref_name }} |
| 56 | +
|
| 57 | + - name: Build package |
| 58 | + run: npm run build:npm |
| 59 | + |
| 60 | + - name: Publish to npm registry |
| 61 | + if: startsWith(github.ref, 'refs/tags/v') || github.event_name == 'workflow_dispatch' |
| 62 | + run: | |
| 63 | + echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > .npmrc |
| 64 | + echo "registry=https://registry.npmjs.org/" >> .npmrc |
| 65 | + npm publish --access public |
| 66 | +
|
| 67 | + - name: Publish to GitHub Packages |
| 68 | + if: startsWith(github.ref, 'refs/tags/v') || github.event_name == 'workflow_dispatch' |
| 69 | + run: | |
| 70 | + echo "//npm.pkg.github.com/:_authToken=${{ secrets.GITHUB_TOKEN }}" > .npmrc |
| 71 | + echo "registry=https://npm.pkg.github.com/" >> .npmrc |
| 72 | + echo "@laravilt:registry=https://npm.pkg.github.com/" >> .npmrc |
| 73 | + npm publish |
| 74 | +
|
| 75 | + - name: Get package info |
| 76 | + id: package_info |
| 77 | + run: | |
| 78 | + echo "name=$(node -p "require('./package.json').name")" >> $GITHUB_OUTPUT |
| 79 | + echo "version=$(node -p "require('./package.json').version")" >> $GITHUB_OUTPUT |
| 80 | +
|
| 81 | + - name: Create Release |
| 82 | + if: startsWith(github.ref, 'refs/tags/v') || github.event_name == 'workflow_dispatch' |
| 83 | + uses: softprops/action-gh-release@v1 |
| 84 | + with: |
| 85 | + tag_name: v${{ steps.package_info.outputs.version }} |
| 86 | + name: v${{ steps.package_info.outputs.version }} |
| 87 | + body: | |
| 88 | + ## ${{ steps.package_info.outputs.name }} v${{ steps.package_info.outputs.version }} |
| 89 | +
|
| 90 | + Published to npm: https://www.npmjs.com/package/${{ steps.package_info.outputs.name }} |
| 91 | +
|
| 92 | + Install with: |
| 93 | + ```bash |
| 94 | + npm install ${{ steps.package_info.outputs.name }}@${{ steps.package_info.outputs.version }} |
| 95 | + ``` |
| 96 | + env: |
| 97 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 98 | + |
| 99 | + - name: Push version tag (if manual trigger) |
| 100 | + if: github.event_name == 'workflow_dispatch' |
| 101 | + run: | |
| 102 | + git tag "v${{ steps.package_info.outputs.version }}" |
| 103 | + git push origin --tags |
0 commit comments