
name: Build

on:
  push:
    branches: [ main ]
    paths-ignore:
      - 'package.json'
      - 'CHANGELOG.md'
  pull_request:
    branches: [ main ]
  workflow_dispatch:
    inputs:
      tags:
        required: false
        description: 'Misc tags'

jobs:
  
  build:
    
    name: 'Build'
    
    runs-on: ubuntu-latest
    
    env:
      BUILD_NUMBER: ${{ github.run_number }}
    
    steps:
    
      - name: Checkout
        uses: actions/checkout@v4

      - name: Enable Corepack
        run: corepack enable

      - name: Prepare Yarn 4
        run: corepack prepare yarn@4.0.2 --activate

      - name: Verify Yarn version
        run: yarn -v

      - name: Set up Node.js with Corepack
        uses: actions/setup-node@v4
        with:
          node-version: 22   # Or another supported version
          cache: 'yarn'      # Caches Yarn dependencies

      - name: Bump semver
        if: github.ref == 'refs/heads/main'
        uses: TriPSs/conventional-changelog-action@v3
        with:
          github-token: ${{ secrets.GH_ACTION }}
          git-message: 'chore(release): {version}'
          preset: 'angular'
          tag-prefix: 'v'
          output-file: 'CHANGELOG.md'
          skip-on-empty: false # alter semver even when we push 'chore: ...' commits
          release-count: 0 # ensure changelog is generated to contain ALL updates

      - name: Pull newly bumped semver
        if: github.ref == 'refs/heads/main'
        run: git pull

      - name: Setup .npmrc
        if: github.ref == 'refs/heads/main'
        # Setup .npmrc file to prepare for possible publish to npm
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
          registry-url: 'https://registry.npmjs.org'

      - name: Extract branch name
        shell: bash
        run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
        id: extract_branch

      - name: Extract PR number
        shell: bash
        run: |
          echo "##[set-output name=pr_number;]$(echo $(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH"))"
        id: extract_pr_number

      - name: Extract version number
        shell: bash
        run: |
          echo "##[set-output name=version_number;]$(echo $(jq -r .version package.json))"
        id: extract_version_number

      - run: yarn install --immutable
      - run: yarn run build
      # - run: npm test

      - name: Publish production release
        if: (github.ref == 'refs/heads/main' && github.actor != 'depbot') # do not publish if only doing a dep update initiated by dependabot 
        run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
