Skip to content

Instantly share code, notes, and snippets.

@jed204
Created October 30, 2019 05:36
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jed204/e2ee82eab958d999b98c082bf4290624 to your computer and use it in GitHub Desktop.
Save jed204/e2ee82eab958d999b98c082bf4290624 to your computer and use it in GitHub Desktop.
Google Cloud Build Maven Build with Cache
steps:
# Grab cached M2 repo
- name: gcr.io/cloud-builders/gsutil
id: Get M2 Cache
args: ['cp', 'gs://BUCKET_NAME/m2.tar.gz', 'm2.tar.gz']
# See https://github.com/GoogleCloudPlatform/cloud-builders-community to get the tar command
- name: gcr.io/PROJECT_ID/tar
id: Expand M2 Cache
args: ['xpzf', 'm2.tar.gz']
# Optional, we had some extra settings to include
- name: 'gcr.io/cloud-builders/gsutil'
id: Get setting.xml
args: ['cp', 'gs://BUCKET_NAME/settings.xml', 'custom-settings.xml']
# The maven.repo.local setting was key
- name: 'gcr.io/cloud-builders/mvn'
id: Test and Build
args: ['-Dmaven.repo.local=/workspace/.m2/repository', '--settings', 'custom-settings.xml', 'deploy']
# Update M2 repo cache
- name: gcr.io/PROJECT_ID/tar
id: Compress M2 Cache
args: ['cpzf', 'm2.tar.gz', '.m2']
- name: gcr.io/cloud-builders/gsutil
id: Save M2 Cache
args: ['cp', 'm2.tar.gz', 'gs://BUCKET_NAME/m2.tar.gz']
@jed204
Copy link
Author

jed204 commented Oct 30, 2019

You have to seed your bucket with a m2.tar.gz file. Adding the .m2 cache via this method sped up our build about 50%. We have about 180MB of jar files that were being downloaded from central every build.

This concept is addressed in the Google Cloud Build docs but there isn't a great example of it: https://cloud.google.com/cloud-build/docs/speeding-up-builds

@hazzyeer
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment