Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
nodejs: Add 'build' and 'prune' lifecycle events (#841)
Browse files Browse the repository at this point in the history
* nodejs: Add 'build' and 'prune' lifecycle events

* Add libssl and ca-certificates dependencies to prod image

* Document build and prune stages in README

* Fix typo
  • Loading branch information
djones6 authored Jul 30, 2020
1 parent e70730b commit 02c41f9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
14 changes: 14 additions & 0 deletions incubator/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ Templates are used to create your local project and start your development. When

**NOTE:** Currently the `appsody deploy` command only works for deploying web applications.

## Customizing the build

Simple Node projects do not require a 'build' process: they are executed directly from source. However, some technologies such as TypeScript require commands to be invoked to prepare the project ready to be run.

If your project's dependencies require an additional build step, you can do so as follows:
- In your `package.json`, edit the `"scripts"` section and define two new scripts:
- `"build": "npm install && <build commands>"`
- `"prune": "<cleanup commands> && npm prune"`
When initially launching your app using `appsody run`, or building the production image with `appsody build`, the 'build' script is executed (if one exists). Your build script should invoke `npm install` if any dependencies specified in your `devDependencies` are required as part of the build process - such as command line tools.
When generating the production image, the 'prune' script is executed after that, if it exists, and is run with the `--production` flag. This allows the removal of development dependencies that were required during the 'build' step. This could be as simple as running the `npm prune` command, which inherits the production flag, and restores the dependencies to a production state. You may also use this step to clean up any build artifacts that are not necessary in your production image.
## License
This stack is licensed under the [Apache 2.0](./image/LICENSE) license
2 changes: 1 addition & 1 deletion incubator/nodejs/image/Dockerfile-stack
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ENV APPSODY_WATCH_DIR=/project/user-app
ENV APPSODY_WATCH_IGNORE_DIR=/project/user-app/node_modules
ENV APPSODY_WATCH_REGEX="^.*.js$"

ENV APPSODY_PREP="npm install"
ENV APPSODY_PREP="npm install && npm run build --if-present"

ENV APPSODY_RUN="npm start --node-options --require=appmetrics-dash/attach"
ENV APPSODY_RUN_ON_CHANGE="npm start --node-options --require=appmetrics-dash/attach"
Expand Down
17 changes: 16 additions & 1 deletion incubator/nodejs/image/project/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,29 @@ RUN rm -rf /project/user-app/node_modules && mkdir -p /project/user-app/node_mod

# Install user-app dependencies
WORKDIR /project/user-app
RUN npm install --production
RUN npm install --production

# Run a build phase. Projects that need to execute build commands can customize the
# 'build' script in their package.json. The build script should call 'npm install'.
# If no build is required, just install production dependencies.
RUN npm run build --if-present

# Uninstall dev dependencies, leaving only production dependencies. Projects can
# customize the 'prune' script in their package.json.
# Ideally this would just be 'npm prune', but this command does not have pre/post
# hooks. Instead, the user's 'prune' script can itself call 'npm prune' in addition
# to any additional actions required.
RUN npm run prune --production --if-present

# Creating a tar to work around poor copy performance when using buildah 1.9.0
RUN cd / && tar czf project.tgz project

# Copy the dependencies into a slim Node docker image
FROM node:12-slim

# Install common dependencies (TLS and CA support)
RUN apt-get update && apt-get install -y libssl1.1 ca-certificates && apt-get clean

# Copy project with dependencies
COPY --chown=node:node --from=0 /project.tgz .
RUN tar xf project.tgz && chown -R node:node project && rm project.tgz
Expand Down
2 changes: 1 addition & 1 deletion incubator/nodejs/stack.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Node.js
version: 0.3.7
version: 0.4.0
description: Runtime for Node.js applications
license: Apache-2.0
language: nodejs
Expand Down

0 comments on commit 02c41f9

Please sign in to comment.