0

Let's say I have:

steps:
    - name: 'gcr.io/cloud-builders/gradle'
      entrypoint: 'bash'
      args: ['-c',
             'chmod +x ./gradlew && ./gradlew build']
    - name: 'gcr.io/cloud-builders/docker'
      args: ['build', '--tag', 'eu.gcr.io/my-app:$_BUILD_ID', '.']
images: ['eu.gcr.io/my-app:$_BUILD_ID']

Are there any ways to leave the gradle's cache for the future builds? Currently, it keeps downloading all dependencies each time.

Maybe I need to suppy a bucket storage and point GRADLE_USER_HOME to it?

3 Answers 3

0

Use Container Builder to separate building the application from building its runtime layer - split your image in a container with the dependencies and another container containing your app as described here. It's also using the JRE instead of the JDK at runtime, saving some extra space

cloudbuild.yaml:

steps:
- name: 'java:8'
  env: ['GRADLE_USER_HOME=cache']
  entrypoint: 'bash'
  args: ['-c',
         './gradlew gate-web:installDist -x test']

- name: 'gcr.io/cloud-builders/docker'
  args: ['build',
         '-t', 'gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA', 
         '-t', 'gcr.io/$PROJECT_ID/$REPO_NAME:latest',
         '-f', 'Dockerfile.slim',
         '.'
  ]
images:
- 'gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA'
- 'gcr.io/$PROJECT_ID/$REPO_NAME:latest'

Dockerfile.slim:

FROM openjdk:8u111-jre-alpine

COPY ./gate-web/build/install/gate /opt/gate

CMD ["/opt/gate/bin/gate"]
0

This is what I'm using for our staging build:

cloudbuild.yaml:

steps:
  # Get the saved global gradle cache from
  # Google Cloud Storage if it exists.
  - name: 'gcr.io/cloud-builders/gsutil'
    entrypoint: 'sh'
    args:
      - '-c'
      - |
        # Global
        if gsutil -q stat gs://${PROJECT_ID}_cloudbuild/gradle_global.tar.gz; then
          cd /
          gsutil cp gs://${PROJECT_ID}_cloudbuild/gradle_global.tar.gz gradle_global.tar.gz
          tar -xpzf gradle_global.tar.gz
        fi
    volumes:
      - name: 'gradle_global'
        path: /root/.gradle

  # Build the java package.
  - name: 'gcr.io/cloud-builders/gradle'
    entrypoint: 'sh'
    args:
      - '-c'
      - |
        # Build the gateway .war package.
        ./gradlew -Pprod -DskipTests clean bootRepackage

        # Move the files needed for the docker image
        # to the slim directory.
        mv ./build/libs/*.war ./app.war

        # Delete all files and folders in the current directory
        # except for the `app.war` and `Dockerfile`.
        find . -type d -exec rm -rf {}
        find . -type f -not -name 'app.war' -not -name 'Dockerfile' -delete
    volumes:
     - name: 'gradle_global'
       path: /root/.gradle

  # Save the global gradle cache to Google Cloud Storage
  - name: 'gcr.io/cloud-builders/gsutil'
    entrypoint: 'sh'
    args:
      - '-c'
      - |
        # Global
        echo 'Creating an archive of /root/.gradle directory'
        tar -cpzf gradle_global.tar.gz /root/.gradle/
        echo 'Saving archive to Cloud Storage'
        gsutil cp gradle_global.tar.gz gs://${PROJECT_ID}_cloudbuild/gradle_global.tar.gz
        # Cleaning up
        echo 'Deleting gradle*.tar.gz'
        rm -f gradle*.tar.gz
  volumes:
    - name: 'gradle_global'
      path: /root/.gradle

  - name: 'gcr.io/cloud-builders/docker'
    args: ['pull', 'asia.gcr.io/$PROJECT_ID/${IMAGE_NAME}:latest']

  # Build the gateway container image.
  - name: 'gcr.io/cloud-builders/docker'
    args:
      - 'build'
      - '-t'
      - 'asia.gcr.io/${PROJECT_ID}/${IMAGE_NAME}:${COMMIT_SHA}'
      - '-t'
      - 'asia.gcr.io/${PROJECT_ID}/${IMAGE_NAME}:latest'
      - '--cache-from'
      - 'asia.gcr.io/${PROJECT_ID}/${IMAGE_NAME}:latest'
      - '.'

images:
  - asia.gcr.io/${PROJECT_ID}/gateway:${COMMIT_SHA}
  - asia.gcr.io/${PROJECT_ID}/gateway:latest

I hope this helps.

0

See these community cache images. You'll just need to build and publish the save_cache and restore_cache images to your image repository before using them because I'm not aware of public images.

This will tar the .gradle/caches and .gradle/wrapper directories and store them in the specified bucket. I think you need to specify the --paths with absolute file references in order for them to be restored automatically to the same directory by restore_cache when it unzips the tar.

Here's my cloudbuild.yaml.

steps:
  - name: 'gcr.io/$PROJECT_ID/restore_cache'
    args:
      - '--bucket=gs://${PROJECT_ID}_cloudbuild/${_CACHE_BUCKET}'
      - '--key=${_CACHE_KEY}'
  - name: 'java:8'
    entrypoint: "bash"
    args:
      - '-c'
      - './gradlew build'
    env:
      - 'GRADLE_OPTS="-Dorg.gradle.daemon=false -Dkotlin.incremental=false"'
      - 'GRADLE_USER_HOME=${_GRADLE_USER_HOME}'
  - name: 'gcr.io/$PROJECT_ID/save_cache'
    args:
      - '--bucket=gs://${PROJECT_ID}_cloudbuild/${_CACHE_BUCKET}'
      - '--key=${_CACHE_KEY}'
      - '--path=${_GRADLE_USER_HOME}/caches'
      - '--path=${_GRADLE_USER_HOME}/wrapper'
substitutions:
  _CACHE_BUCKET: 'gradle_cache'
  _GRADLE_USER_HOME: '/workspace/.gradle'
  _CACHE_KEY: 'gradle-cache'

It's important that GRADLE_USER_HOME be in the default /workspace volume (or a custom volume) so that is is retained between the various cloud build steps (volumes are automatically copied from step to step).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .