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

name: Testing

jobs:
  linux-tests:
    name: "Linux Tests"
    strategy:
      matrix:
        node: [20, 22, 24]
      fail-fast: false
    runs-on: ubuntu-22.04
    steps:
      - name: Install Dependencies for Ubuntu
        run: sudo apt-get update && sudo apt-get install -y software-properties-common git build-essential clang libssl-dev libkrb5-dev libc++-dev wget zlib1g-dev

      - uses: actions/checkout@v4

      - name: Setup Environment
        run: |
          set -e
          mkdir ~/.ssh_tests
          chmod 700 ~/.ssh_tests
          printf "%b" "Host *\n\tStrictHostKeyChecking no\n" > ~/.ssh_tests/config
          cat test/id_rsa.pub > ~/.ssh_tests/id_rsa.pub
          cat test/id_rsa.enc | base64 -d > ~/.ssh_tests/id_rsa
          chmod 600 ~/.ssh_tests/id_rsa*
          git config --global user.name "John Doe"
          git config --global user.email johndoe@example.com

      - uses: actions/setup-python@v5
        with:
          python-version: "3.11"

      - name: Use Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node }}
          check-latest: true

      - name: Install
        run: npm install

      - name: Test
        run: |
          set -e
          eval `ssh-agent -s`
          ssh-add ~/.ssh_tests/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 }}
          AWS_ACCESS_KEY_ID: ${{ secrets.node_pre_gyp_accessKeyId }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.node_pre_gyp_secretAccessKey }}
        run: |
          npm install -g @mapbox/node-pre-gyp aws-sdk
          node lifecycleScripts/clean
          node-pre-gyp package
          node-pre-gyp publish

  macos-tests:
    name: "macOS Tests"
    strategy:
      matrix:
        node: [20, 22, 24]
      fail-fast: false
    runs-on: macOS-13
    # This is mostly the same as the Linux steps, waiting for anchor support
    # https://github.com/actions/runner/issues/1182
    steps:
      - uses: actions/checkout@v4

      - name: Setup Environment
        run: |
          mkdir ~/.ssh_tests
          chmod 700 ~/.ssh_tests
          printf "%b" "Host *\n\tStrictHostKeyChecking no\n" > ~/.ssh_tests/config
          cat test/id_rsa.pub > ~/.ssh_tests/id_rsa.pub
          cat test/id_rsa.enc | base64 -d > ~/.ssh_tests/id_rsa
          chmod 600 ~/.ssh_tests/id_rsa*
          git config --global user.name "John Doe"
          git config --global user.email johndoe@example.com

      - name: Use Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node }}
          check-latest: true

      - name: Install
        run: npm install

      - name: Test
        run: |
          set -e
          eval `ssh-agent -s`
          ssh-add ~/.ssh_tests/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 }}
          AWS_ACCESS_KEY_ID: ${{ secrets.node_pre_gyp_accessKeyId }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.node_pre_gyp_secretAccessKey }}
        run: |
          npm install -g @mapbox/node-pre-gyp aws-sdk
          node lifecycleScripts/clean
          node-pre-gyp package
          node-pre-gyp publish

  windows-tests:
    name: Windows Tests
    strategy:
      matrix:
        node: [20, 22, 24]
        arch: [x86, x64]
        exclude:
        - node: 24
          arch: x86
      fail-fast: false
    runs-on: windows-2019
    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

      - uses: actions/checkout@v4

      - name: Use Node.js
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node }}
          check-latest: true
          architecture: ${{ 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 @mapbox/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 }}
          AWS_ACCESS_KEY_ID: ${{ secrets.node_pre_gyp_accessKeyId }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.node_pre_gyp_secretAccessKey }}
        run: node-pre-gyp publish
