open-meteo-service/.gitlab-ci.yml
2026-02-17 16:09:16 +02:00

107 lines
3.8 KiB
YAML

deploy_test:
stage: deploy_test
image: ubuntu:22.04
before_script:
- |
set -euo pipefail
apt-get update && apt-get install -y curl ca-certificates jq
echo "Installing kubectl ${KUBECTL_VERSION}..."
curl -LO "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl"
chmod +x kubectl && mv kubectl /usr/local/bin/
kubectl version --client
echo "Installing kind ${KIND_VERSION}..."
curl -Lo kind "https://kind.sigs.k8s.io/dl/${KIND_VERSION}/kind-linux-amd64"
chmod +x kind && mv kind /usr/local/bin/
kind version
echo "Installing Helm ${HELM_VERSION}..."
curl -fsSL https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash -s -- --version ${HELM_VERSION}
helm version
script:
- |
set -euo pipefail
echo "Creating kind cluster..."
kind create cluster --name ci-cluster --wait 5m
kubectl get nodes
kubectl cluster-info
echo "Creating imagePullSecret for GitLab Registry..."
kubectl create secret docker-registry gitlab-registry \
--docker-server="${CI_REGISTRY}" \
--docker-username="${CI_REGISTRY_USER}" \
--docker-password="${CI_REGISTRY_PASSWORD}" \
--namespace="${NAMESPACE}" \
--dry-run=client -o yaml | kubectl apply -f -
echo "Deploying Helm chart..."
helm upgrade --install ${HELM_RELEASE_NAME} ${HELM_CHART_PATH} \
--namespace ${NAMESPACE} --create-namespace \
--set image.repository="${CI_REGISTRY_IMAGE}" \
--set image.tag="${CI_COMMIT_SHORT_SHA}" \
--set image.pullPolicy=Always \
--set imagePullSecrets[0].name=gitlab-registry \
--set prometheus.enabled=false \
--set grafana.enabled=false \
--wait --timeout 5m
echo "Cluster state after deploy:"
kubectl get pods -n ${NAMESPACE}
kubectl get svc -n ${NAMESPACE}
echo "Waiting for API pod to become Ready..."
if ! kubectl wait --for=condition=ready pod \
-l app.kubernetes.io/name=open-meteo-service \
-n ${NAMESPACE} --timeout=180s; then
echo "ERROR: Pod not ready. Debug info:"
kubectl get pods -n ${NAMESPACE}
kubectl describe pods -l app.kubernetes.io/name=open-meteo-service -n ${NAMESPACE}
kubectl logs -l app.kubernetes.io/name=open-meteo-service -n ${NAMESPACE} --tail=200
exit 1
fi
echo "Starting port-forward in background..."
kubectl port-forward -n ${NAMESPACE} svc/${SERVICE_NAME} 8000:8000 >/tmp/pf.log 2>&1 &
PF_PID=$!
sleep 5
echo "Testing /healthz..."
for i in $(seq 1 10); do
if curl -fsS --max-time 5 http://localhost:8000/healthz >/dev/null; then
echo "SUCCESS: /healthz endpoint is healthy"
break
fi
echo "Retry $i..."
sleep 3
if [ "$i" -eq 10 ]; then
echo "ERROR: /healthz failed after 10 attempts"
echo "--- port-forward logs ---"
cat /tmp/pf.log || true
exit 1
fi
done
echo "Testing /coordinates..."
COORD_RESPONSE="$(curl -fsS --max-time 15 http://localhost:8000/coordinates)"
echo "$COORD_RESPONSE" | head -50
if ! echo "$COORD_RESPONSE" | grep -q '"data"'; then
echo "ERROR: missing data field in response"
exit 1
fi
echo "SUCCESS: /coordinates endpoint working"
echo "Testing /metrics..."
curl -fsS --max-time 10 http://localhost:8000/metrics | head -30
echo "SUCCESS: /metrics endpoint working"
echo "SUCCESS: All tests passed"
kill $PF_PID || true
after_script:
- |
echo "Final cluster state:"
kubectl get all -n ${NAMESPACE} || true
echo "Deleting kind cluster..."
kind delete cluster --name ci-cluster || true