name: Build Server Candidate

on:
  workflow_dispatch:

jobs:
  build-and-push-server:
    name: Build & Push Server
    runs-on: graalvm-25
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          ref: ${{ github.ref }}

      - name: Cache Maven dependencies
        uses: actions/cache@v3
        with:
          path: ~/.m2/repository
          key: ${{ runner.os }}-maven-${{ hashFiles('**/server/pom.xml') }}
          restore-keys: |
            ${{ runner.os }}-maven-

      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build Docker image
        working-directory: ./server
        run: |
          ./mvnw -Pnative -DskipTests spring-boot:build-image \
            -Dspring-boot.build-image.imageName=rmcampos/tasknote-api:latest \
            -Dspring-boot.build-image.builder=paketobuildpacks/builder-jammy-tiny:0.0.505

      - name: Tag and push candidate
        run: |
          docker tag rmcampos/tasknote-api:latest rmcampos/tasknote-api:candidate
          docker push rmcampos/tasknote-api:candidate

      - name: Log pushed images
        run: |
          echo "Pushed images:"
          echo "  rmcampos/tasknote-api:latest"
          echo "  rmcampos/tasknote-api:candidate"