假設我有:
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,節省了一些額外的空間
雲端建置.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
這就是我用於暫存建置的內容:
雲端建置.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_cache
並將其發佈restore_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
磁碟區(或自訂磁碟區)中,以便在各個雲端建置步驟之間保留它(磁碟區會自動從一個步驟複製到另一個步驟)。