buildpacks icon indicating copy to clipboard operation
buildpacks copied to clipboard

--pack with caching

Open EatonZ opened this issue 4 years ago • 5 comments

I want to use gcloud alpha builds submit --pack as suggested here, but it is very slow compared to building locally. Even using --machine-type=n1-highcpu-32 doesn't help a whole lot.

It looks like the issue is with caching. When running pack on my local machine, everything runs much faster since it uses cached images/layers/etc. Whereas Cloud Build redownloads everything each time.

The solution appears to be to configure caching as outlined here. Sadly, when using --pack, you can't use --config: ERROR: (gcloud.alpha.builds.submit) argument --config: At most one of --config | --pack | --tag may be specified.

When using --pack, what can be done to improve build speeds via caching?

EatonZ avatar Aug 05 '20 02:08 EatonZ

Any tips? This is a blocking issue for me right now.

EatonZ avatar Aug 10 '20 15:08 EatonZ

Unfortunately, it's not currently possible to enable caching in Cloud Build when using the --pack flag. Builds with this flag invoke the pack CLI directly, which only supports local volume caching and volumes are not preserved across builds in Cloud Build.

To get around this limitation, you could create your own cloudbuild.yaml and invoke the build with --config path/to/cloudbuild.yaml in the app directory instead. The config below publishes the built application under gcr.io/<project>/my-app/app:latest and the cache image under gcr.io/<project>/my-app/cache:latest. On subsequent builds, layers are restored from the respective images.

The full command would be: gcloud builds submit --config cloudbuild.yaml .

steps:
- id: prep
  name: gcr.io/cloud-builders/gcloud
  entrypoint: "/bin/bash"
  args:
  - "-c"
  - >
    chown -R "1000:1000" "/workspace"
    && chmod "755" "/workspace"

- id: build
  name: gcr.io/buildpacks/builder:v1
  entrypoint: /cnb/lifecycle/creator
  args:
    - '-app=/workspace'
    - '-cache-image=gcr.io/$PROJECT_ID/my-app/cache'
    - '-previous-image=gcr.io/$PROJECT_ID/my-app/app'
    - 'gcr.io/$PROJECT_ID/my-app/app'

lukasberger avatar Aug 10 '20 17:08 lukasberger

@lukasberger Thank you for the information! Can you also clarify how I would pass in the GOOGLE_BUILDABLE environment variable?

Unfortunately, it's not currently possible to enable caching in Cloud Build when using the --pack flag.

I hope you will consider adding something to handle this. Building without cache is very painful.

EatonZ avatar Aug 11 '20 03:08 EatonZ

Can you also clarify how I would pass in the GOOGLE_BUILDABLE environment variable?

Here's an updated cloudbuild.yaml:

steps:
- name: 'gcr.io/cloud-builders/gcloud'
  entrypoint: '/bin/bash'
  args:
  - '-c'
  - >
    mkdir -p "/platform/env"
    && echo -n "$$GOOGLE_BUILDABLE" > "/platform/env/GOOGLE_BUILDABLE"
    && chown -R "1000:1000" "/workspace"
    && chmod "755" "/workspace"

- name: gcr.io/buildpacks/builder:v1
  entrypoint: /cnb/lifecycle/creator
  args:
    - '-cache-image=gcr.io/$PROJECT_ID/my-app/cache'
    - 'gcr.io/$PROJECT_ID/my-app/app'

options:
  env:
  - 'GOOGLE_BUILDABLE=my-buildable'
  volumes:
  - name: 'platform'
    path: '/platform'

I hope you will consider adding something to handle this. Building without cache is very painful.

I will forward the request to the Cloud Build team, but caching support is likely blocked on buildpacks/pack#491.

Please consider filing a feature request in the Cloud Build Issue Tracker to give feedback to the team directly.

lukasberger avatar Aug 11 '20 17:08 lukasberger

@lukasberger Issue opened here.

EatonZ avatar Aug 12 '20 23:08 EatonZ

A few years later, you can now cache parts of your build using the --cache-image flag. Its not as simple as using the --pack flag, you'll have to create a cloudbuild.yaml. I've gone ahead and documented this here https://cloud.google.com/docs/buildpacks/cache-images

jama22 avatar Aug 24 '23 17:08 jama22