Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(infra): Add --docker flag to yarn infra run-local-env #17677

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions infra/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const cli = yargs(process.argv.slice(2))
default: false,
alias: ['nosecrets', 'no-secrets'],
})
.option('docker', { type: 'boolean', default: false })
// Custom check for 'services' since yargs lack built-in validation
.check((argv) => {
const svc = argv.services
Expand All @@ -89,6 +90,7 @@ const cli = yargs(process.argv.slice(2))
json: argv.json,
print: true,
noUpdateSecrets: argv['no-update-secrets'],
docker: argv.docker,
})
},
)
Expand Down Expand Up @@ -118,6 +120,7 @@ const cli = yargs(process.argv.slice(2))
type: 'boolean',
default: false,
})
.option('docker', { type: 'boolean', default: false })
// Custom check for 'services' since yargs lack built-in validation
.check((argv) => {
const svc = argv.services
Expand All @@ -137,6 +140,7 @@ const cli = yargs(process.argv.slice(2))
noUpdateSecrets: argv['no-update-secrets'],
print: argv.print,
startProxies: argv.proxies,
docker: argv.docker,
})
},
)
Expand Down
37 changes: 37 additions & 0 deletions infra/src/cli/render-local-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ export async function renderLocalServices({
json = false,
dryRun = false,
noUpdateSecrets = false,
docker = false,
}: {
services: string[]
print?: boolean
json?: boolean
dryRun?: boolean
noUpdateSecrets?: boolean
docker?: boolean
}): Promise<LocalrunValueFile> {
logger.debug('renderLocalServices', {
services,
Expand All @@ -40,6 +42,38 @@ export async function renderLocalServices({
{ dryRun, noUpdateSecrets },
)

if (docker) {
const targetString = 'docker-*'
const targets = ...
logger.info('Target:', { targetString, target })

// Ensure the target name starts with "docker-"
const type = target.target.startsWith('docker-')
? target.target.replace('docker-', '')
: undefined

if (!type) {
throw new Error(
`Invalid target: ${targetString}. Expected a target starting with "docker-".`,
)
}
Object.entries(renderedLocalServices.services)
.map(([k, v]): typeof v => ({
...v,
commands: [
[
`docker buildx build`,
`--file="$PWD/scripts/ci/Dockerfile"`,
`--target=output-${type}`,
`--load`,
`--build-arg=APP=${k}`,
`--tag=${k}:local`,
].join(' '),
`dokcer run --rm -it --name=${k} --env-file=.env.${k} ${k}:local`,
],
}))
.map(console.error)
}
if (print) {
const commandedServices = Object.entries(
renderedLocalServices.services,
Expand All @@ -65,13 +99,15 @@ export async function runLocalServices(
json = false,
noUpdateSecrets = false,
startProxies = false,
docker = false,
}: {
dryRun?: boolean
neverFail?: boolean
print?: boolean
json?: boolean
noUpdateSecrets?: boolean
startProxies?: boolean
docker?: boolean
} = {},
) {
logger.debug('runLocalServices', { services, dependencies })
Expand All @@ -85,6 +121,7 @@ export async function runLocalServices(
json,
dryRun,
noUpdateSecrets,
docker,
})

// Verify that all dependencies exist in the rendered dependency list
Expand Down
35 changes: 21 additions & 14 deletions scripts/ci/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ COPY package.json yarn.lock .yarnrc.yml ./
COPY apps/native/app/package.json ./apps/native/app/
COPY .yarn/ ./.yarn

RUN CI=true yarn install --immutable && \
# Don't store the cache in the builder image
yarn cache clean
# hadolint ignore=DL3060
RUN --mount=type=cache,target=.yarn/cache \
# Cache-mount the relatively frequently changing caches
CI=true yarn install --immutable


# Image with source code
FROM deps AS src

# image with the source code
COPY . .
COPY . ./


# Build stage
Expand Down Expand Up @@ -70,8 +71,6 @@ ARG APP_DIST_HOME
ENV APP=${APP}
ENV NODE_ENV=production

WORKDIR /webapp

# Adding user for running the app
RUN addgroup runners && \
adduser --disabled-password runner --ingroup runners
Expand All @@ -93,14 +92,16 @@ FROM base-node AS output-base


FROM base-node-with-pg AS output-express
WORKDIR /webapp

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

COPY --from=builder /dist /webapp/
COPY --from=builder /dist ./
RUN ls -lah .

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}
Expand All @@ -110,6 +111,7 @@ ENTRYPOINT [ "node", "--no-experimental-fetch", "main.js" ]


FROM base-node-with-pg AS output-next
WORKDIR /webapp

# Stage-specific ARGs
ARG APP
Expand All @@ -121,8 +123,9 @@ ARG GIT_REPOSITORY_URL
ENV PORT=4200

# TODO: smallify
COPY --from=deps /build/node_modules /webapp/node_modules
COPY --from=builder /dist /webapp/
COPY --from=deps /build/node_modules ./
COPY --from=builder /dist ./
RUN ls -lah .

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}
Expand All @@ -147,11 +150,13 @@ RUN apk add --no-cache bash nodejs && \


FROM base-static AS output-static
WORKDIR /usr/share/nginx/html

COPY scripts/dockerfile-assets/nginx/* /etc/nginx/templates
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 /dist /usr/share/nginx/html
COPY --from=builder /dist ./
RUN ls -lah .

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}
Expand All @@ -178,8 +183,10 @@ RUN echo 'module.exports = {};' > jest.config.js


FROM base-jest AS output-jest
WORKDIR /webapp

COPY --from=builder /dist /webapp/
COPY --from=builder /dist ./
RUN ls -lah .

USER runner

Expand All @@ -201,6 +208,7 @@ RUN apt-get update -y && \


FROM playwright-base AS output-playwright
WORKDIR /testing

# Stage-specific ARGs
ARG APP
Expand All @@ -211,8 +219,6 @@ ARG GIT_BRANCH
ARG GIT_COMMIT_SHA
ARG GIT_REPOSITORY_URL

WORKDIR /testing

COPY .yarnrc.yml ${APP_HOME}/package.json ./
COPY .yarn/releases ./.yarn/releases

Expand All @@ -221,7 +227,7 @@ RUN yarn install && \
yarn cache clean

COPY --from=builder /dist ./
COPY --chmod=0755 ${APP_HOME}/entrypoint.sh .
COPY --chmod=0755 ${APP_HOME}/entrypoint.sh ./

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}
Expand All @@ -232,5 +238,6 @@ ENTRYPOINT [ "./entrypoint.sh" ]


FROM base-node AS output-native
WORKDIR /native

RUN echo "not-implemented"
Loading