tekton-pipelines/tasks/manage-preview.yaml
2026-02-08 01:20:35 +01:00

68 lines
2.2 KiB
YAML

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: manage-preview
namespace: tekton-pipelines
spec:
params:
- name: app-name
- name: pr-number
- name: action # "apply" or "delete"
- name: image-tag
default: "latest"
workspaces:
- name: source # Workspace containing the app's code (to find preview/values.yaml)
steps:
- name: preview-logic
image: bitnami/kubectl:latest
workingDir: $(workspaces.source.path)
script: |
# Use a specific naming convention for the PR namespace
NS="$(params.app-name)-pr-$(params.pr-number)"
if [ "$(params.action)" = "delete" ]; then
echo "Cleaning up PR environment..."
kubectl delete namespace $NS --ignore-not-found
exit 0
fi
echo "Deploying PR environment to namespace: $NS"
kubectl create namespace $NS --dry-run=client -o yaml | kubectl apply -f -
# Check if the app repo has custom preview values
VALUES_REF_YAML=""
if [ -f "preview/values.yaml" ]; then
kubectl create configmap preview-values --from-file=values.yaml=preview/values.yaml \
-n $NS --dry-run=client -o yaml | kubectl apply -f -
VALUES_REF_YAML="- kind: ConfigMap\n name: preview-values"
fi
# Apply the HelmRelease for this specific PR
cat <<EOF | kubectl apply -n $NS -f -
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
name: $(params.app-name)-pr-$(params.pr-number)
spec:
interval: 5m
releaseName: $(params.app-name)
chart:
spec:
chart: charts/$(params.app-name)
sourceRef:
kind: HelmRepository
name: ghcr-charts
namespace: flux-system
valuesFrom:
$(echo -e $VALUES_REF_YAML)
values:
image:
tag: "$(params.image-tag)"
ingress:
enabled: true
hosts:
- host: $(params.app-name)-pr-$(params.pr-number).nid3.com
paths:
- path: /
pathType: ImplementationSpecific
EOF