# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

name: Clean up Local Registry on manual event
on:
  workflow_dispatch:
    inputs:
      nodes:
        default: "gaudi,xeon"
        description: "Hardware to clean up"
        required: true
        type: string

env:
  EXAMPLES: ${{ vars.NIGHTLY_RELEASE_EXAMPLES }}

jobs:
  get-build-matrix:
    runs-on: ubuntu-latest
    outputs:
      examples: ${{ steps.get-matrix.outputs.examples }}
      nodes: ${{ steps.get-matrix.outputs.nodes }}
    steps:
    - name: Create Matrix
      id: get-matrix
      run: |
        examples=($(echo ${EXAMPLES} | tr ',' ' '))
        examples_json=$(printf '%s\n' "${examples[@]}" | sort -u | jq -R '.' | jq -sc '.')
        echo "examples=$examples_json" >> $GITHUB_OUTPUT
        nodes=($(echo ${{ inputs.nodes }} | tr ',' ' '))
        nodes_json=$(printf '%s\n' "${nodes[@]}" | sort -u | jq -R '.' | jq -sc '.')
        echo "nodes=$nodes_json" >> $GITHUB_OUTPUT

  clean-up:
    needs: get-build-matrix
    strategy:
      matrix:
        node: ${{ fromJson(needs.get-build-matrix.outputs.nodes) }}
      fail-fast: false
    runs-on: "docker-build-${{ matrix.node }}"
    steps:
      - name: Clean Up Local Registry
        run: |
          echo "Cleaning up local registry on ${{ matrix.node }}"
          bash /home/sdp/workspace/fully_registry_cleanup.sh
          docker ps | grep registry

  build:
    needs: [get-build-matrix, clean-up]
    strategy:
      matrix:
        example: ${{ fromJson(needs.get-build-matrix.outputs.examples) }}
        node: ${{ fromJson(needs.get-build-matrix.outputs.nodes) }}
      fail-fast: false
    uses: ./.github/workflows/_example-workflow.yml
    with:
      node: ${{ matrix.node }}
      example: ${{ matrix.example }}
    secrets: inherit
