# build environment
# Please update the .github/variables/node.env for the node version used in github action
FROM node:18.2.0-alpine AS build
ARG appVersion='unknown'
ENV DD_VERSION ${appVersion}

ARG GH_PAT
ENV GH_PAT=$GH_PAT

WORKDIR /app
# commented copy and frozen lock because this contradicts with dependabot.
# Whenever there is an update available the lock file needs to update itself. This frozen-lock parameter gets in the way
# and as a result the whole build fails. Without frozen lockfile, the buildscript will generate a new lock file everytime, therefore COPY too isnt necessary.
COPY package.json yarn.lock .npmrc ./

# RUN yarn install --ignore-scripts --frozen-lockfile

RUN yarn install --ignore-scripts --frozen-lockfile

COPY . .

RUN yarn build
RUN yarn build storybook

# remove development packages needed only for build
RUN npm prune --production --force

# production environment
FROM nginxinc/nginx-unprivileged:1.21.6-alpine

WORKDIR /app

ARG SERVICE_VERSION
ARG SERVICE_NAME

ENV SERVICE_VERSION=$SERVICE_VERSION
ENV SERVICE_NAME=$SERVICE_NAME
ENV DD_VERSION=$SERVICE_VERSION
ENV DD_SERVICE=$SERVICE_NAME

COPY nginx.conf /etc/nginx/nginx.conf

USER root
RUN apk add --no-cache bash tini

COPY --from=build /app/build /usr/share/nginx/html

COPY --from=build /app/storybook-static /usr/share/nginx/html/storybook-static


COPY entrypoint.sh ./
COPY build-config.sh ./

RUN chmod +x /app/build-config.sh /app/entrypoint.sh
RUN chown -R nginx:nginx /usr/share/nginx/html

ENTRYPOINT ["/sbin/tini", "--"]

CMD ["./entrypoint.sh"]

USER nginx
