# Stage 1
FROM ubuntu:<%= ubuntuVersion %> as base

ENV PATH=/tmp/node/bin:$PATH

RUN \
  apt-get update && \
  apt-get install --yes curl && \
  # Download Tini.
  curl --silent --fail --location --output /tmp/tini https://github.com/krallin/tini/releases/download/v<%= tiniVersion %>/tini && \
  chmod +x /tmp/tini && \
  # Download Node.
  mkdir /tmp/node && \
  curl --silent --fail https://nodejs.org/dist/v<%= nodeVersion %>/node-v<%= nodeVersion %>-linux-x64.tar.gz \
  | tar --gunzip --extract --strip-components=1 --directory=/tmp/node

# Create and move to project directory.
WORKDIR /home/app

# Copy manifests and build artifacts.
COPY ./ /home/app

# Conditionally copy .npmrc.
<% if (hasNpmrc) { %>
COPY .npmrc .npmrc
<% } %>

# Install production dependencies.
RUN npm <%= hasLockfile ? 'ci' : 'install' %> --omit dev --skip-optional --ignore-scripts

# Stage 2
FROM gcr.io/distroless/base

# Set environment variables.
<% envVars.forEach(envExpression => { %>
  ENV <%= envExpression %>
<% }); %>

WORKDIR /home/app

COPY --from=base /tmp/tini /bin/tini
COPY --from=base /tmp/node /usr/local
COPY --from=base /home/app /home/app

# Set entrypoint.
ENTRYPOINT ["tini", "--", "node", "<%= entry %>"]
