Skip to content

Commit

Permalink
chore(infra): Move ARG declaration for clarity (#17054)
Browse files Browse the repository at this point in the history
* Move ARG usage for cache-bust minimization

* Fewer lines changed with `ENV` move

* Less-changed ARGs

* Add 'ARG APP' to all "Stage-specific ARGs"

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
AndesKrrrrrrrrrrr and kodiakhq[bot] authored Dec 3, 2024
1 parent 02f2aa4 commit 1dfbe91
Showing 1 changed file with 60 additions and 12 deletions.
72 changes: 60 additions & 12 deletions scripts/ci/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# This is a multi-stage Dockerfile which contains all CI-related operations as well as images to be deployed in production

# Global ARGs (used across multiple stages)
ARG PLAYWRIGHT_VERSION
ARG DOCKER_ECR_REGISTRY=public.ecr.aws/docker
ARG DOCKER_IMAGE_REGISTRY=${DOCKER_ECR_REGISTRY%/docker}
# Alias DOCKER_IMAGE_REGISTRY to DOCKER_REGISTRY for backwards compatibility
ARG DOCKER_REGISTRY=${DOCKER_IMAGE_REGISTRY}/docker
ARG NODE_IMAGE_TAG
ARG APP_HOME
ARG APP_DIST_HOME=dist/${APP_HOME}
ARG APP

# Base image for dependencies
FROM ${DOCKER_REGISTRY}/library/node:${NODE_IMAGE_TAG} AS deps

# hadolint ignore=DL3018
Expand All @@ -21,6 +27,7 @@ COPY .yarn/ ./.yarn
RUN apk add --update --no-cache python3 build-base gcc && ln -sf /usr/bin/python3 /usr/bin/python
RUN CI=true yarn install --immutable

# Image with source code
FROM deps AS src

RUN wget -O /tmp/jq-linux64 https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 && \
Expand All @@ -30,7 +37,11 @@ RUN wget -O /tmp/jq-linux64 https://github.com/stedolan/jq/releases/download/jq-
# image with the source code
COPY . .


# Build stage
FROM src AS builder

# Stage-specific ARGs
ARG APP
ARG APP_DIST_HOME
ENV APP=${APP}
Expand All @@ -39,6 +50,7 @@ ENV NODE_OPTIONS="--max-old-space-size=8192"

RUN yarn run build ${APP} --prod


# This is base image for containers that are to be deployed
FROM ${DOCKER_REGISTRY}/library/node:${NODE_IMAGE_TAG} AS output-base
ARG APP
Expand All @@ -64,9 +76,14 @@ USER runner

FROM output-base-with-pg AS output-express

# Stage-specific ARGs
ARG APP_DIST_HOME
ARG GIT_BRANCH
ARG GIT_COMMIT_SHA
ARG GIT_REPOSITORY_URL

COPY --from=builder /build/${APP_DIST_HOME} /webapp/

ARG GIT_BRANCH GIT_COMMIT_SHA GIT_REPOSITORY_URL
ENV GIT_BRANCH=${GIT_BRANCH} GIT_COMMIT_SHA=${GIT_COMMIT_SHA} GIT_REPOSITORY_URL=${GIT_REPOSITORY_URL}
ENV DD_GIT_BRANCH=${GIT_BRANCH} DD_GIT_COMMIT_SHA=${GIT_COMMIT_SHA} DD_GIT_REPOSITORY_URL=${GIT_REPOSITORY_URL}
LABEL branch=${GIT_BRANCH}
Expand All @@ -75,24 +92,35 @@ ENTRYPOINT []
CMD [ "node", "--no-experimental-fetch", "main.js" ]

FROM output-base-with-pg AS output-next

# Stage-specific ARGs
ARG APP
ARG APP_DIST_HOME
ARG GIT_BRANCH
ARG GIT_COMMIT_SHA
ARG GIT_REPOSITORY_URL

ENV PORT=4200

# TODO: smallify
COPY --from=deps /build/node_modules /webapp/node_modules
COPY --from=builder /build/${APP_DIST_HOME} /webapp/

ARG GIT_BRANCH GIT_COMMIT_SHA GIT_REPOSITORY_URL
ENV GIT_BRANCH=${GIT_BRANCH} GIT_COMMIT_SHA=${GIT_COMMIT_SHA} GIT_REPOSITORY_URL=${GIT_REPOSITORY_URL}
ENV DD_GIT_BRANCH=${GIT_BRANCH} DD_GIT_COMMIT_SHA=${GIT_COMMIT_SHA} DD_GIT_REPOSITORY_URL=${GIT_REPOSITORY_URL}
LABEL branch=${GIT_BRANCH}
LABEL commit=${GIT_COMMIT_SHA}
ENTRYPOINT [ "node", "main.js" ]


FROM ${DOCKER_REGISTRY}/library/nginx:1.21-alpine AS output-static

# Stage-specific ARGs
ARG APP
ARG APP_DIST_HOME
ENV APP=${APP}
ENV BASEPATH=/
ARG GIT_BRANCH
ARG GIT_COMMIT_SHA
ARG GIT_REPOSITORY_URL

RUN mkdir -p /etc/nginx/templates
# hadolint ignore=DL3018
Expand All @@ -105,14 +133,23 @@ COPY scripts/dockerfile-assets/bash/extract-environment.sh /docker-entrypoint.d
COPY scripts/dockerfile-assets/bash/extract-environment.js /docker-entrypoint.d
COPY --from=builder /build/${APP_DIST_HOME} /usr/share/nginx/html

ARG GIT_BRANCH GIT_COMMIT_SHA GIT_REPOSITORY_URL
ENV GIT_BRANCH=${GIT_BRANCH} GIT_COMMIT_SHA=${GIT_COMMIT_SHA} GIT_REPOSITORY_URL=${GIT_REPOSITORY_URL}
ENV DD_GIT_BRANCH=${GIT_BRANCH} DD_GIT_COMMIT_SHA=${GIT_COMMIT_SHA} DD_GIT_REPOSITORY_URL=${GIT_REPOSITORY_URL}
LABEL branch=${GIT_BRANCH}
LABEL commit=${GIT_COMMIT_SHA}

ENV APP=${APP}
ENV BASEPATH=/

FROM output-base AS output-jest

# Stage-specific ARGs
ARG APP
ARG APP_DIST_HOME
ARG GIT_BRANCH
ARG GIT_COMMIT_SHA
ARG GIT_REPOSITORY_URL

RUN echo 'module.exports = {};' > jest.config.js

# hadolint ignore=DL3016
Expand All @@ -122,19 +159,26 @@ COPY --from=builder /build/${APP_DIST_HOME} /webapp/

USER runner

ARG GIT_BRANCH GIT_COMMIT_SHA GIT_REPOSITORY_URL
ENV GIT_BRANCH=${GIT_BRANCH} GIT_COMMIT_SHA=${GIT_COMMIT_SHA} GIT_REPOSITORY_URL=${GIT_REPOSITORY_URL}
ENV DD_GIT_BRANCH=${GIT_BRANCH} DD_GIT_COMMIT_SHA=${GIT_COMMIT_SHA} DD_GIT_REPOSITORY_URL=${GIT_REPOSITORY_URL}
LABEL branch=${GIT_BRANCH}
LABEL commit=${GIT_COMMIT_SHA}
CMD [ "jest", "main.spec.js" ]



FROM mcr.microsoft.com/playwright:v${PLAYWRIGHT_VERSION}-focal AS playwright-base

FROM playwright-base AS output-playwright

# Stage-specific ARGs
ARG APP
ARG APP_DIST_HOME
ARG APP_HOME
ARG PLAYWRIGHT_BROWSER=chromium
ARG GIT_BRANCH
ARG GIT_COMMIT_SHA
ARG GIT_REPOSITORY_URL

# TODO: remove awscli dependency (157 MB extra)

# hadolint ignore=DL3008
Expand All @@ -155,12 +199,10 @@ RUN mkdir ./.yarn
COPY .yarn/releases ./.yarn/releases
RUN yarn install

ENV PLAYWRIGHT_BROWSER=chromium
RUN yarn playwright install ${PLAYWRIGHT_BROWSER}

COPY --chown=pwuser:pwuser --chmod=0755 ${APP_HOME}/entrypoint.sh .

ARG GIT_BRANCH GIT_COMMIT_SHA GIT_REPOSITORY_URL
ENV GIT_BRANCH=${GIT_BRANCH} GIT_COMMIT_SHA=${GIT_COMMIT_SHA} GIT_REPOSITORY_URL=${GIT_REPOSITORY_URL}
ENV DD_GIT_BRANCH=${GIT_BRANCH} DD_GIT_COMMIT_SHA=${GIT_COMMIT_SHA} DD_GIT_REPOSITORY_URL=${GIT_REPOSITORY_URL}
LABEL branch=${GIT_BRANCH}
Expand All @@ -169,12 +211,17 @@ ENTRYPOINT ["./entrypoint.sh"]


FROM playwright-base AS output-local
ARG APP_HOME

# Stage-specific ARGs
ARG APP
ARG APP_DIST_HOME
ARG APP_HOME
ARG GIT_BRANCH
ARG GIT_COMMIT_SHA
ARG GIT_REPOSITORY_URL

WORKDIR ${APP_DIST_HOME}


# node user exists in the base image
RUN mkdir -p /out \
&& chown node:node /out
Expand All @@ -183,12 +230,13 @@ COPY --chown=pwuser:pwuser --chmod=0755 ${APP_HOME}/entrypoint.sh .

USER pwuser

ARG GIT_BRANCH GIT_COMMIT_SHA GIT_REPOSITORY_URL
ENV GIT_BRANCH=${GIT_BRANCH} GIT_COMMIT_SHA=${GIT_COMMIT_SHA} GIT_REPOSITORY_URL=${GIT_REPOSITORY_URL}
ENV DD_GIT_BRANCH=${GIT_BRANCH} DD_GIT_COMMIT_SHA=${GIT_COMMIT_SHA} DD_GIT_REPOSITORY_URL=${GIT_REPOSITORY_URL}
LABEL branch=${GIT_BRANCH}
LABEL commit=${GIT_COMMIT_SHA}
ENTRYPOINT ["./entrypoint.sh"]


FROM output-base AS output-native

RUN echo "not-implemented"

0 comments on commit 1dfbe91

Please sign in to comment.