Skip to content

Commit

Permalink
instructions (base, build, deploy)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubys committed Jul 23, 2023
1 parent 73b5a06 commit 4c2ea2b
Show file tree
Hide file tree
Showing 11 changed files with 834 additions and 9 deletions.
22 changes: 21 additions & 1 deletion gdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ export const defaults = {
litefs: false,
port: 0,
swap: '',
windows: false
windows: false,

packages: { base: [], build: [], deploy: [] },
vars: { base: {}, build: {}, deploy: {} },
args: { base: {}, build: {}, deploy: {} },
instructions: { base: null, build: null, deploy: null }
}

const __dirname = url.fileURLToPath(new URL('.', import.meta.url))
Expand Down Expand Up @@ -479,6 +484,21 @@ export class GDF {

if (options.force) this.#answer = 'a'

// read instructions
for (const stage of ["base", "build", "deploy"]) {
if (options.instructions?.[stage]) {
try {
options.instructions[stage] = fs.readFileSync(
path.join(this._appdir, options.instructions[stage]),
"utf-8"
).trimEnd()
} catch(error) {
console.error(error)
options.instructions[stage] = ""
}
}
}

// select and render templates
const templates = {
'Dockerfile.ejs': 'Dockerfile'
Expand Down
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,20 @@ if (pj) {
}
}

Object.assign(defaults, pj.dockerfile)

const df = pj.dockerfile

options.packages = { ...defaults.packages, ...df.packages }
options.vars = { ...defaults.vars, ...df.envs }
options.args = { ...defaults.args, ...df.args }
options.instructions = { base: null, build: null, deploy: null, ...df.instructions }

Object.assign(defaults, df)

df.packages ||= {}
df.envs ||= {}
df.args ||= {}
df.instructions ||= {}

options.packages = { base: [], build: [], deploy: [], ...df.packages }
options.vars = { base: {}, build: {}, deploy: {}, ...df.envs }
options.args = { base: {}, build: {}, deploy: {}, ...df.args }
options.instructions = { base: null, build: null, deploy: null, ...df.instructions }

for (const stage of ['base', 'build', 'deploy']) {
// packages
for (const pkg of options[`add-${stage}`] || []) {
Expand Down
12 changes: 12 additions & 0 deletions templates/Dockerfile.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ RUN npm install -g yarn@$YARN_VERSION --force
ARG PNPM_VERSION=<%= pnpmVersion %>
RUN npm install -g pnpm@$PNPM_VERSION
<% } %>
<% if (options.instructions.base) { -%>
<%= options.instructions.base %>
<% } -%>
<% if (!distroless) { -%>
# Throw-away build stage to reduce size of final image
Expand Down Expand Up @@ -68,6 +72,10 @@ RUN <%= packager %> run build
RUN <%- packagerPrune %>
<% } -%>
<% } -%>
<% if (options.instructions.build) { -%>
<%= options.instructions.build %>
<% } -%>

# Final stage for app image
Expand Down Expand Up @@ -134,6 +142,10 @@ ENV DATABASE_URL="file:///<%= litefs ? 'litefs' : 'data' %>/sqlite.db"<% if (lit
RUN echo "#!/bin/sh\nset -x\nsqlite3 \$DATABASE_URL" > /usr/local/bin/database-cli && chmod +x /usr/local/bin/database-cli
<% } -%>
<% } -%>
<% if (options.instructions.deploy) { -%>
<%= options.instructions.deploy %>
<% } -%>
<% if (entrypoint) { -%>
<% if (entrypointFixups.length) { -%>
Expand Down
6 changes: 6 additions & 0 deletions test/base/instructions/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.git
/node_modules
.dockerignore
.env
Dockerfile
fly.toml
51 changes: 51 additions & 0 deletions test/base/instructions/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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

# base additions


# 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 python-is-python3 pkg-config build-essential

# Install node modules
COPY --link package-lock.json package.json ./
RUN npm ci --include=dev

# Copy application code
COPY --link . .

# Build application
RUN npm run build

# Remove development dependencies
RUN npm prune --omit=dev

# build additions


# Final stage for app image
FROM base

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

# deploy additions

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD [ "npm", "run", "start" ]
Loading

0 comments on commit 4c2ea2b

Please sign in to comment.