0

我们想使用 Maven 构建一个基于 Spring Boot 的项目。我们在 Tekton Hub 上找到了 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-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-pvcPersistentVolumeClaim 定义为:

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似乎不支持使用缓存。尽管如此,我们如何缓存?

4

1 回答 1

1

有一种使用 Tekto Hub 的 Maven 任务完成缓存的简单方法。您需要在已定义的PersistentVolumeClaim中创建一个新目录,而不是在maven-settings工作区中指定一个空目录。也以与您已经为. 你现在不知何故看起来像这样:emptyDir: {}subPathsource-pvcpersistentVolumeClaimsource-workspacePipelineRun

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-settingsTekton Hub 的 Maven 任务中的工作区使用(现在还没有实现额外的cache工作区)。我们只需要告诉 Maven 使用该路径workspaces.maven-settings.path作为缓存存储库。

因此,我们将-Dmaven.repo.local=$(workspaces.maven-settings.path)a添加到TaskvalueGOALS参数中,如下所示: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 下载语句污染,并根据依赖项的数量加速管道:

在此处输入图像描述

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

于 2021-12-06T11:02:00.510 回答