Skip to content

Commit

Permalink
fix: docker build (#937)
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Bodin authored Apr 18, 2022
1 parent f0946d3 commit 41ce69b
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 20 deletions.
28 changes: 28 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
nodeLinker: node-modules

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
spec: "@yarnpkg/plugin-workspace-tools"

yarnPath: .yarn/releases/yarn-3.2.0.cjs
44 changes: 32 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,31 +1,51 @@
# ---- Base ----
FROM node:16.13.1-alpine AS base

ENV NODE_ENV production
# ------------------
# package.json cache
# ------------------
FROM apteno/alpine-jq:2022-03-27 AS deps

# Install dependencies for native deps
RUN apk add --no-cache bash python3
# To prevent cache invalidation from changes in fields other than dependencies
COPY package.json /tmp
RUN jq 'walk(if type == "object" then with_entries(select(.key | test("^jest|prettier|eslint|semantic|dotenv|nodemon|renovate") | not)) else . end) | { name, dependencies, devDependencies, packageManager }' < /tmp/package.json > /tmp/deps.json

# ------------------
# New base image
# ------------------
FROM base as tmp

ENV IN_DOCKER true
ENV PLAYWRIGHT_BROWSERS_PATH="/ms-playwright"
ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD="true"

# Setup the app WORKDIR
WORKDIR /app
WORKDIR /app/tmp

# Copy and install dependencies separately from the app's code
# To leverage Docker's cache when no dependency has change
COPY package.json yarn.lock ./
COPY --from=deps /tmp/deps.json ./package.json
COPY yarn.lock .yarnrc.yml ./
COPY .yarn .yarn

# Install dependencies for native deps
RUN apk add --no-cache bash python3

# Install dev dependencies
RUN true \
# && yarn set version berry \
&& yarn install --production=false
# Use local version instead of letting yarn auto upgrade itself
&& yarn set version $(ls -d $PWD/.yarn/releases/*) \
&& yarn install

# This step will invalidates cache
COPY . /app
RUN ls -lah /app
COPY . ./
RUN ls -lah /app/tmp

# Builds the code and reinstall node_modules in prod mode
RUN true \
&& yarn build \
&& yarn install --production=true \
# Finally remove all dev packages
&& yarn workspaces focus --all --production \
&& rm -rf src/ \
&& rm -rf .yarn/

Expand All @@ -41,8 +61,8 @@ USER node

WORKDIR /app

COPY --from=base --chown=node:node /app /app
COPY --from=tmp --chown=node:node /app/tmp /app

EXPOSE 8000

CMD [ "npm", "start" ]
CMD [ "node", "dist/src/index.js" ]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"build:hot": "tsc -w --preserveWatchOutput",
"build": "tsc -b",
"clean": "rm -rf dist/",
"dev": "node -r dotenv/config --async-stack-traces --max-old-space-size=920 dist/index.js",
"dev": "node -r dotenv/config --async-stack-traces --max-old-space-size=920 dist/src/index.js",
"lint": "eslint --ext=jsx,ts,tsx,js .",
"prepare": "husky install",
"start": "UV_THREADPOOL_SIZE=64 node --async-stack-traces --max-old-space-size=920 dist/index.js",
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type http from 'http';

import { nextTick } from 'async';

import { version } from '../package.json';

import { StateManager } from './StateManager';
import * as algolia from './algolia/index';
import { createAPI } from './api';
Expand All @@ -22,7 +24,7 @@ class Main {
healthApi: http.Server | undefined;

async run(): Promise<void> {
log.info('🗿 npm ↔️ Algolia replication starts ⛷ 🐌 🛰');
log.info('🗿 npm ↔️ Algolia replication starts ⛷ 🐌 🛰', { version });
let start = Date.now();

// We schedule to kill the process:
Expand Down Expand Up @@ -107,6 +109,7 @@ process.on('uncaughtException', (err) => {
try {
await main.run();
} catch (err) {
sentry.report(new Error('Error during run'), { err });
close();
}
})();
Expand Down
10 changes: 6 additions & 4 deletions src/utils/sentry.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as Sentry from '@sentry/node';

import { version } from '../../package.json';

import { log } from './log';

Sentry.init({
dsn: process.env.SENTRY_DSN,
release: `1.0.0`,
release: version,
environment: 'prod',
serverName: 'npm-search',
ignoreErrors: [
Expand All @@ -14,13 +16,13 @@ Sentry.init({
],
});

export function report(err, extra = {}): void {
log.error(err.message);
export function report(err: any, extra = {}): void {
if (!process.env.SENTRY_DSN) {
log.error(err);
log.error(err, extra);
return;
}

log.error(err.message);
Sentry.withScope((scope) => {
scope.setExtras(extra);
Sentry.captureException(err);
Expand Down
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
"removeComments": true, /* Do not emit comments to output. */
Expand Down Expand Up @@ -69,7 +69,8 @@
"resolveJsonModule": true,
},
"include": [
"src/*"
"src/*",
"package.json"
],
"exclude": [
"node_modules"
Expand Down

0 comments on commit 41ce69b

Please sign in to comment.