on:
  push:
    branches:
      - master
      - backport/*
    tags:
      - v*.*.*
  pull_request:

name: Testing

jobs:
  nix-tests:
    name: "*nix Tests"
    strategy:
      matrix:
        node: [8, 10, 12]
        os: [ubuntu-16.04, macOS-10.14]
    runs-on: ${{ matrix.os }}
    steps:
      - name: Setup Environment
        run: |
          mkdir ~/.ssh
          chmod 700 ~/.ssh
          echo -e "Host *\n\tStrictHostKeyChecking no\n" > ~/.ssh/config
          echo -e "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBkHMoNRRkHYNE7EnQLdFxMgVcqGgNPYDhrWiLMlYuzpmEcUnhwW3zNaIa4J2JlGkRNgYZVia1Ic1V3koJPE3YO2+exAfJBIPeb6O1qDADc2hFFHzd28wmHKUkO61yzo2ZjDQfaEVtjN39Yiy19AbddN3bzNrgvuQT574fa6Rghl2RfecKYO77iHA1RGXIFc8heXVIUuUV/jHjb56WqoHH8vyt1DqUz89oyiHq8Cku0qzKN80COheZPseA1EvT0zlIgbXBxwijN4xRmvInK0fB5Kc9r3kddH2tT7V09bOFJsvGQaQmQ1WFTCqjpBFw1CHKcbfPLOxbLpVIR9gyx03R" > ~/.ssh/id_rsa.pub
          echo -e "-----BEGIN RSA PRIVATE KEY-----\nMIIEogIBAAKCAQEAwZBzKDUUZB2DROxJ0C3RcTIFXKhoDT2A4a1oizJWLs6ZhHFJ\n4cFt8zWiGuCdiZRpETYGGVYmtSHNVd5KCTxN2DtvnsQHyQSD3m+jtagwA3NoRRR8\n3dvMJhylJDutcs6NmYw0H2hFbYzd/WIstfQG3XTd28za4L7kE+e+H2ukYIZdkX3n\nCmDu+4hwNURlyBXPIXl1SFLlFf4x42+elqqBx/L8rdQ6lM/PaMoh6vApLtKsyjfN\nAjoXmT7HgNRL09M5SIG1wccIozeMUZryJytHweSnPa95HXR9rU+1dPWzhSbLxkGk\nJkNVhUwqo6QRcNQhynG3zyzsWy6VSEfYMsdN0QIDAQABAoIBABsZNPYBEFy/wPvq\nNJ8/et3lC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4BtNHKPeGgV87dM0DWhQaAtEXEIcQquFtba0lAXioGHg8U4\nzeiugl4Qzchwk5qd3wnZ4SOhx0s16/5gQDlnkbjFR6EREUnvLRwV92zBXUTOGIkh\nZ7Z4rcgUKlVAaHT3OHN/lTyqJG/ib+K4wZhbztl/ox+JUFsvD98=\n-----END RSA PRIVATE KEY-----" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa*
          git config --global user.name "John Doe"
          git config --global user.email johndoe@example.com

      - uses: actions/checkout@master

      - name: Use Node.js 8.x
        uses: actions/setup-node@master
        with:
          node-version: ${{ matrix.node }}

      - name: Install Dependencies for Ubuntu
        if: startsWith(matrix.os, 'ubuntu')
        run: sudo apt install build-essential libssl-dev libkrb5-dev libc++-dev

      - name: Install
        env:
          CC: clang
          CXX: clang++
          npm_config_clang: 1
          GYP_DEFINES: use_obsolete_asm=true
        # There is a race condition in node/generate that needs to be fixed
        run: node utils/retry npm install

      - name: Test
        run: |
          set -e
          eval `ssh-agent -s`
          ssh-add ~/.ssh/id_rsa
          node utils/retry npm test

      - name: Deploy
        if: startsWith(github.ref, 'refs/tags/v')
        env:
          node_pre_gyp_bucket: ${{ secrets.node_pre_gyp_bucket }}
          node_pre_gyp_accessKeyId: ${{ secrets.node_pre_gyp_accessKeyId }}
          node_pre_gyp_secretAccessKey: ${{ secrets.node_pre_gyp_secretAccessKey }}
        run: |
          npm install -g node-pre-gyp aws-sdk
          node lifecycleScripts/clean
          node-pre-gyp package
          node-pre-gyp publish

  windows-tests:
    name: Windows Tests
    strategy:
      matrix:
        node: [8, 10, 12]
        arch: [x86, x64]
    runs-on: windows-2016
    steps:
      - name: Setup Environment
        run: |
          git config --file C:\ProgramData\Git\config core.autocrlf input
          git config --system core.autocrlf input
          git config --global core.autocrlf input
          git config --global user.name "John Doe"
          git config --global user.email johndoe@example.com
          npm install -g node-pre-gyp aws-sdk

      - uses: actions/checkout@master

      - name: Use Node.js 8.x
        uses: implausible/setup-node@feature/expose-architecture-override
        with:
          node-version: ${{ matrix.node }}
          node-arch: ${{ matrix.arch }}

      - name: Install
        run: npm install

      - name: Test
        env:
          GIT_SSH: ${{ github.workspace }}\vendor\plink.exe
        run: |
          powershell -command "Start-Process ${{ github.workspace }}\vendor\pageant.exe ${{ github.workspace }}\vendor\private.ppk"
          node utils/retry npm test

      # You're probably wondering why this isn't a single `run: |` step, it certainly is for *nix,
      # but it's not, because the CI runner for windows doesn't wait for each step as listed here
      # and it treats each additional step past the first as an orphaned process.
      - name: Deploy (Dependencies)
        if: startsWith(github.ref, 'refs/tags/v')
        run: npm install -g node-pre-gyp aws-sdk

      - name: Deploy (Clean)
        if: startsWith(github.ref, 'refs/tags/v')
        run: node lifecycleScripts\clean

      - name: Deploy (Package)
        if: startsWith(github.ref, 'refs/tags/v')
        run: node-pre-gyp package

      - name: Deploy (Publish)
        if: startsWith(github.ref, 'refs/tags/v')
        env:
          node_pre_gyp_bucket: ${{ secrets.node_pre_gyp_bucket }}
          node_pre_gyp_accessKeyId: ${{ secrets.node_pre_gyp_accessKeyId }}
          node_pre_gyp_secretAccessKey: ${{ secrets.node_pre_gyp_secretAccessKey }}
        run: node-pre-gyp publish
