Publish npm package and Create GitHub Release #17
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 npm package and Create GitHub Release | |
| on: | |
| workflow_dispatch: | |
| permissions: | |
| contents: write # Grant write permission to push the tag | |
| jobs: | |
| build-and-publish: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| with: | |
| fetch-depth: 0 # Ensures the entire Git history is available | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v3 | |
| with: | |
| node-version: '16' | |
| registry-url: 'https://registry.npmjs.org' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm install | |
| - name: Build the project | |
| run: npm run build # adjust the build command if necessary | |
| - name: Configure npm for authentication | |
| run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc | |
| - name: Get package version | |
| id: get_version | |
| run: | | |
| VERSION=$(node -p "require('./package.json').version") | |
| echo "VERSION=$VERSION" >> $GITHUB_ENV | |
| - name: Get the last Git tag | |
| id: last_tag | |
| run: echo "LAST_TAG=$(git describe --tags --abbrev=0)" >> $GITHUB_ENV | |
| - name: Get release notes from commits | |
| id: release_notes | |
| run: | | |
| echo "Release Notes:" > release_notes.txt | |
| git log ${{ env.LAST_TAG }}..HEAD --pretty=format:"- %s" >> release_notes.txt | |
| shell: bash | |
| - name: Show release notes | |
| run: cat release_notes.txt | |
| - name: Create Git tag | |
| run: | | |
| git tag v${{ env.VERSION }} | |
| git push origin v${{ env.VERSION }} | |
| - name: Create GitHub release | |
| uses: actions/create-release@v1 | |
| with: | |
| tag_name: v${{ env.VERSION }} | |
| release_name: Release v${{ env.VERSION }} | |
| body_path: ./release_notes.txt | |
| draft: false | |
| prerelease: false | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Publish to npm | |
| run: npm publish | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |