name: Build all packages

on:
  push:
    branches:
      - master
  pull_request:

env:
  NX_CLOUD_DISTRIBUTED_EXECUTION: false
  NX_CLOUD_ACCESS_TOKEN: ${{ secrets.NX_CLOUD_ACCESS_TOKEN }}
  TATUM_API_KEY: ${{ secrets.TATUM_API_KEY }}

jobs:
  pr:
    runs-on: ubuntu-latest
    if: ${{ github.event_name == 'pull_request' && github.head_ref != 'release'  }}
    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.event.pull_request.head.ref }}
          fetch-depth: 0
      - 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 }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-
      - name: Derive appropriate SHAs for base and head for `nx affected` commands
        uses: nrwl/nx-set-shas@v2
        with:
          main-branch-name: 'master'
      - uses: actions/setup-node@v1
        with:
          node-version: '14'
      - run: yarn --ignore-scripts --frozen-lockfile
      #      - run: npx nx-cloud start-ci-run
      - run: npx nx affected --target=build --parallel --max-parallel=3 --skip-nx-cache
      - run: yarn test:ci
      #- run: npx nx-cloud stop-all-agents
      - name: SonarCloud Scan
        uses: SonarSource/sonarcloud-github-action@v1.6
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # Needed to get PR information, if any
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

#  agents:
#    if: ${{ github.head_ref != 'release'  }}
#    runs-on: ubuntu-latest
#    name: Agent 1
#    timeout-minutes: 60
#    strategy:
#      matrix:
#        agent: [1, 2, 3]
#    steps:
#      - uses: actions/checkout@v2
#      - uses: actions/setup-node@v1
#        with:
#          node-version: '14'
#      - run: yarn
#      - name: Start Nx Agent ${{ matrix.agent }}
#        run: npx nx-cloud start-agent
