Disable the default clone and use a custom clone step with up to 5 retry attempts in both frontend and api pipelines. This prevents builds from failing due to transient "Could not resolve host" errors.
214 lines
7.4 KiB
YAML
214 lines
7.4 KiB
YAML
kind: pipeline
|
|
type: kubernetes
|
|
name: frontend
|
|
|
|
clone:
|
|
disable: true
|
|
|
|
concurrency:
|
|
limit: 1
|
|
|
|
timeout: 20
|
|
|
|
trigger:
|
|
branch:
|
|
- master
|
|
event:
|
|
- push
|
|
|
|
steps:
|
|
- name: clone
|
|
image: alpine/git
|
|
commands:
|
|
- |
|
|
for i in 1 2 3 4 5; do
|
|
git clone --depth=50 "$DRONE_REMOTE_URL" . && exit 0
|
|
echo "Clone attempt $i failed, retrying in 5s..."
|
|
sleep 5
|
|
done
|
|
echo "Clone failed after 5 attempts"
|
|
exit 1
|
|
- git checkout "$DRONE_COMMIT"
|
|
|
|
- name: Run frontend tests
|
|
image: node:24-alpine
|
|
commands:
|
|
- cd frontend
|
|
- npm ci
|
|
- npx vitest run
|
|
|
|
- name: Build frontend image
|
|
image: plugins/kaniko
|
|
settings:
|
|
username: viktorbarzin
|
|
password:
|
|
from_secret: dockerhub-token
|
|
repo: viktorbarzin/immoweb
|
|
dockerfile: frontend/Dockerfile
|
|
context: frontend
|
|
enable_cache: true
|
|
cache_repo: viktorbarzin/immoweb-cache
|
|
tags:
|
|
- latest
|
|
- ${DRONE_BUILD_NUMBER}
|
|
|
|
- name: Update deployment
|
|
image: alpine
|
|
commands:
|
|
- apk add curl
|
|
- 'curl -s -X PATCH "https://kubernetes:6443/apis/apps/v1/namespaces/realestate-crawler/deployments/realestate-crawler-ui" -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" -H "Content-Type: application/json-patch+json" -k -d ''[{"op":"replace","path":"/spec/template/spec/containers/0/image","value":"viktorbarzin/immoweb:''"$DRONE_BUILD_NUMBER"''"}]'' | head'
|
|
|
|
- name: verify-deploy
|
|
image: alpine
|
|
commands:
|
|
- apk add --no-cache curl jq
|
|
- |
|
|
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
|
|
EXPECTED_IMAGE="viktorbarzin/immoweb:${DRONE_BUILD_NUMBER}"
|
|
PODS_API="https://kubernetes:6443/api/v1/namespaces/realestate-crawler/pods?labelSelector=app%3Drealestate-crawler-ui"
|
|
|
|
for i in $(seq 1 60); do
|
|
RESULT=$(curl -sfk "$PODS_API" \
|
|
-H "Authorization: Bearer $TOKEN" \
|
|
-H "Accept: application/json" | \
|
|
jq --arg img "$EXPECTED_IMAGE" '[.items[] | select(
|
|
(now - (.status.startTime | fromdateiso8601)) < 60 and
|
|
(.status.containerStatuses[]? | .ready == true) and
|
|
(.spec.containers[]? | .image == $img)
|
|
) | {name: .metadata.name, age: (now - (.status.startTime | fromdateiso8601) | floor), image: .spec.containers[0].image, started: .status.startTime}]')
|
|
|
|
COUNT=$(echo "$RESULT" | jq 'length')
|
|
echo "Attempt $i/60: $COUNT pod(s) younger than 60s, ready, running $EXPECTED_IMAGE"
|
|
|
|
if [ "$COUNT" -gt 0 ]; then
|
|
echo "$RESULT" | jq -r '.[] | " \(.name) age=\(.age)s image=\(.image) started=\(.started)"'
|
|
echo "New pod is live!"
|
|
exit 0
|
|
fi
|
|
|
|
sleep 5
|
|
done
|
|
|
|
echo "ERROR: No new ready pod with image $EXPECTED_IMAGE appeared within 5 minutes"
|
|
exit 1
|
|
|
|
---
|
|
kind: pipeline
|
|
type: kubernetes
|
|
name: api
|
|
|
|
clone:
|
|
disable: true
|
|
|
|
concurrency:
|
|
limit: 1
|
|
|
|
timeout: 20
|
|
|
|
trigger:
|
|
branch:
|
|
- master
|
|
event:
|
|
- push
|
|
|
|
steps:
|
|
- name: clone
|
|
image: alpine/git
|
|
commands:
|
|
- |
|
|
for i in 1 2 3 4 5; do
|
|
git clone --depth=50 "$DRONE_REMOTE_URL" . && exit 0
|
|
echo "Clone attempt $i failed, retrying in 5s..."
|
|
sleep 5
|
|
done
|
|
echo "Clone failed after 5 attempts"
|
|
exit 1
|
|
- git checkout "$DRONE_COMMIT"
|
|
|
|
- name: Cache test image
|
|
image: plugins/docker
|
|
settings:
|
|
username: viktorbarzin
|
|
password:
|
|
from_secret: dockerhub-token
|
|
repo: viktorbarzin/realestatecrawler
|
|
dockerfile: Dockerfile
|
|
context: .
|
|
target: test
|
|
cache_from:
|
|
- viktorbarzin/realestatecrawler:test
|
|
- viktorbarzin/realestatecrawler:builder
|
|
tags:
|
|
- test
|
|
|
|
- name: Run backend tests
|
|
image: viktorbarzin/realestatecrawler:test
|
|
commands:
|
|
- pytest tests/ -x -q
|
|
|
|
- name: Build API image
|
|
image: plugins/docker
|
|
settings:
|
|
username: viktorbarzin
|
|
password:
|
|
from_secret: dockerhub-token
|
|
repo: viktorbarzin/realestatecrawler
|
|
dockerfile: Dockerfile
|
|
context: .
|
|
cache_from:
|
|
- viktorbarzin/realestatecrawler:builder
|
|
- viktorbarzin/realestatecrawler:latest
|
|
tags:
|
|
- latest
|
|
- ${DRONE_BUILD_NUMBER}
|
|
|
|
- name: Update deployment
|
|
image: alpine
|
|
commands:
|
|
- apk add curl
|
|
- 'curl -s -X PATCH "https://kubernetes:6443/apis/apps/v1/namespaces/realestate-crawler/deployments/realestate-crawler-api" -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" -H "Content-Type: application/json-patch+json" -k -d ''[{"op":"replace","path":"/spec/template/spec/containers/0/image","value":"viktorbarzin/realestatecrawler:''"$DRONE_BUILD_NUMBER"''"}]'' | head'
|
|
- 'curl -s -X PATCH "https://kubernetes:6443/apis/apps/v1/namespaces/realestate-crawler/deployments/realestate-crawler-celery" -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" -H "Content-Type: application/json-patch+json" -k -d ''[{"op":"replace","path":"/spec/template/spec/containers/0/image","value":"viktorbarzin/realestatecrawler:''"$DRONE_BUILD_NUMBER"''"}]'' | head'
|
|
- 'curl -s -X PATCH "https://kubernetes:6443/apis/apps/v1/namespaces/realestate-crawler/deployments/realestate-crawler-celery-beat" -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" -H "Content-Type: application/json-patch+json" -k -d ''[{"op":"replace","path":"/spec/template/spec/containers/0/image","value":"viktorbarzin/realestatecrawler:''"$DRONE_BUILD_NUMBER"''"}]'' | head'
|
|
|
|
- name: verify-deploy
|
|
image: alpine
|
|
commands:
|
|
- apk add --no-cache curl jq
|
|
- |
|
|
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
|
|
EXPECTED_IMAGE="viktorbarzin/realestatecrawler:${DRONE_BUILD_NUMBER}"
|
|
BASE_API="https://kubernetes:6443/api/v1/namespaces/realestate-crawler/pods"
|
|
|
|
for DEPLOY in realestate-crawler-api realestate-crawler-celery realestate-crawler-celery-beat; do
|
|
echo "Verifying $DEPLOY..."
|
|
PODS_API="$BASE_API?labelSelector=app%3D$DEPLOY"
|
|
|
|
FOUND=0
|
|
for i in $(seq 1 60); do
|
|
RESULT=$(curl -sfk "$PODS_API" \
|
|
-H "Authorization: Bearer $TOKEN" \
|
|
-H "Accept: application/json" | \
|
|
jq --arg img "$EXPECTED_IMAGE" '[.items[] | select(
|
|
(now - (.status.startTime | fromdateiso8601)) < 60 and
|
|
(.status.containerStatuses[]? | .ready == true) and
|
|
(.spec.containers[]? | .image == $img)
|
|
) | {name: .metadata.name, age: (now - (.status.startTime | fromdateiso8601) | floor), image: .spec.containers[0].image, started: .status.startTime}]')
|
|
|
|
COUNT=$(echo "$RESULT" | jq 'length' 2>/dev/null || echo 0)
|
|
echo " Attempt $i/60: $COUNT pod(s) younger than 60s, ready, running $EXPECTED_IMAGE"
|
|
|
|
if [ "$COUNT" -gt 0 ] 2>/dev/null; then
|
|
echo "$RESULT" | jq -r '.[] | " \(.name) age=\(.age)s image=\(.image) started=\(.started)"'
|
|
echo "$DEPLOY is live!"
|
|
FOUND=1
|
|
break
|
|
fi
|
|
|
|
sleep 5
|
|
done
|
|
|
|
if [ "$FOUND" -ne 1 ]; then
|
|
echo "ERROR: No new ready pod for $DEPLOY with image $EXPECTED_IMAGE appeared within 5 minutes"
|
|
exit 1
|
|
fi
|
|
done
|