name: Build Docker Container

on:
  push:
    branches: [ "main" ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          clean: false

      - name: Patch Email From
        run: sed -i "s/message.setTo(toEmail);/message.setTo(toEmail);\nmessage.setFrom(\"thymecrunch@cs.nmt.edu\");/g" src/main/java/com/example/demo/service/EmailService.java

      - name: Get Metadata
        id: meta
        run: |
          echo REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F"/" '{print $2}') >> $GITHUB_OUTPUT
          echo REPO_VERSION=$(git describe --tags --always | sed 's/^v//') >> $GITHUB_OUTPUT                

      - name: Set up Maven
        uses: stCarolas/setup-maven@v5
        with:
          maven-version: 3.9.14

      - name: Set up JDK 25
        uses: actions/setup-java@v4
        with:
          java-version: 25
          distribution: 'oracle'
          cache: 'maven'

      - name: Build with Maven
        run: mvn clean package -Dmaven.test.skip

      - name: Log into Registry
        uses: docker/login-action@v2
        with:
          registry: https://git.ls-r.net
          username: etc404
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: |
            git.ls-r.net/etc404/thymecrunch:${{ steps.meta.outputs.REPO_VERSION }}
            git.ls-r.net/etc404/thymecrunch:latest
