2025-04-10 10:06:43 +00:00

63 lines
1.8 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").
- name: pvcStorageClass
type: string
default: ""
description: |
The name of the StorageClass to use. Leave empty to use the default storage class.
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
if [ "$(params.pvcEnable)" = "true" ]; then
RAND=$(head /dev/urandom | tr -dc a-z0-9 | head -c 6)
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