Search code examples
yamlgoogle-cloud-buildcloudbuild.yaml

is there any way to run npm install and npm run build in a cloudbuild.yaml?


I have this react app, I need to deploy into a google cloud storage thru google cloud build, I'm trying to do it adding a cloudbuild.yaml and add some steps to run the npm

This is what I have:

steps:
  - name: bash
    script: |
      npm install --force
  - name: bash
    script: |
      npm run build
  - name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
    entrypoint: gcloud
    args:
      ["storage", "cp", "/dist/*", "gs://bucket"]
timeout: 1200s
options:
  logging: CLOUD_LOGGING_ONLY

but cloud build show me an error:

Step #0: script: line 2: npm: not found


Solution

  • As mentioned in this documentation. Before you can build your application, you must ensure that all of your project's dependencies are installed from npm or yarn.

    steps:
    - name: node
      entrypoint: npm
      args: ['install']
    
    

    If you've defined a test script in your package.json, you can configure Cloud Build to run the script by adding test to the args field:

    steps:
    - name: node
      entrypoint: npm
      args: ['install']
    - name: node
      entrypoint: npm
      args: ['test']
    

    You can also check this link and github thread