name: Linux-aarch64

env:
  DEBUG: 'napi:*'
  CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER: 'aarch64-linux-gnu-gcc-9'

on:
  push:
    branches:
      - main
  pull_request:

jobs:
  build:
    name: stable - aarch64-unknown-linux-gnu - node@14
    runs-on: ubuntu-20.04

    steps:
      - run: docker run --rm --privileged multiarch/qemu-user-static:register --reset

      - uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: 14
          check-latest: true

      - name: Install
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          override: true

      - name: Install aarch64 toolchain
        run: rustup target add aarch64-unknown-linux-gnu

      - name: Generate Cargo.lock
        uses: actions-rs/cargo@v1
        with:
          command: generate-lockfile

      - name: Cache cargo registry
        uses: actions/cache@v1
        with:
          path: ~/.cargo/registry
          key: stable-linux-aarch64-gnu-node@14-cargo-registry-trimmed-${{ hashFiles('**/Cargo.lock') }}

      - name: Cache cargo index
        uses: actions/cache@v1
        with:
          path: ~/.cargo/git
          key: stable-linux-aarch64-gnu-node@14-cargo-index-trimmed-${{ hashFiles('**/Cargo.lock') }}

      - name: Cache NPM dependencies
        uses: actions/cache@v1
        with:
          path: node_modules
          key: npm-cache-linux-aarch64-gnu-node@14-${{ hashFiles('yarn.lock') }}

      - name: Install cross compile toolchain
        run: sudo apt-get install gcc-9-aarch64-linux-gnu g++-9-aarch64-linux-gnu -y

      - name: Install dependencies
        run: yarn install --frozen-lockfile --registry https://registry.npmjs.org --network-timeout 300000

      - name: 'Build TypeScript'
        run: yarn build

      - name: Cross build native tests
        run: yarn build:test:aarch64

      - name: Setup and run tests
        uses: docker://multiarch/ubuntu-core:arm64-focal
        with:
          args: >
            sh -c "
              apt-get update && \
              apt-get install -y ca-certificates gnupg2 curl && \
              curl -sL https://deb.nodesource.com/setup_14.x | bash - && \
              apt-get install -y nodejs && \
              npm test
            "