This commit is contained in:
병준 박 2025-04-10 12:02:50 +00:00
parent 3ef2049145
commit 5c9fa9337f
2 changed files with 46 additions and 62 deletions

View File

@ -2,24 +2,35 @@ apiVersion: tekton.dev/v1
kind: Task kind: Task
metadata: metadata:
name: after-pipeline name: after-pipeline
annotations:
tekton.dev/pipelines.minVersion: "0.28.0"
tekton.dev/categories: "Cleanup"
tekton.dev/tags: "directory,workspace"
tekton.dev/displayName: "Cleanup Working Folder in Shared Workspace"
spec: spec:
description: |
This task removes a previously created working folder in the shared workspace.
It accepts the folder name as a parameter. If the folder exists, it will be deleted.
If the folder name is not provided or the folder does not exist, the task will exit gracefully.
params: params:
- name: pvcName - name: workingFolder
type: string type: string
default: "" default: ""
description: | description: |
The name of the PVC to delete. If empty, no action will be taken. The name of the folder in the shared workspace to delete.
If empty, no cleanup action will be performed.
workspaces:
- name: shared
steps: steps:
- name: maybe-delete-pvc - name: cleanup-folder
image: bitnami/kubectl:latest image: ubuntu
script: | script: |
#!/bin/sh #!/bin/bash
set -e
if [ -n "$(params.pvcName)" ]; then workingFolder="/workspace/shared/$(params.workingFolder)"
echo "🔧 Deleting PVC: $(params.pvcName)" if [ -d "${workingFolder}" ]; then
kubectl delete pvc $(params.pvcName) || echo "⚠️ PVC not found: $(params.pvcName)" rm -rf "${workingFolder}"
echo "Deleted folder: ${workingFolder}"
else else
echo "✅ No PVC to delete (pvcName is empty)" echo "Folder not found: ${workingFolder}"
fi fi

View File

@ -2,62 +2,35 @@ apiVersion: tekton.dev/v1
kind: Task kind: Task
metadata: metadata:
name: before-pipeline name: before-pipeline
annotations:
tekton.dev/pipelines.minVersion: "0.28.0"
tekton.dev/categories: "Preparation"
tekton.dev/tags: "directory,workspace"
tekton.dev/displayName: "Create Working Folder in Shared Workspace"
spec: spec:
description: |
This task generates a unique working folder inside the provided shared workspace.
The folder name is a combination of the given prefix, a timestamp, and a random string.
The result can be used as a scoped working directory for subsequent tasks in a pipeline.
params: params:
- name: pvcEnable - name: workingFolderPrefix
type: string type: string
default: "false" default: "pipeline-run"
description: | description: |
Whether to create a PersistentVolumeClaim (PVC). A prefix for the working folder name.
Set to "true" to create a PVC. Set to "false" to skip creation. The final folder name will include this prefix, a timestamp, and a random string.
- name: pvcSize
type: string
default: "1Gi"
description: |
The requested size of the PVC (e.g., "1Gi", "5Gi").
- name: pvcStorageClass
type: string
default: ""
description: |
The name of the StorageClass to use. Leave empty to use the default storage class.
results: results:
- name: pvcName - name: workingFolder
description: | description: |
The name of the created PVC. If pvcEnable is false, this will be an empty string. The unique folder name created in the shared workspace.
workspaces:
- name: shared
steps: steps:
- name: maybe-create-pvc - name: create-folder
image: bitnami/kubectl:latest image: ubuntu
script: | script: |
#!/bin/sh #!/bin/bash
set -e workingFolder="$(params.workingFolderPrefix)-$(date +%s)-$(head /dev/urandom | tr -dc a-z0-9 | head -c 6)"
mkdir -p /workspace/shared/${workingFolder}
if [ "$(params.pvcEnable)" = "true" ]; then echo "Created folder: ${workingFolder}"
RAND=$(head /dev/urandom | tr -dc a-z0-9 | head -c 6) echo -n "${workingFolder}" > $(results.workingFolder.path)
PVC_NAME="before-pvc-${RAND}"
echo "Creating PVC: ${PVC_NAME}"
cat <<EOF > /tmp/pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: ${PVC_NAME}
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: $(params.pvcSize)
volumeMode: Filesystem
EOF
if [ "$(params.pvcStorageClass)" != "" ]; then
echo " storageClassName: $(params.pvcStorageClass)" >> /tmp/pvc.yaml
fi
kubectl apply -f /tmp/pvc.yaml
echo -n "${PVC_NAME}" > $(results.pvcName.path)
echo "PVC created: ${PVC_NAME}"
else
echo "PVC creation skipped (pvcEnable=false)"
echo -n "" > $(results.pvcName.path)
fi