name: Build and Push Docker Image

on:
  push:
    branches:
      - main

jobs:
  docker:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Login to Harbor
        uses: docker/login-action@v3
        with:
          registry: harbor.italiadatacenter.com
          username: ${{ secrets.HARBOR_USERNAME }}
          password: ${{ secrets.HARBOR_PASSWORD }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: harbor.italiadatacenter.com/test/nginxtest:${{ github.sha }}
		  
		  #run: /root/work/pipeline/build_container.sh ${{ github.event.repository.name }}
		  
---------------------------------------------------------------------
name: Build and Push Docker Images from Containers

on:
  push:
    branches: [ main ]
  workflow_dispatch:  # Permette esecuzione manuale

jobs:
  # Job per elencare le directory con Dockerfile e creare una matrice
  list-containers:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
	  
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set matrix for containers
        id: set-matrix
        run: |
          # Trova tutte le directory contenenti Dockerfile in containers/
          dirs=$(find containers -name Dockerfile -exec dirname {} \; | sort)
          # Converte in JSON per la matrice (es. [{"dir": "containers/app1"}, {"dir": "containers/app2"}])
          matrix=$(echo "$dirs" | jq -R -s -c 'split("\n")[:-1] | map({dir: .})')
          echo "matrix=$matrix" >> $GITHUB_OUTPUT

      - name: Log in to Docker registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io  # Esempio: GitHub Container Registry; cambia se usi Docker Hub
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

  # Job per buildare e pushare ogni immagine usando la matrice
  build:
    needs: list-containers
    runs-on: ubuntu-latest
    strategy:
      matrix:
        container: ${{ fromJson(needs.list-containers.outputs.matrix) }}
  
 
      - name: Build and push Docker image
        uses: docker/build-push-action@v5
        with:
          context: ${{ matrix.container.dir }}  # Contesto del build (la directory del Dockerfile)
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max		  