Skip to content

Commit

Permalink
allow overriding the build command
Browse files Browse the repository at this point in the history
  • Loading branch information
rubys committed Sep 4, 2023
1 parent c3801de commit 936dc6b
Show file tree
Hide file tree
Showing 7 changed files with 788 additions and 6 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ Options are saved between runs into `package.json`. To invert a boolean options,

### Options:

* `--build=defer` - 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.
* `--cmd` - CMD to use in Dockerfile
* `--build=CMD` - command to be used to build your application.
* `--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.
* `--distroless` - use [distroless](https://github.com/GoogleContainerTools/distroless) base image to reduce image size
* `--entrypoint` - ENTRYPOINT to use in Dockerfile
Expand Down
7 changes: 4 additions & 3 deletions gdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,8 @@ export class GDF {

// Is there a build script?
get build() {
return !!this.#pj.scripts?.build
if (this.options.build) return this.options.build
if (this.#pj.scripts?.build) return `${this.packager} run build`
}

// Descriptive form of detected runtime
Expand Down Expand Up @@ -657,9 +658,9 @@ export class GDF {
this.#pj = JSON.parse(fs.readFileSync(path.join(appdir, 'package.json'), 'utf-8'))

// backwards compatibility with previous definition of --build=defer
if (options.build == "defer") {
if (options.build === 'defer') {
options.deferBuild = true
options.build = ""
options.build = ''
}

// install modules needed to run
Expand Down
2 changes: 1 addition & 1 deletion templates/Dockerfile.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ COPY<% if (options.link) { %> --link<% } %> . .

<% if (build && !options.deferBuild) { -%>
# Build application
RUN <%- mountSecrets %><%= packager %> run build
RUN <%- mountSecrets %><%= build %>
<% if (devDependencies && !options.dev && !nestjs && !adonisjs) { -%>
# Remove development dependencies
Expand Down
6 changes: 6 additions & 0 deletions test/base/build/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.git
/node_modules
.dockerignore
.env
Dockerfile
fly.toml
45 changes: 45 additions & 0 deletions test/base/build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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-lock.json package.json ./
RUN npm ci --include=dev

# Copy application code
COPY --link . .

# Build application
RUN npm run tsc

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


# 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 [ "npm", "run", "start" ]
Loading

0 comments on commit 936dc6b

Please sign in to comment.