我们想要使用 Maven 构建一个基于 Spring Boot 的项目。我们发现the Maven Task on the Tekton Hub并且已经有一个正在运行的管道。在缩短版本中,我们的 pipeline.yml
如下所示:
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: buildpacks-test-pipeline
spec:
params:
- name: SOURCE_URL
type: string
description: A git repo url where the source code resides.
- name: SOURCE_REVISION
description: The branch, tag or SHA to checkout.
default: ""
workspaces:
- name: maven-settings
- name: source-workspace
tasks:
- name: fetch-repository
taskRef:
name: git-clone
workspaces:
- name: output
workspace: source-workspace
params:
- name: url
value: "$(params.SOURCE_URL)"
- name: revision
value: "$(params.SOURCE_REVISION)"
- name: subdirectory
value: ""
- name: deleteExisting
value: "true"
- name: maven
taskRef:
name: maven
runAfter:
- fetch-repository
params:
- name: GOALS
value:
- package
workspaces:
- name: source
workspace: source-workspace
- name: maven-settings
workspace: maven-settings
PipelineRun 定义为:
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
generateName: buildpacks-test-pipeline-run-
spec:
pipelineRef:
name: buildpacks-test-pipeline
workspaces:
- name: maven-settings
emptyDir: {}
- name: source-workspace
subPath: source
persistentVolumeClaim:
claimName: source-pvc
params:
- name: SOURCE_URL
value: https://gitlab.com/jonashackt/microservice-api-spring-boot
- name: SOURCE_REVISION
value: 3c4131f8566ef157244881bacc474543ef96755d
source-pvc
PersistentVolumeClaim 定义为:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: source-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
我们的项目构建得很好,但是当我们启动另一个 PipelineRun 时,任务会一遍又一遍地下载项目的所有 Maven 依赖项:
Tekton Hub 的 Maven 任务 https://hub.tekton.dev/tekton/task/maven似乎不支持使用缓存。尽管如此,我们如何才能缓存呢?
最佳答案
有一种简单的方法可以使用 Tekto Hub 的 Maven 任务来完成缓存。而不是在 maven-settings
中指定空目录工作区 emptyDir: {}
您需要创建一个新的 subPath
在你已经定义的source-pvc
里面持久卷声明。另链接 persistentVolumeClaim
与您已经链接到 source-workspace
的方式相同。您的PipelineRun
现在看起来像这样:
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
generateName: buildpacks-test-pipeline-run-
spec:
pipelineRef:
name: buildpacks-test-pipeline
workspaces:
- name: maven-settings
subPath: maven-repo-cache
persistentVolumeClaim:
claimName: source-pvc
- name: source-workspace
subPath: source
persistentVolumeClaim:
claimName: source-pvc
params:
- name: SOURCE_URL
value: https://gitlab.com/jonashackt/microservice-api-spring-boot
- name: SOURCE_REVISION
value: 3c4131f8566ef157244881bacc474543ef96755d
现在是新的subPath
已可通过 maven-settings
获取Tekton Hub 的 Maven 任务 ( which doesn't implement an extra cache
workspace right now ) 内的工作区。我们只需要告诉Maven使用路径workspaces.maven-settings.path
作为缓存存储库。
因此我们添加-Dmaven.repo.local=$(workspaces.maven-settings.path)
作为value
到GOALS
maven
的参数像这样的任务:
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: buildpacks-test-pipeline
spec:
params:
- name: SOURCE_URL
type: string
description: A git repo url where the source code resides.
- name: SOURCE_REVISION
description: The branch, tag or SHA to checkout.
default: ""
workspaces:
- name: maven-settings
- name: source-workspace
tasks:
- name: fetch-repository # This task fetches a repository from github, using the `git-clone` task you installed
taskRef:
name: git-clone
workspaces:
- name: output
workspace: source-workspace
params:
- name: url
value: "$(params.SOURCE_URL)"
- name: revision
value: "$(params.SOURCE_REVISION)"
- name: subdirectory
value: ""
- name: deleteExisting
value: "true"
- name: maven
taskRef:
name: maven
runAfter:
- fetch-repository
params:
- name: GOALS
value:
- -Dmaven.repo.local=$(workspaces.maven-settings.path)
- verify
workspaces:
- name: source
workspace: source-workspace
- name: maven-settings
workspace: maven-settings
现在,在第一次管道执行之后,每次下一次运行都应该重新使用 maven-settings
内的 Maven 存储库。工作区。这还可以防止日志被 Maven Download 语句污染,并根据依赖项的数量加速管道:
我们的简单示例的构建速度是原来的两倍多。
关于java - 在 Tekton Hub 的 Tekton Maven 任务中缓存 Maven 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70244444/