FROM        086679231553.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:node20 AS prod-deps
# Copy app and install production dependencies
WORKDIR     /var/app
COPY        .npmrc package.json yarn.lock apollo.config.js tsconfig.json tsconfig.build.json ./
RUN         --mount=type=secret,id=GITHUB_NPM_TOKEN,required=true,uid=1000 GITHUB_NPM_TOKEN=$(cat /run/secrets/GITHUB_NPM_TOKEN) yarn --frozen-lockfile --production=true


##### dev-deps #####
# Install dev dependencies, and copy in source.
####################
FROM        prod-deps AS dev-deps
RUN         --mount=type=secret,id=GITHUB_NPM_TOKEN,required=true,uid=1000 GITHUB_NPM_TOKEN=$(cat /run/secrets/GITHUB_NPM_TOKEN) yarn --frozen-lockfile --production=false
COPY        src ./src


##### dev #######
# Run as a hot-reloading service.
#################
FROM        dev-deps as dev
COPY        nodemon.json ./
ENV         PORT 8080
EXPOSE      8080
ENTRYPOINT  ["yarn", "start"]


##### lint-and-test #####
# Install unit testing and linting configs
# and configure to run tests
#########################
FROM        dev-deps as lint-and-test

COPY        .eslintrc.yml jest.config.json graphql-schema-linter.config.js ./
COPY        lib ./lib
COPY        e2e ./e2e

ENTRYPOINT  ["yarn", "test"]


##### integration-tests #####
# Install unit testing and linting configs
# and configure to run tests
#############################
FROM        lint-and-test as integration-tests

COPY        tsconfig.json ./

ENTRYPOINT ["yarn", "test:integration"]


##### build-artifacts #####
# Build production-ready artifacts
###########################
FROM        dev-deps as build-artifacts
RUN         --mount=type=secret,id=GITHUB_NPM_TOKEN,required=true,uid=1000 GITHUB_NPM_TOKEN=$(cat /run/secrets/GITHUB_NPM_TOKEN) yarn build


##### deploy #####
# Grab production node dependencies and
# production build artifacts
# and run as a service.
##################
FROM        086679231553.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:node20 AS deploy

ARG         GIT_COMMIT
ARG         REPOSITORY_URL
ARG         VERSION

WORKDIR     /var/app

# Copy production dependencies
COPY        --from=prod-deps --chown=node:node /var/app/node_modules ./node_modules
# Copy build
COPY        --from=build-artifacts --chown=node:node /var/app/build ./

ENV         PORT 8080
EXPOSE      8080

# Configure datadog environment variables
ENV         DD_GIT_COMMIT_SHA=$GIT_COMMIT
ENV         DD_GIT_REPOSITORY_URL=$REPOSITORY_URL
ENV         DD_VERSION=$VERSION

CMD         ["node", "index.js"]
