# Copyright 2022 Google LLC
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd

# A workflow to test building in various Linux distros.
name: Test Linux Distros

on:
  # Runs when called from another workflow.
  workflow_call:
    inputs:
      ref:
        required: true
        type: string

  # Runs when triggered manually by a maintainer.
  workflow_dispatch:
    inputs:
      ref:
        description: The ref to build from.
        required: true
        type: string


# By default, run all commands in a bash shell.  On Windows, the default would
# otherwise be powershell.
defaults:
  run:
    shell: bash

jobs:
  # Configure the build matrix based on files in the repo.
  docker_matrix_config:
    name: Matrix configuration
    runs-on: ubuntu-latest
    outputs:
      MATRIX: ${{ steps.configure.outputs.MATRIX }}
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ inputs.ref }}
          persist-credentials: false

      - name: Configure Build Matrix
        id: configure
        shell: node {0}
        run: |
          const fs = require('fs');

          const folder = 'packager/testing/dockers/';
          const files = fs.readdirSync(folder);

          const matrix = [];
          for (const file of files) {
            const os_name = file.replace('_Dockerfile', '');
            matrix.push({
              os_name,
              docker_platform: 'linux/amd64',
              runs_on: 'ubuntu-latest',
            });

            const contents = fs.readFileSync(folder + file);
            if (/\bNOARM\b/.exec(contents) == null) {
              matrix.push({
                os_name,
                docker_platform: 'linux/arm64',
                runs_on: 'ubuntu-24.04-arm',
              });
            }
          }

          // Output a JSON object consumed by the build matrix below.
          fs.writeFileSync(process.env.GITHUB_OUTPUT,
              `MATRIX=${ JSON.stringify(matrix) }`,
              {flag: 'a'});

          // Log the outputs, for the sake of debugging this script.
          console.log({matrix});

  # Build each dockerfile in parallel in a different CI job.
  build:
    needs: docker_matrix_config
    strategy:
      # Let other matrix entries complete, so we have all results on failure
      # instead of just the first failure.
      fail-fast: false
      matrix:
        include: ${{ fromJSON(needs.docker_matrix_config.outputs.MATRIX) }}

    name: ${{ matrix.os_name }} on ${{ matrix.docker_platform }}
    runs-on: ${{ matrix.runs_on }}

    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ inputs.ref }}
          submodules: recursive
          persist-credentials: false

      - name: Build in Docker
        run: ./packager/testing/test_dockers.sh "${{ matrix.os_name }}"
