ARG BUILDER_IMAGE=nexus.ervu.rt-sk.ru/ervu-base/alpine:3.22.2
ARG RUNTIME_IMAGE=nexus.ervu.rt-sk.ru/ervu-base/alpine:3.22.2


FROM $BUILDER_IMAGE AS builder

ARG MVN_FLAGS="-T4C -Pprod --batch-mode --no-transfer-progress"

RUN echo "https://dl-cdn.alpinelinux.org/alpine/v3.22/main" >> /etc/apk/repositories \
 && echo "https://dl-cdn.alpinelinux.org/alpine/v3.22/community" >> /etc/apk/repositories \
 && apk --update --no-cache add git maven nodejs npm openjdk21-jdk

ENV LANG=ru_RU.UTF-8
ENV LANGUAGE=ru_RU.UTF-8
ENV LC_ALL=ru_RU.UTF-8

WORKDIR /app
COPY . .

RUN mkdir -p /root/.m2 \
# && cp config/settings.xml /root/.m2/settings.xml \
 && mvn clean ${MVN_FLAGS} \
 && mvn package ${MVN_FLAGS}


FROM $RUNTIME_IMAGE

RUN rm -f /etc/apk/repositories \
 && echo "https://dl-cdn.alpinelinux.org/alpine/v3.22/main" >> /etc/apk/repositories \
 && echo "https://dl-cdn.alpinelinux.org/alpine/v3.22/community" >> /etc/apk/repositories \
 && apk update \
 && apk upgrade \
 && apk --no-cache add nginx openjdk21-jre-headless

ENV BACKEND_URL=http://localhost:8080
ENV CONFIG_DATA_EXECUTOR_URL=http://localhost:8080/api


EXPOSE 80

RUN addgroup --system --gid 1002 app \
 && adduser -S app -u 1002 -G app \
 && adduser -S cde -u 1003 -G app

COPY config/nginx.conf /etc/nginx/nginx.conf
COPY --from=builder /app/frontend/dist /usr/share/nginx/html
COPY --from=builder /app/backend/target/*.jar /home/app/backend.jar
COPY --from=builder /app/config-data-executor/target/*.jar /home/cde/cde.jar

USER app

WORKDIR /home/app

ENTRYPOINT ["java", "-jar", "/home/app/backend.jar"]
