name: WebFlasher CI

on: [push]

jobs:
  check-changes:
    runs-on: ubuntu-latest
    outputs:
      all_changed_and_modified_files_count: ${{ steps.changed-files.outputs.all_changed_and_modified_files_count }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Get changed files count
        id: changed-files
        uses: tj-actions/changed-files@v40.1.1
        with:
          files_ignore: 'doc/**,README.md,Dockerfile,.*'
          files_ignore_separator: ','
      - name: Print changed files count
        run: >
          echo "Changed files count: ${{ 
            steps.changed-files.outputs.all_changed_and_modified_files_count }}"

  build:
    needs: check-changes
    runs-on: ubuntu-latest
    if: ${{ needs.check-changes.outputs.all_changed_and_modified_files_count >= 1 }}
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: recursive
      - uses: actions/setup-node@v4
        with:
          node-version: lts/*
          cache: yarn
          cache-dependency-path: '**/yarn.lock'
      - uses: actions/cache@v4
        with:
          path: |
            ~/node_modules
          key: ${{ runner.os }}-webflasher
      - name: Install yarn
        run: yarn
      - name: Run linter
        run: yarn lint
      # - name: Run vitest tests
      #   run: yarn vitest run
      # - name: Install Playwright Browsers
      #   run: npx playwright install --with-deps
      # - name: Run Playwright tests
      #   run: npx playwright test
      - name: Build
        run: yarn build
      - name: Create tarball
        run: tar czf webflasher.tgz --strip-components=1 dist
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: 'github-pages'
          path: |
            webflasher.tgz
  deploy:
    # Add a dependency to the build job
    needs: build
    permissions:
      pages: write # to deploy to Pages
      id-token: write # to verify the deployment originates from an appropriate source
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4