89 lines
2.9 KiB
YAML
89 lines
2.9 KiB
YAML
stages: [build, deploy]
|
|
|
|
variables:
|
|
IMAGE_REPO: "$CI_REGISTRY_IMAGE"
|
|
TAG: "${CI_COMMIT_REF_SLUG}-${CI_COMMIT_SHORT_SHA}"
|
|
RELEASE: "open-meteo-service-gitlab"
|
|
NAMESPACE: "sandbox-gitlab"
|
|
CHART_PATH: "./open-meteo-service"
|
|
VALUES_FILE: "./open-meteo-service/values.yaml"
|
|
SERVICE_NAME: "open-meteo-service-open-meteo-service" # <-- אם אצלך שם אחר, תשנה פה
|
|
|
|
build:
|
|
stage: build
|
|
tags: [homelab]
|
|
script:
|
|
- docker login "$CI_REGISTRY" -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD"
|
|
- docker build -t "$IMAGE_REPO:$TAG" .
|
|
- docker push "$IMAGE_REPO:$TAG"
|
|
- docker tag "$IMAGE_REPO:$TAG" "$IMAGE_REPO:latest"
|
|
- docker push "$IMAGE_REPO:latest"
|
|
- echo "Built and pushed $IMAGE_REPO:$TAG and $IMAGE_REPO:latest"
|
|
|
|
deploy:
|
|
stage: deploy
|
|
tags: [homelab]
|
|
rules:
|
|
- if: '$CI_COMMIT_MESSAGE =~ /^ci: bump image tag/'
|
|
when: never
|
|
- when: on_success
|
|
script: |-
|
|
set -euo pipefail
|
|
|
|
# Install yq if missing
|
|
if ! command -v yq >/dev/null 2>&1; then
|
|
YQ_VERSION="v4.35.1"
|
|
wget -qO /usr/local/bin/yq "https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_amd64"
|
|
chmod +x /usr/local/bin/yq
|
|
fi
|
|
|
|
# Update values.yaml with the new tag
|
|
yq e ".image.tag = \"${TAG}\"" -i "${VALUES_FILE}"
|
|
echo "Updated ${VALUES_FILE} with image.tag=${TAG}"
|
|
|
|
# Configure git identity
|
|
git config user.email "gitlab-ci@dvirlabs.com"
|
|
git config user.name "GitLab CI"
|
|
|
|
# Commit & push values bump (skip-ci to avoid loop)
|
|
git add "${VALUES_FILE}"
|
|
git commit -m "ci: bump image tag to ${TAG} [skip ci]" || echo "No changes to commit"
|
|
|
|
git remote set-url origin "https://gitlab-ci-token:${CI_JOB_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git"
|
|
git push origin "HEAD:${CI_COMMIT_REF_NAME}" || echo "Nothing to push"
|
|
|
|
echo "Deploying with Helm..."
|
|
helm upgrade --install "${RELEASE}" "${CHART_PATH}" \
|
|
-n "${NAMESPACE}" \
|
|
--create-namespace \
|
|
--wait --timeout 5m
|
|
|
|
echo "Waiting for deployment to be ready..."
|
|
kubectl -n "${NAMESPACE}" rollout status deploy -l app.kubernetes.io/name=open-meteo-service --timeout=180s
|
|
|
|
echo "Port-forward for tests..."
|
|
kubectl -n "${NAMESPACE}" port-forward "svc/${SERVICE_NAME}" 8000:8000 >/tmp/pf.log 2>&1 &
|
|
PF_PID=$!
|
|
sleep 5
|
|
|
|
echo "Testing /healthz..."
|
|
curl -fsS http://localhost:8000/healthz >/dev/null
|
|
echo "OK /healthz"
|
|
|
|
echo "Testing /coordinates..."
|
|
COORDS_RESPONSE="$(curl -fsS http://localhost:8000/coordinates)"
|
|
echo "$COORDS_RESPONSE" | head -20
|
|
if ! echo "$COORDS_RESPONSE" | grep -q '"data"'; then
|
|
echo "ERROR: /coordinates response missing 'data' field"
|
|
kill $PF_PID || true
|
|
exit 1
|
|
fi
|
|
echo "OK /coordinates"
|
|
|
|
echo "Testing /metrics..."
|
|
curl -fsS http://localhost:8000/metrics | head -20
|
|
echo "OK /metrics"
|
|
|
|
kill $PF_PID || true
|
|
echo "All tests passed!"
|