name: Appium Deploy
on:
  push:
    tags:
      - master
  workflow_dispatch:

permissions:
  contents: read

jobs:
  prepare_matrix:
    runs-on: ubuntu-latest
    outputs:
      versions: ${{ steps.generate-matrix.outputs.active }}
    steps:
    - name: Select all active LTS versions of Node.js
      id: generate-matrix
      uses: msimerson/node-lts-versions@v1

  check:
    needs:
    - prepare_matrix
    name: Check
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        node-version: ${{ fromJSON(needs.prepare_matrix.outputs.versions) }}
        os: [ubuntu-latest, windows-latest, macos-latest]
    steps:
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
      - name: Install Appium
        run: |
          if [[ "$GITHUB_REF_TYPE" == tag ]]; then
            npm i --location=global $GITHUB_REF_NAME
          else
            npm i --location=global appium
          fi
        shell: bash
      - name: Install drivers and plugins
        run: |
          if grep -q macos <<< ${{ matrix.os }}; then
            appium driver install xcuitest
          fi
          appium driver install uiautomator2
          appium plugin install images
        shell: bash
      - name: Verify installation
        run: |
          installed_drivers=$(appium driver list --installed 2>&1)
          if grep -q macos <<< ${{ matrix.os }}; then
            grep -q xcuitest <<< $installed_drivers
          fi
          grep -q uiautomator2 <<< $installed_drivers
          installed_plugins=$(appium plugin list --installed 2>&1)
          grep -q images <<< $installed_plugins
        shell: bash
      - name: Verify Appium could be executed
        run: |
          appium --use-plugins=images &
          pid=$(echo $!)
          sleep 5
          ps -p $pid || exit 1
          kill -2 $pid
        shell: bash
