name: shidoka-applications
on:
  push:
    branches:
      - main
      - beta
  pull_request:
env:
  GH_TOKEN: '${{ secrets.GH_TOKEN }}'
jobs:
  Lint:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Use Node.js 20
        uses: actions/setup-node@v3
        with:
          node-version: 20
          cache: 'npm'
      - name: Install dependencies
        run: npm ci --legacy-peer-deps
      - name: Lint
        run: npm run lint
  Build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Use Node.js 20.x
        uses: actions/setup-node@v3
        with:
          node-version: 20
          cache: 'npm'
      - name: Install dependencies
        run: npm ci --legacy-peer-deps
      - name: Build component bundles
        run: npm run build
      - name: Upload build artifact
        uses: actions/upload-artifact@v3
        with:
          name: build-artifact
          path: dist
  Test:
    needs: Build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Use Node.js 20
        uses: actions/setup-node@v3
        with:
          node-version: 20
          cache: 'npm'
      - name: Install dependencies
        run: npm ci --legacy-peer-deps
      - name: Download build artifact
        uses: actions/download-artifact@v3
        with:
          name: build-artifact
          path: dist
      - name: Install Playwright
        run: npx playwright install --with-deps
      - name: Test
        run: npm run test
  Build_Deploy_Storybook:
    needs: Test
    runs-on: ubuntu-latest
    if: ${{ github.ref == 'refs/heads/main' && github.event_name == 'push' }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Use Node.js 20
        uses: actions/setup-node@v3
        with:
          node-version: 20
          cache: 'npm'
      - name: Install dependencies
        run: npm ci --legacy-peer-deps
      - name: Build Storybook
        run: npm run build-storybook
      - name: Deploy Storybook to GitHub Pages
        run: npm run deploy-storybook -- --ci --existing-output-dir=storybook-static
  Release:
    needs: Test
    runs-on: ubuntu-latest
    if: ${{ (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/beta') && github.event_name == 'push' }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
          token: ${{ secrets.GH_TOKEN }}
      - name: Use Node.js 20
        uses: actions/setup-node@v3
        with:
          node-version: 20
          cache: 'npm'
          scope: '@kyndryl-design-system'
      - name: Install dependencies
        run: npm ci --legacy-peer-deps
      - name: Install semantic-release extra plugins
        run: npm i --save-dev @semantic-release/changelog @semantic-release/git --legacy-peer-deps
      - name: Download build artifact
        uses: actions/download-artifact@v3
        with:
          name: build-artifact
          path: dist
      - name: Release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NODE_AUTH_TOKEN: ${{ secrets.GH_TOKEN_PACKAGES }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: HUSKY=0 npx semantic-release
