Skip to content

Commit

Permalink
chore: optimize Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
guesant committed Nov 5, 2024
1 parent 2b07853 commit 198c616
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 105 deletions.
6 changes: 1 addition & 5 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,5 @@
"@ladesa-ro/api-client-fetch.docs": "1.0.0-alpha.27",
"@ladesa-ro/api.integrations.openapi-json": "1.0.0-alpha.27"
},
"changesets": [
"curly-seas-fix",
"honest-drinks-visit",
"olive-stingrays-camp"
]
"changesets": ["curly-seas-fix", "honest-drinks-visit", "olive-stingrays-camp"]
}
57 changes: 41 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
ARG GIT_COMMIT_HASH
ARG PATH_SOURCE=/tmp/ldsa/.source
ARG PATH_LOCAL_BUILDS=/tmp/ldsa/.builds
ARG NX_CACHE_DIRECTORY=/tmp/ldsa/.cache/nx
ARG PATH_FINAL_BUILDS=/usr/local/ladesa-ro/services/

# ========================================
# BASE NODEJS IMAGE
# ========================================

FROM node:23 AS base
ARG PATH_SOURCE

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"

RUN corepack enable
WORKDIR "/ldsa"

WORKDIR "${PATH_SOURCE}"
COPY package.json .
RUN corepack install

Expand All @@ -15,49 +25,64 @@ RUN corepack install
# ========================================

FROM base AS build
COPY . "/ldsa"
ARG PATH_LOCAL_BUILDS
ARG GIT_COMMIT_HASH
ARG NX_CACHE_DIRECTORY

ENV NX_DAEMON=true
ENV GIT_COMMIT_HASH=
# ENV NX_VERBOSE_LOGGING=true
ENV GIT_COMMIT_HASH=${GIT_COMMIT_HASH}
ENV NX_CACHE_DIRECTORY=${NX_CACHE_DIRECTORY}

COPY . "${PATH_SOURCE}"

RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
RUN pnpm exec nx daemon --start
RUN pnpm exec nx daemon status
RUN cat /ldsa/.nx/workspace-data/d/daemon.log
RUN pnpm run -w build

RUN --mount=type=cache,id=nx,target=${NX_CACHE_DIRECTORY},sharing=locked pnpm run -w build

# ========================================
# BUILD / API-SERVICE
# ========================================

FROM build AS build-api-service
ARG NX_CACHE_DIRECTORY

RUN pnpm exec nx run @ladesa-ro/api.service:build
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm deploy --prod --filter=@ladesa-ro/api.service "${PATH_LOCAL_BUILDS}/api-service"

RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm deploy --prod --filter=@ladesa-ro/api.service "/ldsa/.builds/api-service"
# ========================================
# BUILD / NPM / API-CLIENT-FETCH / DOCS
# ========================================

FROM build AS build-npm-api-client-fetch-docs

RUN pnpm exec nx run @ladesa-ro/api-client-fetch.docs:build

RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm deploy --prod --filter=@ladesa-ro/api-client-fetch.docs "/ldsa/.builds/npm-api-client-fetch.docs"
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm deploy --prod --filter=@ladesa-ro/api-client-fetch.docs "${PATH_LOCAL_BUILDS}/npm-api-client-fetch.docs"

# ========================================
# RUNTIME / API-SERVICE
# ========================================

FROM base AS api-service
ARG PATH_LOCAL_BUILDS
ARG PATH_FINAL_BUILDS

COPY --from=build-api-service \
"/ldsa/.builds/api-service" \
"/ldsa/api-service"
WORKDIR "/ldsa/api-service"
"${PATH_LOCAL_BUILDS}/api-service" \
"${PATH_FINAL_BUILDS}/api-service"
WORKDIR "${PATH_FINAL_BUILDS}/api-service"

CMD pnpm run migration:run && pnpm run start:prod

# ========================================
# RUNTIME / NPM / API-CLIENT-FETCH / DOCS
# ========================================

FROM nginx:alpine AS npm-api-client-fetch-docs
ARG PATH_LOCAL_BUILDS
ARG PATH_FINAL_BUILDS

COPY \
./integrations/npm/api-client-fetch-docs/nginx.conf \
/etc/nginx/nginx.conf

COPY --from=build-npm-api-client-fetch-docs "/ldsa/.builds/npm-api-client-fetch.docs" "/ldsa/npm-api-client-fetch-docs"
COPY --from=build-npm-api-client-fetch-docs "${PATH_LOCAL_BUILDS}/npm-api-client-fetch.docs" "${PATH_FINAL_BUILDS}/npm-api-client-fetch-docs"
EXPOSE 80
1 change: 1 addition & 0 deletions Dockerfile.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ dist
.wireit

volumes
.nx
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ setup:
$(shell (cd .; find . -type f -name "*.example" -exec sh -c 'cp -n {} $$(basename {} .example)' \;))
$(shell (bash -c "docker network create $(d_network) &>/dev/null"))

echo "baixando imagens base dos containers (node e postgres), isso pode levar alguns minutos..."
docker compose $(compose_options) build -q

