# Written by Lance McCarthy as an example only. Use at your own risk, carefully review how you are handling your secrets and what is included in your final image.

###############
### STAGE 1 ###
###############
# Install required dependencies into the stripped-down base image
FROM tgagor/centos:latest AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

# Update the repositories
RUN cd /etc/yum.repos.d/
# For CentOS 7
# RUN sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
# RUN sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
# For newer CentOS:latest
RUN sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/centos.repo
RUN sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/centos.repo

RUN yum update -y

# IMPORTANT - Enable EPEL repostitory (if this doesn't work, use `RUN rpm -ihv --nodeps https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm` instead)
RUN yum install epel-release -y

# REPORTING DEPENDENCIES - See https://docs.telerik.com/reporting/knowledge-base/how-to-build-and-install-libgdiplus-linux#solution-for-centos-and-amazon-linux
# Install Skia dependencies for new Telerik report rendering with Skia
RUN yum install freetype fontconfig -y

# DOTNET RUNTIME - Install aspnetcore runtime (if your base image does not have it already)
RUN rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
RUN yum install aspnetcore-runtime-10.0 -y


###############
### STAGE 2 ###
###############

# Compile the project and create production-ready artifacts
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:10.0 AS build
WORKDIR /src
COPY . .

# 1. Mount the "telerik-nuget-key" secret and immediately use it to add the Telerik server as a package source
RUN --mount=type=secret,id=telerik-nuget-key \
    dotnet nuget add source 'https://nuget.telerik.com/v3/index.json' -n "TelerikNuGetServer" -u "api-key" -p $(cat /run/secrets/telerik-nuget-key) --store-password-in-clear-text

# 2. Restore NuGet packages
RUN dotnet restore "./MyAspNetCoreApp.csproj"

# 3. Mount the "telerik-license-key" secret as an env var and build the project
RUN --mount=type=secret,id=telerik-license-key \
    TELERIK_LICENSE="$(cat /run/secrets/telerik-license-key)" \
    dotnet publish "./MyAspNetCoreApp.csproj" -o /app/publish /p:UseAppHost=false --no-restore --self-contained false


###############
### STAGE 3 ###
###############

# Build the final image from the base, but copy the pubilsh artifacts from the intermediate stage
FROM base AS final
WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "MyAspNetCoreApp.dll"]