name: Build and test
on:
  push:
    branches-ignore:
      - staging.tmp

jobs:
  preflight:
    if: "!contains(github.event.head_commit.message, 'skip ci')"
    runs-on: ubuntu-latest

    steps:
      - run: exit 0

  test:
    needs: preflight
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        node-version: ['14']

    steps:
      - uses: actions/checkout@v2

      - uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}

      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - uses: actions/cache@v2
        id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-${{ matrix.node-version }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.node-version }}-yarn-

      - name: Install Depedencies
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: scripts/ci/yarn_install

      - name: Run Lint
        run: yarn lint

      - name: Run default tests
        run: yarn test

      - name: Install dependencies (no lockfile)
        run: |
          rm -rf node_modules
          yarn install --non-interactive --no-lockfile

      - name: Run tests (floating dependencies)
        run: yarn test

  ember-try:
    needs: preflight
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        node-version: ['14']
        embery-try-scenario: ['ember-release', 'ember-beta', 'ember-canary']

    steps:
      - uses: actions/checkout@v2

      - uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}

      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - uses: actions/cache@v2
        id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-${{ matrix.node-version }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-${{ matrix.node-version }}-yarn-

      - name: Install Depedencies
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: scripts/ci/yarn_install

      - name: 'Run Tests for ${{ matrix.embery-try-scenario }}'
        run: yarn ember try:one ${{ matrix.embery-try-scenario }} --skip-cleanup

  # "Group" individual statuses into a pseudo-status that we can use for bors to watch for.
  ci-success:
    name: ci-finished
    if: github.event_name == 'push'
    needs:
      - test
      - ember-try
    runs-on: ubuntu-latest
    steps:
      - name: Mark the job as a success
        if: ${{ success() }}
        run: exit 0
      - name: Mark the job as a failure
        if: ${{ !success() }}
        run: exit 1
