###
# Copyright (c) 2024 Eclipse Foundation and others.
#
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which accompanies this distribution, and is available
# at http://eclipse.org/legal/epl-2.0
###

# Set default Keycloak and Node.js
ARG KEYCLOAK_VERSION=25.0.4
ARG NODE_VERSION=18

# Stage 1: Build custom theme and package as .jar
FROM node:${NODE_VERSION} as node_jar_builder
RUN apt-get update && \
    apt-get install -y openjdk-17-jdk && \
    apt-get install -y maven;
WORKDIR /opt/app

# https://gitlab.eclipse.org/eclipsefdn/it/webdev/keycloak-solstice-theme
COPY ./theme/keycloak-solstice-theme/ /opt/app/
RUN yarn install --frozen-lockfile
RUN yarn build-keycloak-theme

# Stage 2: Build theme .jar using Keycloak
FROM keycloak/keycloak:${KEYCLOAK_VERSION} as builder
WORKDIR /opt/keycloak
COPY --from=node_jar_builder /opt/app/dist_keycloak/target/keycloak-solstice-theme-keycloak-theme-0.0.1.jar /opt/keycloak/providers/
RUN /opt/keycloak/bin/kc.sh build

# Stage 3: Final Keycloak image
FROM keycloak/keycloak:${KEYCLOAK_VERSION}
COPY --from=builder /opt/keycloak/ /opt/keycloak/
