2025-04-10 09:14:16 +00:00

61 lines
1.5 KiB
YAML

apiVersion: tekton.dev/v1
kind: Task
metadata:
name: before-pipeline
spec:
params:
- name: pvcEnable
type: string
default: "false"
description: |
Whether to create a PersistentVolumeClaim (PVC).
Set to "true" to create a PVC. Set to "false" to skip creation.
- name: pvcSize
type: string
default: "1Gi"
description: |
The requested size of the PVC (e.g., "1Gi", "5Gi").
results:
- name: pvcName
description: |
The name of the created PVC. If pvcEnable is false, this will be an empty string.
steps:
- name: maybe-create-pvc
image: bitnami/kubectl:latest
script: |
#!/bin/sh
set -e
echo "pvcEnable: $(params.pvcEnable)"
if [ "$(params.pvcEnable)" = "true" ]; then
# Generate a random PVC name
RAND=$(head /dev/urandom | tr -dc a-z0-9 | head -c 6)
PVC_NAME="before-pvc-${RAND}"
echo "Creating PVC: ${PVC_NAME}"
# Apply PVC manifest
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: ${PVC_NAME}
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: $(params.pvcSize)
volumeMode: Filesystem
EOF
# Output the PVC name as a Task result
echo -n "${PVC_NAME}" > $(results.pvcName.path)
echo "PVC creation complete: ${PVC_NAME}"
else
echo "PVC creation skipped (pvcEnable=false)"
echo -n "" > $(results.pvcName.path)
fi