Here are three scripts for Tekton. It did not find git directory and compile with mvn due to the missing pom.xml. I tried to fix the problems by revising task, pipeline and pipelinerun but it is not fixed. Where am I supposed to revise the script to fix the issue?
I tried to fix the workspace path and used static path to resolve the issue; however, it did not work.
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)/tekton-tutorial-greeter
# 프로젝트가 있는지 확인
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
Pipeline:
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)"
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:
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: "master"
- name: image-name
value: "greeter-app"
Log:
task build-and-package has failed: "step-build" exited with code 1
[build-and-package : build] === Checking workspace contents ===
[build-and-package : build] total 8.0K
[build-and-package : build] drwxrwxrwx 2 root root 4.0K Mar 29 07:46 .
[build-and-package : build] drwxrwxrwx 3 root root 4.0K Mar 29 07:46 ..
[build-and-package : build] /tekton/scripts/script-0-7cbds: 6: cd: can't cd to /workspace/source/tekton-tutorial-greeter
[build-and-package : build] ERROR: pom.xml not found! Exiting...
failed to get logs for task build-and-package : container step-build has failed : [{"key":"StartedAt","value":"2025-03-29T07:46:57.141Z","type":3}]
It is not problem with script but with workspace. You need to have "persistent" workspace between fetch-source
and build-and-package
package tasks. The easiest is to change your workspace definition in the pipeline run to this:
workspaces:
- name: workspace
volumeClaimTemplate:
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
another option is to create pvc
first and then use it in the pipeline run.