我们希望使用Maven构建一个基于Spring的项目。我们找到了Tekton集线器的Maven任务,并且已经有了一个正在运行的管道。在一个缩短的版本中,我们的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-settingsPipelineRun被定义为:
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: 3c4131f8566ef157244881bacc474543ef96755dsource-pvc PersistentVolumeClaim定义为:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: source-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi我们的项目正在构建良好,但是当我们启动另一个PipelineRun时,任务会一次又一次地下载该项目的所有Maven依赖项:

Tekton的Maven任务https://hub.tekton.dev/tekton/task/maven似乎不支持使用缓存。然而,我们如何缓存呢?
发布于 2021-12-06 11:02:00
有一种使用Tekto的Maven任务来完成缓存的简单方法。不需要在maven-settings工作区中使用emptyDir: {}指定一个空目录,而是需要在已经定义的source-pvc PersistentVolumeClaim中创建一个新的subPath。另外,链接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已经可以通过Tekton的Maven任务(workspace right now)中的maven-settings工作区获得。我们只需要告诉Maven使用path workspaces.maven-settings.path作为缓存存储库。
因此,我们将-Dmaven.repo.local=$(workspaces.maven-settings.path)作为value添加到maven任务的GOALS参数中,如下所示:
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下载语句污染的日志,并根据依赖项的数量加快管道的速度:

我们的简单示例构建的速度是以前的两倍多。
https://stackoverflow.com/questions/70244444
复制相似问题