在 Tecton Hub 的 Tecton Maven 任务中缓存 Maven 依赖项

Cache Maven dependencies in Tekton Maven Task from Tekton Hub

我们想使用 Maven 构建一个 Spring 基于引导的项目。我们找到了 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 任务完成缓存的简单方法。您需要在已经定义的 source-pvc PersistentVolumeClaim 中创建一个新的 subPath,而不是使用 emptyDir: {}maven-settings 工作区中指定一个空目录。 link persistentVolumeClaim 的方式与您已经 link 为 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 Hub 的 Maven 任务 (which doesn't implement an extra cache workspace right now) 中的 maven-settings 工作区获得。我们只需要告诉Maven使用路径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 下载语句污染,并根据依赖项的数量加快管道速度:

我们的简单示例的构建速度是原来的两倍多。