FROM ssa-containers.aoc.nrao.edu/ops/base:nodejs-14 as base-build

# Requires build-arg $env
# Build arg that sets environment; sets to "dev" if no build arg is given
ARG env=dev
ENV ENV=${env}

# Requires build-arg WS_VERSION
# Build arg that sets Workspaces Version; defaults to "dev" if no build arg is given
ARG WS_VERSION=dev
ENV NG_APP_WS_VERSION=${WS_VERSION}

# Switch to vlapipe
USER vlapipe

# Create ~/app
RUN mkdir -p /home/vlapipe/app

# Set NPM environment variables for vlapipe
ENV NPM_CONFIG_PREFIX=/home/vlapipe/.npm-global
ENV PATH=$PATH:/home/vlapipe/.npm-global/bin

# Change working directory to /home/vlapipe/app
WORKDIR /home/vlapipe/app

# Copy package.json to /home/vlapipe/app in the image
# set ownership of content to vlapipe and the vlapipe group
COPY --chown=vlapipe:vlapipe /apps/web/package-lock.json .
COPY --chown=vlapipe:vlapipe /apps/web/package.json .

ENV NG_CLI_ANALYTICS=false

# Install node modules
RUN npm install

# Switch to root
USER root

# remove build packages
RUN apk del build-dependencies

# Switch to vlapipe
USER vlapipe

# Copy web directory to /home/vlapipe/app in the image
# set ownership of content to vlapipe and the vlapipe group
COPY --chown=vlapipe:vlapipe ./apps/web ./

# Build the angular app
RUN ./node_modules/.bin/ng build --configuration=${ENV} --output-path=dist

#
## NGINX section of multi-stage image
#
# Use nginx base image
FROM nginx:1.19.7-alpine

# Copy WS nginx config from base-build stage
COPY --from=base-build /home/vlapipe/app/ws-nginx.conf.template /etc/nginx/templates/

# Remove default nginx config
RUN rm /etc/nginx/conf.d/default.conf

# Copy Angular build from base-build stage
COPY --from=base-build /home/vlapipe/app/dist /usr/share/nginx/html