# The workflow template is designed for Ember addons.
#
# It assumes certain dependencies and scripts in package.json. If
# they don't apply to your project, feel free to remove irrelevant
# code in the workflow. These can be inferred from the job name.
#
# {
#   "scripts": {
#     "build": "ember build --environment=production",
#     "build:test": "ember build --environment=test",
#     "lint": "npm-run-all --aggregate-output --continue-on-error --parallel lint:*",
#     "lint:dependency": "ember dependency-lint",
#     "lint:hbs": "ember-template-lint .",
#     "lint:js": "eslint .",
#     "start": "ember serve",
#     "test": "npm-run-all lint:* test:*",
#     "test:ember": "ember exam --query=nolint --split=2 --parallel=1",
#     "test:ember-compatibility": "ember try:one"
#   },
#   "devDependencies": {
#     "ember-a11y-testing" (test-app)
#     "ember-cli-dependency-lint" (lint)
#     "ember-exam" (test-app, test-compatibility)
#     "ember-template-lint" (lint)
#     "ember-try" (test-compatibility)
#     "eslint" (lint)
#     "npm-run-all" (lint, test-app)
#   },
# }
#
# For more information, please visit:
#
#   - https://crunchingnumbers.live/2020/03/17/ci-with-github-actions-for-ember-apps/
#   - https://crunchingnumbers.live/2020/08/31/ci-with-github-actions-for-ember-apps-part-2/
#
# Author: Isaac J. Lee (GitHub: @ijlee2)
# Created at: August 31, 2020
# Updated at: August 31, 2020
#

name: CI

on:
  push:
    branches:
      - master
  pull_request:

env:
  NODE_VERSION: 14

jobs:
  build-app:
    name: Build app for testing
    runs-on: ubuntu-latest
    timeout-minutes: 7
    steps:
      - name: Check out a copy of the repo
        uses: actions/checkout@v2

      - name: Use Node.js ${{ env.NODE_VERSION }}
        uses: actions/setup-node@v2-beta
        with:
          node-version: ${{ env.NODE_VERSION }}

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

      - name: Cache Yarn cache and node_modules
        id: cache-dependencies
        uses: actions/cache@v2
        with:
          path: |
            ${{ steps.yarn-cache-dir-path.outputs.dir }}
            node_modules
          key: ${{ runner.os }}-${{ env.NODE_VERSION }}-${{ hashFiles('**/yarn.lock') }}
          restore-keys: ${{ runner.os }}-${{ env.NODE_VERSION }}-

      - name: Install dependencies
        run: yarn install --frozen-lockfile
        if: steps.cache-dependencies.outputs.cache-hit != 'true'

      - name: Build app
        run: yarn build:test

      - name: Upload app
        uses: actions/upload-artifact@v2
        with:
          name: dist
          path: dist

  lint:
    name: Lint files and dependencies
    runs-on: ubuntu-latest
    timeout-minutes: 7
    steps:
      - name: Check out a copy of the repo
        uses: actions/checkout@v2

      - name: Use Node.js ${{ env.NODE_VERSION }}
        uses: actions/setup-node@v2-beta
        with:
          node-version: ${{ env.NODE_VERSION }}

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

      - name: Cache Yarn cache and node_modules
        id: cache-dependencies
        uses: actions/cache@v2
        with:
          path: |
            ${{ steps.yarn-cache-dir-path.outputs.dir }}
            node_modules
          key: ${{ runner.os }}-${{ env.NODE_VERSION }}-${{ hashFiles('**/yarn.lock') }}
          restore-keys: ${{ runner.os }}-${{ env.NODE_VERSION }}-

      - name: Install dependencies
        run: yarn install --frozen-lockfile
        if: steps.cache-dependencies.outputs.cache-hit != 'true'

      - name: Lint
        run: yarn lint

  test-app:
    name: Test app
    needs: [build-app]
    runs-on: ubuntu-latest
    strategy:
      matrix:
        partition:
          - 1
          - 2
    timeout-minutes: 7
    steps:
      - name: Check out a copy of the repo
        uses: actions/checkout@v2

      - name: Use Node.js ${{ env.NODE_VERSION }}
        uses: actions/setup-node@v2-beta
        with:
          node-version: ${{ env.NODE_VERSION }}

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

      - name: Cache Yarn cache and node_modules
        id: cache-dependencies
        uses: actions/cache@v2
        with:
          path: |
            ${{ steps.yarn-cache-dir-path.outputs.dir }}
            node_modules
          key: ${{ runner.os }}-${{ env.NODE_VERSION }}-${{ hashFiles('**/yarn.lock') }}
          restore-keys: ${{ runner.os }}-${{ env.NODE_VERSION }}-

      - name: Install dependencies
        run: yarn install --frozen-lockfile
        if: steps.cache-dependencies.outputs.cache-hit != 'true'

      - name: Download app
        uses: actions/download-artifact@v2
        with:
          name: dist
          path: dist

      - name: Test
        run: yarn test:ember --partition=${{ matrix.partition }} --path=dist

  test-compatibility:
    name: Test compatibility
    runs-on: ubuntu-latest
    strategy:
      matrix:
        scenario:
          - 'ember-lts-3.16'
          - 'ember-lts-3.20'
          - 'ember-release'
          - 'ember-beta'
          - 'ember-canary'
        partition:
          - 1
          - 2
    timeout-minutes: 7
    steps:
      - name: Check out a copy of the repo
        uses: actions/checkout@v2

      - name: Use Node.js ${{ env.NODE_VERSION }}
        uses: actions/setup-node@v2-beta
        with:
          node-version: ${{ env.NODE_VERSION }}

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

      - name: Cache Yarn cache and node_modules
        id: cache-dependencies
        uses: actions/cache@v2
        with:
          path: |
            ${{ steps.yarn-cache-dir-path.outputs.dir }}
            node_modules
          key: ${{ runner.os }}-${{ env.NODE_VERSION }}-${{ matrix.scenario }}-${{ hashFiles('**/yarn.lock') }}
          restore-keys: ${{ runner.os }}-${{ env.NODE_VERSION }}-${{ matrix.scenario }}-

      - name: Install dependencies
        run: yarn install --frozen-lockfile
        if: steps.cache-dependencies.outputs.cache-hit != 'true'

      - name: Test
        run: yarn ci:ember-compatibility ${{ matrix.scenario }} --- yarn test:ember --partition=${{ matrix.partition }}
