简体   繁体   中英

Do I need to add node_modules to .gcloudignore when running “gcloud builds submit ./project-folder”?

This is my project structure:

project/
  node_modules/
  src/
  .gcloudignore
  cloudbuild.yaml
  Dockerfile
  package.json

Here is how I'm building it:

gcloud builds submit ./project --config=./project/cloudbuild.yaml --project=$PROJECT_ID   // AND SOME SUBSTITUTIONS

This is my cloudbuild.yaml file:

steps:
  # BUILD IMAGE
  - name: "gcr.io/cloud-builders/docker"
    args:
      - "build"
      - "--tag"
      - "gcr.io/$PROJECT_ID/$_SERVICE_NAME:$_TAG_NAME"
      - "."
    timeout: 180s

    # PUSH IMAGE TO REGISTRY
  - name: "gcr.io/cloud-builders/docker"
    args:
      - "push"
      - "gcr.io/$PROJECT_ID/$_SERVICE_NAME:$_TAG_NAME"
    timeout: 180s

    # DEPLOY CONTAINER WITH GCLOUD
  - name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
    entrypoint: gcloud
    args:
      - "run"
      - "deploy"
      - "$_SERVICE_NAME"
      - "--image=gcr.io/$PROJECT_ID/$_SERVICE_NAME:$_TAG_NAME"
      - "--platform=managed"
      - "--region=$_REGION"
      - "--port=8080"
      - "--allow-unauthenticated"
    timeout: 180s

# DOCKER IMAGES TO BE PUSHED TO CONTAINER REGISTRY
images: 
  - "gcr.io/$PROJECT_ID/$_SERVICE_NAME:$_TAG_NAME"

And here is my Dockerfile :

FROM node:12-slim

WORKDIR /

COPY ./package.json ./package.json
COPY ./package-lock.json ./package-lock.json

COPY ./src ./src

RUN npm ci

From my configuration file, since nothing is being told to copy the node_modules folder, it seems unnecessary to add node_modules to .gcloudignore . But is it?

I'm asking this because I saw this answer that said:

When you run gcloud builds submit... you provide some source code and either a Dockerfile or a configuration file. The former is a simple case of the second, a configuration file containing a single step that runs docker build....

Configuration files (YAML) list a series of container images with parameters that are run in series. Initially Cloud Build copies a designated source (can be the current directory) to a Compute Engine VM (created by the service) as a directory (that's automatically mounted into each container) as /workspace.

If it copies the source, will it copy node_modules as well? Should I add it to .gcloudignore or is it not necessary?

Yes you can skip the node module because you don't use them in your build (and it's huge and long to upload). In your command npm ci , I'm sure you download the dependencies, so, add the node_modules to your .gcloudignore (and .gitignore also)

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