name: CI

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  check-skip:
    runs-on: ubuntu-latest
    steps:
      - name: Cancel Previous Redundant Builds
        uses: styfle/cancel-workflow-action@0.6.0
        with:
          access_token: ${{ github.token }}

  build:
    needs: check-skip
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - uses: actions/setup-node@v3
        with:
          node-version-file: '.nvmrc'
          registry-url: 'https://registry.npmjs.org'
      - name: Install dependencies
        run: |
          npm ci --legacy-peer-deps --no-audit --no-fund --no-optional && npx -y ultra-runner --raw --recursive prepublishOnly &>/dev/null
          npm run lint
          npm run build
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
      - uses: actions/upload-artifact@v3
        with:
          name: public-folder
          path: public

  testing:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version-file: '.nvmrc'
          registry-url: 'https://registry.npmjs.org'
      - name: Install xvfb for headless testing
        run: sudo apt-get install xvfb

      - name: Setup Max User Watches for Testing
        run: echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

      - name: Testing
        run: |
          npm ci --legacy-peer-deps --no-audit --no-fund --no-optional
          npx -y ultra-runner --raw --recursive prepublishOnly &>/dev/null
          xvfb-run --auto-servernum npm run test -- --ci

  release:
    needs: [build, testing]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - uses: actions/setup-node@v3
        with:
          node-version-file: '.nvmrc'
          registry-url: 'https://registry.npmjs.org'
      - name: Release components
        run: |
          npm ci --legacy-peer-deps --no-audit --no-fund --no-optional
          npx @s-ui/ci release
        env:
          GITHUB_EMAIL: cloud-accounts@scmspain.com
          GITHUB_TOKEN: ${{github.token}}
          GITHUB_USER: sui-bot
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

  deploy:
    needs: [build, testing]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version-file: '.nvmrc'
          registry-url: 'https://registry.npmjs.org'
      - uses: actions/download-artifact@v3
        with:
          name: public-folder
      - name: Deploy
        run: |
          npm i -g vercel --no-save --no-fund --no-audit --no-optional --no-scripts
          DEPLOYMENT_URL=$(vercel --token $VERCEL_TOKEN --confirm $VERCEL_PARAMS)
        env:
          VERCEL_PARAMS: ${{ github.event_name != 'pull_request' && '--prod' || ''}}
          VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
          VERCEL_ORG_ID: ${{ secrets.VERCEL_ORG_ID }}
          VERCEL_PROJECT_ID: ${{ secrets.VERCEL_PROJECT_ID }}
