Skip to content

Commit

Permalink
install node during the BUILD stage of bun deploys
Browse files Browse the repository at this point in the history
This prevents failure when build scripts contain the inevitable:

  #!/usr/bin/env node

Doesn't yet seem to be a widespread problem for start scripts.
Bun will intercept the top script, the problem arises when
scripts call scripts.
  • Loading branch information
rubys committed Mar 20, 2024
1 parent a2b2ce4 commit 0f462ff
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
25 changes: 23 additions & 2 deletions gdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,13 @@ export class GDF {

// Packages needed for build stage
get buildPackages() {
const packages = ['node-gyp', 'pkg-config', 'build-essential', this.python]
const packages = ['pkg-config', 'build-essential', this.python]

if (!this.bun) {
packages.push('node-gyp')
} else if (this.bunNode) {
packages.push('ca-certificates', 'curl')
}

// https://docs.npmjs.com/cli/v10/configuring-npm/package-json#git-urls-as-dependencies
if (Object.values(this.#pj.dependencies || {}).some(value => /^git(\+|:|hub:)|^\w+\//.test(value))) {
Expand All @@ -256,6 +262,21 @@ export class GDF {
}
}

// Does the build script require node?
// This is just an approximation, but too many build scripts actually require node.
get bunNode() {
if (!this.bun) return false

const build = this.#pj.scripts?.build

if (build && fs.existsSync(`node_modules/.bin/${build.split(' ')[0]}`)) {
const script = fs.readFileSync(`node_modules/.bin/${build.split(' ')[0]}`, 'utf-8')
return /^#!.*node/.test(script)
}

return false
}

// packages needed for deploy stage
get deployPackages() {
const packages = [...this.options.packages.deploy]
Expand Down Expand Up @@ -405,7 +426,7 @@ export class GDF {

// what node version should be used?
get nodeVersion() {
const ltsVersion = '18.16.0'
const ltsVersion = '20.11.1'

return process.version.match(/\d+\.\d+\.\d+/)?.[0] || ltsVersion
}
Expand Down
6 changes: 6 additions & 0 deletions templates/Dockerfile.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ RUN <% if (options.cache) { %><%= pkg_cache %>
<% } %><%= pkg_update %> && \
<%= pkg_install %> <%= buildPackages.join(' ') %>

<% if (bunNode) { -%>
# Install Node.js
RUN curl -fsSL https://deb.nodesource.com/setup_<%= nodeVersion.split('.')[0] %>.x | bash - && \
apt-get install -y nodejs
<% } -%>
# Install node modules
COPY<% if (options.link) { %> --link<% } %> <%= packageFiles.join(' ') %> ./
RUN <%- buildCache %><%= packagerInstall %>
Expand Down
2 changes: 1 addition & 1 deletion test/base/bun/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FROM base as build

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

# Install node modules
COPY --link bun.lockb package.json ./
Expand Down
2 changes: 1 addition & 1 deletion test/base/cache-bun/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ FROM base as build
RUN --mount=type=cache,id=cache,target=/var/cache/apt \
--mount=type=cache,id=lib,target=/var/lib/apt \
apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential node-gyp pkg-config python-is-python3
apt-get install --no-install-recommends -y build-essential pkg-config python-is-python3

# Install node modules
COPY --link bun.lockb package.json ./
Expand Down

0 comments on commit 0f462ff

Please sign in to comment.