Search code examples
npmgoogle-cloud-platformgoogle-cloud-build

Google Cloud Builds, How to speed up build time of application(npm packages)


My web application runs in Google Cloud Platform, lately I realized that application's build time takes really long, especially when you are testing a feature, let say refresh the page, you can see that it takes really long to start application. What I am looking for is how to speed up this process. I am using docker images to build on google cloud registry. I do not want to re-build all npm packages every time, when there is an update in some npm packages then I want to re-build application.

here is the my cloudbuild.yaml file for polymer

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/myapp-polymer', '.' ]
images:
- 'gcr.io/$PROJECT_ID/myapp-polymer'

then here is the my main cloudbuild.yaml file

steps:
- name: 'gcr.io/cloud-builders/npm'
  args: ['--prefix', 'myapp','install']
- name: 'gcr.io/cloud-builders/npm'
  args: ['--prefix', 'myapp/functions', 'install']
- name: 'gcr.io/$PROJECT_ID/myapp-polymer'
  args: ['cd', 'myapp']
- name: 'gcr.io/$PROJECT_ID/myapp-polymer'
  args: ['build']

I read Google Cloud API especially section "Speeding up your Builds" (https://cloud.google.com/cloud-build/docs/speeding-up-builds)

I think it just cache build images and using it. Are there any way that I can cache specifically npm packages, dependencies etc in Google Cloud so that I do not have to build every time whole application? My main goal is to reduce build time, speed up build process.

Thanks!


Solution

  • Have you tried Kaniko? It can save the cache in gcr.io and and if you have built your Dockerfile with right steps (see https://cloud.google.com/blog/products/gcp/7-best-practices-for-building-containers), it should save you a lot of time. Here is the cloudbuild.yaml example:

    - name: 'gcr.io/kaniko-project/executor:latest'
      args:
      - --destination=gcr.io/$PROJECT_ID/image
      - --cache=true
      - --cache-ttl=XXh
    

    More info: https://cloud.google.com/cloud-build/docs/kaniko-cache