prepare:
Expand Down
2 changes: 1 addition & 1 deletion integrations/npm/api-client-fetch-docs/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ http {

server_name localhost;

root /ldsa/npm-api-client-fetch-docs/dist;
root /usr/local/ladesa-ro/services/npm-api-client-fetch-docs/dist;

location / {
include /etc/nginx/mime.types;
Expand Down
30 changes: 7 additions & 23 deletions integrations/npm/api-client-fetch-docs/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"name": "@ladesa-ro/api-client-fetch.docs",
"version": "1.0.0-next.29",
"files": [
"dist/**/*.*"
],
"files": ["dist/**/*.*"],
"private": true,
"scripts": {
"generate": "nx generate",
Expand All @@ -12,15 +10,10 @@
"serve": "nx serve"
},
"nx": {
"tags": [
"integrations",
"integrations:npm"
],
"tags": ["integrations", "integrations:npm"],
"root": "integrations/npm/api-client-fetch-docs",
"namedInputs": {
"default": [
"{projectRoot}/.config/**/*"
]
"default": ["{projectRoot}/.config/**/*"]
},
"targets": {
"serve": {
Expand All @@ -30,9 +23,7 @@
"cwd": "{projectRoot}",
"command": "serve dist"
},
"dependsOn": [
"build"
]
"dependsOn": ["build"]
},
"build": {
"cache": false,
Expand All @@ -43,25 +34,18 @@
"command": "echo noop"
},
"outputs": [],
"dependsOn": [
"generate"
]
"dependsOn": ["generate"]
},
"generate": {
"cache": true,
"executor": "nx:run-commands",
"inputs": [
"default",
"^default"
],
"inputs": ["default", "^default"],
"options": {
"cwd": "{projectRoot}",
"command": "typedoc --options .config/typedoc.jsonc"
},
"outputs": [],
"dependsOn": [
"@ladesa-ro/api-client-fetch:build"
]
"dependsOn": ["@ladesa-ro/api-client-fetch:build"]
}
}
},
Expand Down
52 changes: 11 additions & 41 deletions integrations/npm/api-client-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
"version": "1.0.0-next.29",
"main": "dist/index.js",
"license": "MIT",
"files": [
"*.md",
"dist/**/*.*"
],
"files": ["*.md", "dist/**/*.*"],
"description": "LADESA - Biblioteca de integração a API - Pacote NPM.",
"scripts": {
"prebuild": "rimraf dist",
Expand All @@ -18,33 +15,21 @@
"cleanup:generated": "rimraf ./src/http/generated"
},
"nx": {
"tags": [
"integrations",
"integrations:npm"
],
"tags": ["integrations", "integrations:npm"],
"root": "integrations/npm/api-client-fetch",
"namedInputs": {
"default": [
"{projectRoot}/src/**/*",
"{projectRoot}/{package.json,openapi-ts.config.mjs,tsconfig.json}"
]
"default": ["{projectRoot}/src/**/*", "{projectRoot}/{package.json,openapi-ts.config.mjs,tsconfig.json}"]
},
"targets": {
"build": {
"executor": "nx:run-commands",
"inputs": [
"default"
],
"inputs": ["default"],
"options": {
"cwd": "{projectRoot}",
"command": "tsc -p tsconfig.json"
},
"outputs": [
"{projectRoot}/dist/**/*"
],
"dependsOn": [
"generate"
]
"outputs": ["{projectRoot}/dist/**/*"],
"dependsOn": ["generate"]
},
"generate": {
"cache": false,
Expand All @@ -55,26 +40,18 @@
"command": "echo noop"
},
"outputs": [],
"dependsOn": [
"generate:client"
]
"dependsOn": ["generate:client"]
},
"generate:client": {
"cache": true,
"executor": "nx:run-commands",
"inputs": [
"{workspaceRoot}/integrations/openapi-json/generated.json"
],
"inputs": ["{workspaceRoot}/integrations/openapi-json/generated.json"],
"options": {
"cwd": "{projectRoot}",
"command": "pnpm run cleanup:generated && openapi-ts && pnpm run -w code-static:fix $(pwd)"
},
"outputs": [
"{projectRoot}/src/http/generated/**/*"
],
"dependsOn": [
"@ladesa-ro/api.integrations.openapi-json:build"
]
"outputs": ["{projectRoot}/src/http/generated/**/*"],
"dependsOn": ["@ladesa-ro/api.integrations.openapi-json:build"]
}
}
},
Expand All @@ -96,12 +73,5 @@
"bugs": {
"url": "https://github.com/ladesa-ro/api/issues"
},
"keywords": [
"ladesa-ro",
"api",
"api-client",
"fetch",
"sisgha",
"sisgea"
]
"keywords": ["ladesa-ro", "api", "api-client", "fetch", "sisgha", "sisgea"]
}
21 changes: 5 additions & 16 deletions integrations/openapi-json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
"name": "@ladesa-ro/api.integrations.openapi-json",
"version": "1.0.0-next.29",
"private": true,
"files": [
"generated.json"
],
"files": ["generated.json"],
"scripts": {
"build": "nx build"
},
Expand All @@ -15,30 +13,21 @@
"typedoc-plugin-mdn-links": "^3.3.6"
},
"nx": {
"tags": [
"integrations",
"integrations:openapi-json"
],
"tags": ["integrations", "integrations:openapi-json"],
"root": "integrations/openapi-json",
"namedInputs": {
"default": []
},
"targets": {
"build": {
"executor": "nx:run-commands",
"inputs": [
"default"
],
"inputs": ["default"],
"options": {
"cwd": "{projectRoot}",
"command": "OUT_FILE=$(pwd)/generated.json pnpm run --filter '@ladesa-ro/api.service' generate:openapi && pnpm run -w code-static:fix $(pwd)/generated.json"
},
"outputs": [
"{projectRoot}/generated.json"
],
"dependsOn": [
"@ladesa-ro/api.service:build"
]
"outputs": ["{projectRoot}/generated.json"],
"dependsOn": ["@ladesa-ro/api.service:build"]
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
}
},
"commitlint": {
"extends": [
"@commitlint/config-conventional"
]
"extends": ["@commitlint/config-conventional"]
},
"lint-staged": {
"**/*": "pnpm run code-static:fix"
Expand Down

0 comments on commit 198c616

Please sign in to comment.