Skip to content

NPM Release

Verify Before Release

Release workflows publish to public registries. Published packages cannot be deleted.

Secure workflow for publishing npm packages with provenance statements.

NPM with Provenance

Publish to npm registry with automated provenance generation.

name: NPM Release with Provenance
on:
  push:
    tags:
      - 'v*.*.*'

permissions:
  contents: read

jobs:
  # Job 1: Publish to npm with provenance
  publish-npm:
    runs-on: ubuntu-latest
    environment:
      name: npm-production
      url: https://www.npmjs.com/package/${{ github.event.repository.name }}
    permissions:
      contents: read
      id-token: write  # Generate provenance
    steps:
      - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11  # v4.1.1
        with:
          persist-credentials: false

      - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d  # v3.8.1
        with:
          node-version: '20'
          registry-url: 'https://registry.npmjs.org'

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm test

      - name: Build package
        run: npm run build

      # SECURITY: Verify package contents before publishing
      - name: Verify package contents
        run: |
          npm pack --dry-run
          echo "Package contents:"
          tar -tzf "$(npm pack)"

      # SECURITY: Publish with provenance
      # Provenance links package to source repository and build
      - name: Publish to npm
        run: npm publish --provenance --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

  # Job 2: Publish to GitHub Packages
  publish-github:
    runs-on: ubuntu-latest
    environment: github-packages
    permissions:
      contents: read
      packages: write
      id-token: write
    steps:
      - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11  # v4.1.1
        with:
          persist-credentials: false

      - uses: actions/setup-node@5e21ff4d9bc1a8cf6de233a3057d20ec6b3fb69d  # v3.8.1
        with:
          node-version: '20'
          registry-url: 'https://npm.pkg.github.com'
          scope: '@${{ github.repository_owner }}'

      - run: npm ci
      - run: npm run build

      # SECURITY: Update package.json for GitHub Packages
      - name: Configure package for GitHub Packages
        run: |
          jq '.name = "@${{ github.repository_owner }}/" + .name' package.json > package.json.tmp
          mv package.json.tmp package.json

      - name: Publish to GitHub Packages
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  # Job 3: Create GitHub release
  release:
    needs: [publish-npm, publish-github]
    runs-on: ubuntu-latest
    environment: production
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11  # v4.1.1

      - name: Create GitHub release
        uses: softprops/action-gh-release@de2c0eb89ae2a093876385947365aca7b0e5f844  # v0.1.15
        with:
          generate_release_notes: true
          body: |
            ## NPM Package

            Install via npm:
            ```bash
            npm install ${{ github.event.repository.name }}@${{ github.ref_name }}
            ```

            Install via GitHub Packages:
            ```bash
            npm install @${{ github.repository_owner }}/${{ github.event.repository.name }}@${{ github.ref_name }}
            ```

            ### Provenance Verification

            Verify package provenance:
            ```bash
            npm audit signatures
            ```
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Provenance: npm CLI automatically generates and publishes provenance statements when --provenance flag used.

Comments