Custom (or latest) npm version in Google Cloud Builder

1.1k Views Asked by At

I'm using Google Cloud Builder (GCB) to build some Node.js code. I'm using npm's new package-lock.json feature to pin dependencies. This works best when using the npm ci command that was introduced in [email protected]. Unfortunately, all of GCB's npm images are currently set to [email protected]

How can I use a different npm version in GCB without creating a custom builder image?

Edit: It may not be the case that all of GCB's images are set to [email protected], but the one that I need to use (node-8.11.0) is set to this version.

1

There are 1 best solutions below

0
On

I solved the issue by creating my own container image based on the cloud-builder's npm image.

Dockerfile:

FROM gcr.io/cloud-builders/npm:node-8.11.0

ARG NPM_VERSION
RUN npm i -g npm@${NPM_VERSION}

ENTRYPOINT ["npm"]

cloudbuild.yaml:

steps:
- name: 'gcr.io/cloud-builders/docker'
  args:
  - 'build'
  - '--build-arg=NPM_VERSION=latest'
  - '--tag=gcr.io/$PROJECT_ID/npm:latest'
  - '.'

images:
- 'gcr.io/$PROJECT_ID/npm:latest'

I ran gcloud builds submit . --config=cloudbuild.yaml from the same folder containing the Dockerfile and cloudbuild.yaml files. This submitted the build to GCB and posted an image in my project's container registry. I then used this image in my other project's cloudbuild.yaml that needed the upgraded npm version, like so:

steps:
- id: frontend_install
  name: 'gcr.io/$PROJECT_ID/npm:latest'
  args: ['ci']
  waitFor: ['-']

After doing this, everything works as expected.