Tekton脚本构建失败:Git目录及pom.xml缺失问题修复咨询
修复Tekton Maven构建中找不到Git目录与pom.xml的问题
问题根源分析
从日志可定位两个核心问题:
- Git克隆任务未拉取到代码:
ls -lah显示工作空间为空,原因是PipelineRun中指定的revision为master,但目标仓库的有效分支是main,导致克隆失败。 - 目录切换路径错误:即使克隆成功,Task中尝试进入
$(workspaces.source.path)/tekton-tutorial-greeter也不符合逻辑——默认git-clone任务会将代码直接克隆到工作空间根目录,不会自动创建以仓库名为名的子目录。
修复步骤
1. 修正PipelineRun的分支参数
将revision的值改为仓库实际存在的main分支:
apiVersion: tekton.dev/v1beta1 kind: PipelineRun metadata: name: greeter-maven-build-run spec: pipelineRef: name: greeter-maven-build-pipeline workspaces: - name: shared-workspace emptyDir: {} params: - name: repo-url value: "https://github.com/gitops-cookbook/tekton-tutorial-greeter.git" - name: revision value: "main" # 从master改为main - name: image-name value: "greeter-app"
2. 修正maven-build Task的目录逻辑
去掉多余的子目录路径,直接切换到工作空间根目录(代码克隆的默认位置):
apiVersion: tekton.dev/v1beta1 kind: Task metadata: name: maven-build spec: workspaces: - name: source params: - name: package type: string steps: - name: build image: maven:3.8.6-openjdk-11 script: | #!/bin/sh echo "=== Checking workspace contents ===" ls -lah $(workspaces.source.path) # 直接切换到代码根目录 cd $(workspaces.source.path) # 检查项目是否存在 if [ ! -f "pom.xml" ]; then echo "ERROR: pom.xml not found! Exiting..." exit 1 fi mvn clean package cp target/*.jar $(workspaces.source.path)/$(params.package).jar
可选:指定Git克隆到子目录(保留原路径逻辑)
如果希望代码克隆到指定子目录,可在Pipeline的fetch-source任务中添加subdirectory参数,这样原Task中的路径无需修改:
apiVersion: tekton.dev/v1beta1 kind: Pipeline metadata: name: greeter-maven-build-pipeline spec: params: - name: repo-url type: string default: "https://github.com/gitops-cookbook/tekton-tutorial-greeter.git" - name: revision type: string default: "main" - name: image-name type: string default: "greeter-app" workspaces: - name: shared-workspace tasks: - name: fetch-source taskRef: name: git-clone params: - name: url value: "$(params.repo-url)" - name: revision value: "$(params.revision)" - name: subdirectory # 添加参数指定克隆到子目录 value: "tekton-tutorial-greeter" workspaces: - name: output workspace: shared-workspace - name: build-and-package taskRef: name: maven-build runAfter: - fetch-source params: - name: package value: "$(params.image-name)" workspaces: - name: source workspace: shared-workspace
验证修复
重新部署修改后的资源并执行PipelineRun:
kubectl apply -f maven-build-task.yaml kubectl apply -f greeter-pipeline.yaml kubectl apply -f greeter-pipelinerun.yaml
查看日志确认代码克隆成功,Maven构建正常完成。
内容的提问来源于stack exchange,提问作者Joseph
相关产品推荐
相关产品推荐

