java - 在 Tekton Hub 的 Tekton Maven 任务中缓存 Maven 依赖项

标签 java maven tekton tekton-pipelines openshift-pipelines

我们想要使用 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 依赖项:

enter image description here

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)作为valueGOALS 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 语句污染,并根据依赖项的数量加速管道:

enter image description here

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

关于java - 在 Tekton Hub 的 Tekton Maven 任务中缓存 Maven 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70244444/

相关文章:

java - 如何在 Spring 中使用注释定义 PasswordEncoder?

java - 在 Eclipse/Java/JDT 中,有没有办法只过滤最新版本的 “Open Type” 中的条目?

kubernetes - argocd 应用程序在 CI 管道中创建(GitHub Actions、Tekton...)抛出 "PermissionDenied desc = permission denied: applications, create, default/myapp"

java - 将 JNI 库添加到本地 Maven 存储库

kubernetes - GCP/GKE 上的私有(private) Kubernetes 集群上的 Tekton

tekton - Tekton 如何处理访问同一工作区的并行任务?

java - 部署GAE应用程序时出现"Initialization failed"

java - 如何创建文档子集并在 Elasticsearch 中对该子集执行查询?

java - Intershop studio 4.20.0 - 编译Java - ClassNotFoundException : com. sun.xml.bind.v2.ContextFactory

java - Bazel - 获取当前操作系统和 CPU 类型作为构建规则中使用的变量