简体   繁体   中英

Custom (or latest) npm version in Google Cloud Builder

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 npm@5.7.1 . Unfortunately, all of GCB's npm images are currently set to npm@5.6.0

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 npm@5.6.0 , but the one that I need to use ( node-8.11.0 ) is set to this version.

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM