@ 261107.5
current
name: Release undici and undici-types on NPM and create GitHub Release

on:
  push:
    branches:
     - main
    paths:
      - package.json

permissions:
  contents: read

jobs:
  determine-release-version:
    runs-on: ubuntu-latest
    outputs:
      release-version: ${{ steps.determine-release-version.outputs.result }}
    steps:
      - name: Checkout Repository
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        id: checkout
        with:
          persist-credentials: false
      - name: Determine release version
        uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
        id: determine-release-version
        with:
          result-encoding: string
          script: |
            const { owner, repo } = context.repo
            const version = require("./package.json").version
            const versionTag = `v${version}`

            try {
              await github.rest.repos.getReleaseByTag({
                owner,
                repo,
                tag: versionTag
              })
            } catch (error) {
              if (error.status !== 404) {
                throw error
              }

              return versionTag
            }

  release:
    runs-on: ubuntu-latest
    needs: determine-release-version
    if: ${{ startsWith(needs.determine-release-version.outputs.release-version, 'v') }}

    permissions:
      contents: write
      id-token: write

    environment: release

    steps:
      - name: Checkout Repository
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        id: checkout
        with:
          persist-credentials: true

      - name: Setup Node.js
        uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
        with:
          node-version: 'lts/*'
          registry-url: 'https://registry.npmjs.org'

      - name: Install globally latest npm
        run: npm install -g npm@latest
        id: install-globally-latest-npm

      - name: Install dependencies
        run: npm ci
        id: install-dependencies

      - name: Publish undici on NPM
        run: npm publish --access public
        id: npm-publish-undici

      - name: Generate Types Package
        run: node scripts/generate-undici-types-package-json.js
        id: generate-types-package

      - name: Publish undici-types on NPM
        run: npm publish
        id: npm-publish-undici-types
        working-directory: './types'

      - name: Create GitHub release
        uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
        id: create-gh-release
        with:
          script: |
            const defaultBranch = "${{ github.event.repository.default_branch }}"
            const versionTag = "${{ needs.determine-release-version.outputs.release-version }}"
            const commitHash = "${{ github.sha }}"
            await require('./scripts/release').release({ github, context, defaultBranch, versionTag, commitHash })