Skip to content

Commit

Permalink
Better caching in dockerfiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjsr committed Apr 25, 2024
1 parent 057de7c commit 3b0a71b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
23 changes: 10 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@ ARG ALPINE_VERSION=3.19
ARG NPM_VERSION=10.5.2
FROM ghcr.io/tjsr/node_patched_npm:${NODE_VERSION}-alpine${ALPINE_VERSION}-npm${NPM_VERSION} as tagtool-build-preflight

RUN mkdir /opt/tagtool
RUN --mount=type=cache,target=/root/.npm mkdir /opt/tagtool && npm config set fund false --location=global

WORKDIR /opt/tagtool

FROM tagtool-build-preflight as tagtool-build

COPY src/ /opt/tagtool/src
# First, files that are unlikely to change frequently.
COPY [ "tsconfig.json", ".npmrc", "babel.config.js", ".prettierrc.json", "vite.config.ts", "index.ts", "index.html", "jest.config.json", "eslint.config.mjs", "/opt/tagtool/" ]
# Then files that might.
COPY [ "package.json", "package-lock.json", "/opt/tagtool/" ]
COPY src /opt/tagtool/src
COPY public/ /opt/tagtool/public
COPY package*.json /opt/tagtool
COPY index.ts /opt/tagtool
COPY .eslintrc.json /opt/tagtool
COPY babel.config.js /opt/tagtool
COPY tsconfig.json /opt/tagtool
COPY .npmrc /opt/tagtool

RUN --mount=type=secret,id=github,target=/root/.npm/github_pat \
RUN --mount=type=secret,id=github,target=/root/.npm/github_pat --mount=type=cache,target=/root/.npm \
echo "//npm.pkg.github.com/:_authToken=$(cat /root/.npm/github_pat)" >> /root/.npmrc && \
npm install && \
npm run build && \
Expand All @@ -29,13 +27,12 @@ FROM tagtool-build-preflight as tagtool
COPY package*.json /opt/tagtool
COPY .npmrc /opt/tagtool

RUN --mount=type=secret,id=github,target=/root/.npm/github_pat \
echo "//npm.pkg.github.com/:_authToken=$(cat /root/.npm/github_pat)" >> /root/.npmrc && \
npm install --omit=dev && \
RUN --mount=type=secret,id=github,target=/root/.npm/github --mount=type=cache,target=/root/.npm \
echo "//npm.pkg.github.com/:_authToken=$(cat /root/.npm/github)" >> /root/.npmrc && \
npm install --omit=dev --no-fund && \
rm -f /root/.npmrc

COPY --from=tagtool-build /opt/tagtool/dist /opt/tagtool/dist
COPY --from=tagtool-build /opt/tagtool/build /opt/tagtool/dist/build
WORKDIR /opt/tagtool/dist

EXPOSE 8242
Expand Down
8 changes: 4 additions & 4 deletions Dockerfile.dbmigrate
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ ARG ALPINE_VERSION=3.19
ARG NPM_VERSION=10.5.2
FROM ghcr.io/tjsr/node_patched_npm:${NODE_VERSION}-alpine${ALPINE_VERSION}-npm${NPM_VERSION} as tagtool-dbmigrate

RUN mkdir /opt/tagtool
COPY package.json /opt/tagtool
COPY package-lock.json /opt/tagtool
RUN --mount=type=cache,target=/root/.npm mkdir /opt/tagtool && npm config set fund false --location=global

COPY [ "package.json", "package-lock.json", "/opt/tagtool/" ]
COPY prisma /opt/tagtool

WORKDIR /opt/tagtool
RUN --mount=type=secret,id=github,target=/root/.npm/github_pat \
RUN --mount=type=secret,id=github,target=/root/.npm/github_pat --mount=type=cache,target=/root/.npm \
echo "//npm.pkg.github.com/:_authToken=$(cat /root/.npm/github_pat)" >> /root/.npmrc && \
npm install && \
rm -f /root/.npmrc
Expand Down
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default tseslint.config( {
...tseslint.configs.recommended,
],
files: ['**/*.ts'],
ignores: ['dist/**'],
ignores: ['dist/**', 'src/generated'],
plugins: { 'prettier': eslintPluginPrettier },
rules: {
...google.rules,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"start:frontend": "cross-env GENERATE_SOURCEMAP=true PORT=3008 vite dev",
"start:dev": "nodemon index.ts && cross-env PORT=3008 vite dev",
"start:prod": "node index.js",
"build": "cross-env NODE_OPTIONS=--openssl-legacy-provider react-scripts build && tsc",
"build": "vite build && tsc",
"build:clean": "npm run clean && vite build && tsc",
"predocker:": "npx tsx bin/check-environment.ts TAGTOOL_GITHUB_PAT",
"docker:build": "npx tsx bin/docker-build-with-secret.ts tagtool Dockerfile",
"docker:build:dbmigrate": "npx tsx bin/docker-build-with-secret.ts tagtool-dbmigrate Dockerfile.dbmigrate",
Expand Down

0 comments on commit 3b0a71b

Please sign in to comment.