Skip to content

Commit dff68fd

Browse files
committed
refactor(lint-pr-title): modernise Dockerfile
The previous Dockerfile was overly complex and inefficient, a result of adapting an older style to a workspace environment. The multi-stage build was difficult to read and did not use modern Docker features for caching. To fix these problems, we simplify the build process into a more readable multi-stage build. We introduce Docker cache mounts for dependency installation, which will result in significantly faster builds. The COPY commands are now more specific to avoid copying unnecessary files into the image. Tests continue to be run as a non-root user to ensure file permission tests work correctly, but in a more streamlined way.
1 parent 1acdfbb commit dff68fd

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

actions/lint-pr-title/Dockerfile

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,52 @@
11
# This file must be built from the root of the repository.
22
# Example: docker build . -f actions/lint-pr-title/Dockerfile
3+
34
FROM oven/bun:1.2.18@sha256:2cdd9c93006af1b433c214016d72a3c60d7aa2c75691cb44dfd5250aa379986b AS base
45

5-
# Create a non-root user with a home directory
6-
# Run tests as non-root user because one test expects EACCES when writing to a
7-
# read-only file, and this will not fail as root.
8-
RUN useradd -ms /bin/bash -d /home/newuser newuser
6+
WORKDIR /usr/src/app
7+
8+
# Create a non-root user to run the tests. Run tests as non-root user because
9+
# one test expects EACCES when writing to a read-only file, and this will not
10+
# fail as root.
11+
RUN useradd -ms /bin/bash newuser
12+
13+
# Install dependencies
14+
FROM base AS deps
15+
16+
COPY package.json bun.lock ./
17+
COPY actions/dependabot-auto-triage/package.json ./actions/dependabot-auto-triage/
18+
19+
# Because we use bun's workspaces, we need to have all package.json files
20+
# available, even if they're not used. (We filter to one workspace.)
21+
COPY actions/get-latest-workflow-artifact/package.json ./actions/get-latest-workflow-artifact/
22+
COPY actions/lint-pr-title/package.json ./actions/lint-pr-title/
23+
24+
RUN --mount=type=cache,target=/root/.bun/install/cache \
25+
bun install --frozen-lockfile --filter lint-pr-title
26+
27+
# Run tests
28+
FROM base AS test
29+
30+
COPY --from=deps /usr/src/app/node_modules ./node_modules
31+
COPY --chown=newuser:newuser actions/lint-pr-title/ ./actions/lint-pr-title/
932
USER newuser
10-
WORKDIR /home/newuser
11-
12-
FROM base AS install
13-
RUN mkdir -p temp/dev
14-
WORKDIR /home/newuser/temp/dev
15-
COPY --chown=newuser:newuser package.json bun.lock ./
16-
COPY --chown=newuser:newuser actions/dependabot-auto-triage/package.json ./actions/dependabot-auto-triage/
17-
COPY --chown=newuser:newuser actions/get-latest-workflow-artifact/package.json ./actions/get-latest-workflow-artifact/
18-
COPY --chown=newuser:newuser actions/lint-pr-title/package.json ./actions/lint-pr-title/
19-
RUN bun install --frozen-lockfile --filter lint-pr-title
20-
21-
# install with --production (exclude devDependencies)
22-
RUN mkdir -p temp/prod
23-
WORKDIR /home/newuser/temp/prod
24-
COPY --chown=newuser:newuser package.json bun.lock ./
25-
COPY --chown=newuser:newuser actions/dependabot-auto-triage/package.json ./actions/dependabot-auto-triage/
26-
COPY --chown=newuser:newuser actions/get-latest-workflow-artifact/package.json ./actions/get-latest-workflow-artifact/
27-
COPY --chown=newuser:newuser actions/lint-pr-title/package.json ./actions/lint-pr-title/
28-
RUN bun install --frozen-lockfile --production --filter lint-pr-title
29-
30-
FROM base AS prerelease
31-
WORKDIR /home/newuser/app
32-
COPY --from=install /home/newuser/temp/dev/node_modules ./node_modules
33-
COPY --chown=newuser:newuser . .
34-
35-
# Install dev dependencies to run the tests
33+
3634
ENV NODE_ENV=development
35+
3736
RUN bun run --filter lint-pr-title test
3837

39-
FROM base AS release
38+
# Assemble final image from a clean stage
39+
FROM base
40+
4041
USER root
42+
4143
WORKDIR /usr/src/app
42-
COPY --from=install /home/newuser/temp/prod/node_modules ./node_modules
43-
COPY --from=prerelease /home/newuser/app/actions/lint-pr-title/src ./actions/lint-pr-title/src
44-
COPY --from=prerelease /home/newuser/app/actions/lint-pr-title/package.json ./actions/lint-pr-title/
45-
COPY --from=prerelease /home/newuser/app/actions/lint-pr-title/commitlint.config.js ./actions/lint-pr-title/
44+
45+
COPY --from=deps /usr/src/app/node_modules ./node_modules
46+
COPY actions/lint-pr-title/src ./actions/lint-pr-title/src
47+
COPY actions/lint-pr-title/package.json ./actions/lint-pr-title/
48+
COPY actions/lint-pr-title/commitlint.config.js ./actions/lint-pr-title/
4649

4750
WORKDIR /usr/src/app/actions/lint-pr-title
51+
4852
ENTRYPOINT [ "bun", "run", "src/index.ts" ]

0 commit comments

Comments
 (0)