Skip to content

Commit

Permalink
rough in build cache
Browse files Browse the repository at this point in the history
  • Loading branch information
rubys committed Sep 6, 2023
1 parent 936dc6b commit cef3fa7
Show file tree
Hide file tree
Showing 8 changed files with 610 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Options are saved between runs into `package.json`. To invert a boolean options,
### Options:

* `--build=CMD` - command to be used to build your application.
* `--cache` - use build caching to speed up builds
* `--cmd=CMD` - CMD to use in Dockerfile
* `--defer-build` - may be needed when your build step requires access to secrets that are not available at image build time. Results in larger images and slower deployments.
* `--dev` - include `devDependencies` in the production image.
Expand Down
36 changes: 35 additions & 1 deletion gdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as ShellQuote from 'shell-quote'
// defaults for all the flags that will be saved
export const defaults = {
build: '',
cache: false,
cmd: '',
deferBuild: false,
dev: false,
Expand Down Expand Up @@ -203,6 +204,30 @@ export class GDF {
}
}

get buildCache() {
if (!this.options.cache) return ''

let id = 'npm'
let target = '/app/.npm'

if (this.yarn) {
id = 'yarn'
if (this.yarnVersion.startsWith('1.')) {
target = '/usr/local/share/.cache/yarn/'
} else {
target = '/app/.yarn/berry/cache'
}
} else if (this.pnpm) {
id = 'pnpm'
target = '/pnpm/store'
} else if (this.bun) {
id = 'bun'
target = '/root/.bun'
}

return `--mount=type=cache,id=${id},target=${target} \\\n `
}

get baseEnv() {
const env = {
NODE_ENV: 'production'
Expand All @@ -212,7 +237,16 @@ export class GDF {
}

get buildEnv() {
return { ...this.options.vars.build }
let env = {}

if (this.options.cache && this.pnpm) {
env = {
PNPM_HOME: '/pnpm',
PATH: '$PNPM_HOME:$PATH'
}
}

return { ...this.options.vars.build, ...env }
}

get deployEnv() {
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const options = yargs((hideBin(process.argv)))
describe: 'CMD to be used in the Dockerfile',
type: 'string'
})
.option('cache', {
describe: 'use build caching to speed up builds',
type: 'string'
})
.option('defer-build', {
describe: 'if true, run build at deploy time',
type: 'boolean'
Expand Down
4 changes: 2 additions & 2 deletions templates/Dockerfile.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ RUN apt-get update -qq && \

# Install node modules
COPY<% if (options.link) { %> --link<% } %> <%= packageFiles.join(' ') %> ./
RUN <%= packagerInstall %>
RUN <%- buildCache %><%= packagerInstall %>

<% if (prisma) { -%>
# Generate Prisma Client
Expand All @@ -89,7 +89,7 @@ RUN <%- mountSecrets %><%= build %>
<% if (devDependencies && !options.dev && !nestjs && !adonisjs) { -%>
# Remove development dependencies
RUN <%- packagerPrune %>
RUN <%- buildCache %><%- packagerPrune %>
<% } -%>
<% } -%>
Expand Down
6 changes: 6 additions & 0 deletions test/base/cache-yarn/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.git
/node_modules
.dockerignore
.env
Dockerfile
fly.toml
47 changes: 47 additions & 0 deletions test/base/cache-yarn/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# syntax = docker/dockerfile:1

# Adjust NODE_VERSION as desired
ARG NODE_VERSION=xxx
FROM node:${NODE_VERSION}-slim as base

LABEL fly_launch_runtime="Node.js"

# Node.js app lives here
WORKDIR /app

# Set production environment
ENV NODE_ENV="production"


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build node modules
RUN apt-get update -qq && \
apt-get install -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link package.json yarn.lock ./
RUN --mount=type=cache,id=yarn,target=/usr/local/share/.cache/yarn/ \
yarn install --frozen-lockfile --production=false

# Copy application code
COPY --link . .

# Build application
RUN yarn run build

# Remove development dependencies
RUN --mount=type=cache,id=yarn,target=/usr/local/share/.cache/yarn/ \
yarn install --production=true


# Final stage for app image
FROM base

# Copy built application
COPY --from=build /app /app

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD [ "yarn", "run", "start" ]
17 changes: 17 additions & 0 deletions test/base/cache-yarn/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"@types/express": "^4.17.17",
"@types/node": "^20.5.1",
"typescript": "^5.1.6"
},
"scripts": {
"build": "tsc",
"start": "node build/server.js"
},
"dockerfile": {
"cache": true
}
}
Loading

0 comments on commit cef3fa7

Please sign in to comment.