name: Build
on:
  pull_request:
    branches:
      - master
      - main
  push:
    branches:
      - master
      - main
    tags:
      - v*
  schedule:
    - cron: '0 0 * * *'

jobs:
  test:
    name: Test (${{ matrix.scenario }})
    runs-on: ubuntu-latest
    env:
      CI: 'true'
    strategy:
      fail-fast: false
      matrix:
        scenario:
          - default
          - default-with-lockfile
          - default-with-jquery
          - release
          - beta
          - canary
          - classic
          - lts-3.12
          - lts-3.16
    steps:
      - name: Checkout
        uses: actions/checkout@v1
      - name: Set up Volta
        uses: volta-cli/action@v1
      - name: Install dependencies (yarn)
        if: matrix.scenario == 'default-with-lockfile'
        run: yarn install --non-interactive --frozen-lockfile
      - name: Install dependencies (yarn)
        if: matrix.scenario != 'default-with-lockfile'
        run: yarn install --non-interactive --no-lockfile
      - name: Setup ember-try scenario
        if: matrix.scenario != 'default' && matrix.scenario != 'default-with-lockfile'
        run: >-
          yarn ember try:one ember-${{ matrix.scenario }} --skip-cleanup ---
          '[ -z "$EMBER_OPTIONAL_FEATURES" ] || echo "EMBER_OPTIONAL_FEATURES=$EMBER_OPTIONAL_FEATURES" >> $GITHUB_ENV'
      - name: Lint (hbs)
        run: yarn lint:hbs
      - name: Lint (js)
        run: yarn lint:js
      - name: Build
        run: yarn ember build --environment test
      - name: Run test (babel)
        run: yarn test:babel
      - name: Run test (ember)
        # Due to a bug in ember-cli, running `ember test` with `--path` doesn't set `EMBER_ENV=test`
        # See https://github.com/ember-cli/ember-cli/issues/8922
        run: EMBER_ENV=test yarn test:ember --path dist

  publish:
    name: Publish
    needs: test
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v1
      - name: Set up Volta
        uses: volta-cli/action@v1
      - name: Set up npm
        run: |
          echo "//registry.npmjs.org/:_authToken=${NPM_AUTH_TOKEN}"> ~/.npmrc
          npm whoami
        env:
          NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
      - name: Publish to npm
        run: npm publish
      - name: Publish to GitHub
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: ${{ github.ref }}
          draft: false
          prerelease: false
