Publish to NPM #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publish to NPM | |
| on: | |
| release: | |
| types: [published] | |
| workflow_dispatch: | |
| inputs: | |
| version_type: | |
| description: 'Version bump type' | |
| required: true | |
| default: 'patch' | |
| type: choice | |
| options: | |
| - patch | |
| - minor | |
| - major | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '18' | |
| registry-url: 'https://registry.npmjs.org' | |
| - name: Cache node modules | |
| uses: actions/cache@v4 | |
| with: | |
| path: ~/.npm | |
| key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} | |
| restore-keys: | | |
| ${{ runner.os }}-node- | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Run linting | |
| run: npm run lint | |
| - name: Build project | |
| run: npm run build | |
| - name: Verify build output | |
| run: | | |
| if [ ! -d "dist" ]; then | |
| echo "Build failed: dist directory not found" | |
| exit 1 | |
| fi | |
| if [ ! -f "dist/index.js" ]; then | |
| echo "Build failed: main file not found" | |
| exit 1 | |
| fi | |
| if [ ! -f "dist/index.d.ts" ]; then | |
| echo "Build failed: type definitions not found" | |
| exit 1 | |
| fi | |
| - name: Bump version and create tag (manual trigger only) | |
| if: github.event_name == 'workflow_dispatch' | |
| id: version | |
| run: | | |
| git config --local user.email "[email protected]" | |
| git config --local user.name "GitHub Action" | |
| # Bump version | |
| npm version ${{ github.event.inputs.version_type }} --no-git-tag-version | |
| # Get new version | |
| NEW_VERSION=$(node -p "require('./package.json').version") | |
| echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT | |
| # Commit changes | |
| git add package.json package-lock.json | |
| git commit -m "chore: bump version to $NEW_VERSION" | |
| # Create and push tag | |
| git tag "v$NEW_VERSION" | |
| git push origin main | |
| git push origin "v$NEW_VERSION" | |
| - name: Get version for release trigger | |
| if: github.event_name == 'release' | |
| id: release_version | |
| run: | | |
| VERSION=$(echo "${{ github.event.release.tag_name }}" | sed 's/^v//') | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| - name: Publish to NPM | |
| run: npm publish --access public | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
| - name: Create GitHub Release (manual trigger only) | |
| if: github.event_name == 'workflow_dispatch' | |
| uses: actions/create-release@v1 | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| with: | |
| tag_name: v${{ steps.version.outputs.version }} | |
| release_name: Release v${{ steps.version.outputs.version }} | |
| body: | | |
| ## Release v${{ steps.version.outputs.version }} | |
| This release was automatically generated from the workflow. | |
| ### Changes | |
| - Version bump (${{ github.event.inputs.version_type }}) | |
| - Updated package to v${{ steps.version.outputs.version }} | |
| ### Installation | |
| ```bash | |
| npm install nestjs-offline-oauth2@${{ steps.version.outputs.version }} | |
| ``` | |
| ### Documentation | |
| Please refer to the [README](https://github.com/RedSoftwareSystems/nestjs-offline-oauth2/blob/main/README.md) for usage instructions. | |
| draft: false | |
| prerelease: false | |
| - name: Post-publish notification | |
| run: | | |
| if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then | |
| VERSION="${{ steps.version.outputs.version }}" | |
| else | |
| VERSION="${{ steps.release_version.outputs.version }}" | |
| fi | |
| echo "✅ Successfully published nestjs-offline-oauth2@$VERSION to NPM" | |
| echo "📦 Package URL: https://www.npmjs.com/package/nestjs-offline-oauth2" |