name: Publish UI

on:
  workflow_dispatch:
    inputs:
      update:
        type: choice
        options:
          - prerelease
          - patch
          - minor
          - major
        description: 'Degree of update'
        default: 'prerelease'
        required: true
      publish:
        description: 'Publish version to npm'
        type: boolean
        required: false
        default: true

jobs:
  update:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2.3.5
        name: Checkout main
        with:
          fetch-depth: 0
          token: ${{ secrets.DEVOPS_TEAM_PAT }}

      - uses: actions/setup-node@v2
        with:
          node-version: '16'
          registry-url: 'https://registry.npmjs.org'

      - name: Setup git user
        run: git config --global user.email alex.segal@perimeter81.com && git config --global user.name github-ci

      - name: Cache node modules
        uses: actions/cache@v2
        env:
          cache-name: cache-node-modules
        with:
          path: ~/.npm
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-build-${{ env.cache-name }}-
            ${{ runner.os }}-build-
            ${{ runner.os }}-

      - name: Install dependencies
        run: npm ci

      - name: Update package.json version
        run: npx nx run ui:version --releaseAs=${{ github.event.inputs.update }}

      - name: Fix formatting after changelog additions
        run: npx nx format check

      - name: Build
        run: npx nx run ui:build

      - name: Push new versions and tags
        run: git push --follow-tags origin main

      - name: Publish UI lib
        if: ${{ github.event.inputs.publish }}
        run: npx nx run ui:publish-ui
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
