带缓存的 Google Cloud Container Builder

带缓存的 Google Cloud Container Builder

假设我有:

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']

有什么方法可以将 gradle 的缓存保留用于将来的构建?目前,它每次都会继续下载所有依赖项。

也许我需要提供一个存储桶并将 GRADLE_USER_HOME 指向它?

答案1

使用 Container Builder 将应用程序的构建与运行时层的构建分开 - 将您的镜像拆分为一个包含依赖项的容器和一个包含您的应用程序的容器,具体如下所述这里。它在运行时还使用 JRE 而不是 JDK,从而节省了一些额外的空间

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"]

答案2

这是我用于我们的暂存构建的内容:

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

我希望这有帮助。

答案3

看看这些社区缓存图像。您只需要在使用它们之前构建并发布save_cacherestore_cache图像到您的图像存储库,因为我不知道公共图像。

这将和tar目录并将它们存储在指定的存储桶中。我.gradle/caches.gradle/wrapper思考您需要指定--path具有绝对文件引用的 s,以便在restore_cache解压时将它们自动恢复到同一目录tar

这是我的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'

重要的是,它GRADLE_USER_HOME位于默认/workspace卷(或自定义卷)中,以便在各个云构建步骤之间保留(卷会自动从一个步骤复制到另一个步骤)。

相关内容