name: Build and Publish

on:
  push:
    branches:
      - '**'
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    container:
      image: catthehacker/ubuntu:java-tools-24.04
    steps:
      - name: Checkout repository
        uses: https://github.com/actions/checkout@v4

      - name: Login al registry di Gitea
        run: |
          echo "${{ secrets.AUTHTOKEN }}" | docker login ${{ vars.REGISTRY_URL }} -u ${{ github.repository_owner }} --password-stdin
          echo "${{ secrets.AUTHTOKEN }}" ${{ vars.REGISTRY_URL }}  ${{ github.repository_owner }}

      - name: Build immagine Docker
        run: |
          IMAGE_NAME="${{ vars.REGISTRY_URL }}/${{ github.repository }}"
          ./gradlew bootJar jibDockerBuild -x test -Pprod --image=${IMAGE_NAME}:${{ github.sha }}
          docker tag ${IMAGE_NAME}:${{ github.sha }} ${IMAGE_NAME}:${{ github.ref_name}}
          docker tag ${IMAGE_NAME}:${{ github.sha }} ${IMAGE_NAME}:latest
          echo "IMAGE_NAME=${IMAGE_NAME}" >> $GITHUB_ENV

      - name: Push immagine Docker
        run: |
          docker push ${{ env.IMAGE_NAME }}:${{ github.sha }}
          docker push ${{ env.IMAGE_NAME }}:${{ github.ref_name }}
          docker push ${{ env.IMAGE_NAME }}:latest
          echo "Docker image pushed successfully!"
