# This is a multi-stage build to not spill the contents of the deploy_key
FROM mhart/alpine-node:10 as base

# We need git and openssh to resolve `git+ssh` links in package.json
RUN apk update \
    && apk add git openssh

WORKDIR /usr/src
COPY package*.json ./

# Pass through Now build-env as Docker build-arg
ARG EXAMPLE_DEPLOY_KEY

# Setup key file during build stage
RUN mkdir -p ~/.ssh
RUN echo $EXAMPLE_DEPLOY_KEY | base64 -d > ~/.ssh/deploy_key
RUN chmod 600 ~/.ssh/deploy_key

# Add github.com to known hosts to avoid error
RUN ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts

# Install dependencies (incl. `git+ssh` links)
ENV GIT_SSH_COMMAND "ssh -i ~/.ssh/deploy_key"
RUN npm install

# Copy resulting output and build app
COPY . .
RUN npm run now-build

# Finally, assemble the running image
FROM mhart/alpine-node:base-10
WORKDIR /usr/src

# Now v1 expects static assets at `/public`
RUN mkdir /public
COPY --from=base /usr/src/build /public