stages: - test - build - deploy variables: DOCKER_IMAGE_REPOSITORY: $DOCKER_REGISTRY/cwshed-clone/belki GATEWAY_IMAGE_REPOSITORY: $DOCKER_REGISTRY/cwshed KUBE_NAMESPACE: belki DOCKER_HOST: tcp://docker:2375 DOCKER_BUILDKIT: "1" DOCKER_TLS_CERTDIR: "" DOCKER_DRIVER: overlay2 # The registry images and production Kubernetes nodes are amd64-only. DOCKER_DEFAULT_PLATFORM: linux/amd64 # The runner fetches the repository before any job script runs. Retry transient # GitLab connectivity failures at that stage instead of failing the pipeline. GET_SOURCES_ATTEMPTS: "3" test: stage: test image: python:3.12-slim variables: # Unit tests must not use the Kubernetes PostgreSQL endpoint supplied to deploy. BELKI_DATABASE_URL: "" BELKI_IDENTITIES_DB: /tmp/belki-test-identities.sqlite3 before_script: - python -m pip install --no-cache-dir --upgrade "pip==26.2.1" - python -m pip install --no-cache-dir -r requirements.txt script: - python -m compileall -q app tests - python -m unittest discover -s tests -q build: stage: build tags: - fast image: docker:29.6.1-cli services: - name: docker:29.6.1-dind alias: docker before_script: - | i=0 until docker info >/dev/null 2>&1; do i=$((i + 1)) if [ "$i" -ge 60 ]; then echo "docker:dind did not become ready within 60 seconds." exit 1 fi echo "Waiting for docker:dind (${i}/60)..." sleep 1 done script: # Docker Registry - echo "$DOCKER_PASSWORD" | docker login $DOCKER_REGISTRY -u "$DOCKER_USERNAME" --password-stdin - export IMAGE_TAG="$CI_COMMIT_SHORT_SHA" - docker build --platform linux/amd64 --pull -t "$DOCKER_IMAGE_REPOSITORY:$IMAGE_TAG" -t "$DOCKER_IMAGE_REPOSITORY:latest" . - docker build --platform linux/amd64 --pull -f Dockerfile.gateway -t "$GATEWAY_IMAGE_REPOSITORY:$IMAGE_TAG" -t "$GATEWAY_IMAGE_REPOSITORY:latest" . - docker push "$DOCKER_IMAGE_REPOSITORY:$IMAGE_TAG" - docker push "$DOCKER_IMAGE_REPOSITORY:latest" - docker push "$GATEWAY_IMAGE_REPOSITORY:$IMAGE_TAG" - docker push "$GATEWAY_IMAGE_REPOSITORY:latest" rules: - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH' deploy: stage: deploy image: alpine/k8s:1.36.2 needs: [build] rules: - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH' before_script: - test -n "$KUBE_CONFIG_B64" - test -n "$VK_GROUP_TOKEN" - test -n "$VK_GROUP_ID" - test -n "$BELKI_DATABASE_URL" - test -n "$POSTGRES_PASSWORD" - test -n "$BELKI_ADMIN_PASSWORD" - mkdir -p "$CI_PROJECT_DIR/.kube" - printf '%s' "$KUBE_CONFIG_B64" | base64 -d > "$CI_PROJECT_DIR/.kube/config" - export KUBECONFIG="$CI_PROJECT_DIR/.kube/config" - kubectl version --client - kubectl -n "$KUBE_NAMESPACE" get serviceaccount gitlab-deploy script: - | kubectl -n "$KUBE_NAMESPACE" create secret generic belki-runtime \ --from-literal=VK_GROUP_TOKEN="$VK_GROUP_TOKEN" \ --from-literal=VK_GROUP_ID="$VK_GROUP_ID" \ --from-literal=VK_API_VERSION="${VK_API_VERSION:-5.199}" \ --from-literal=BELKI_DATABASE_URL="$BELKI_DATABASE_URL" \ --from-literal=POSTGRES_PASSWORD="$POSTGRES_PASSWORD" \ --from-literal=BELKI_ADMIN_PASSWORD="$BELKI_ADMIN_PASSWORD" \ --dry-run=client -o yaml | kubectl apply -f - - > sed -E "s#image: reg.nyan404.ru/cwshed-clone/belki:[^[:space:]]+#image: $DOCKER_IMAGE_REPOSITORY:$CI_COMMIT_SHORT_SHA#g" deploy/k8s/production.yaml | kubectl apply -f - - > sed -E "s#image: reg.nyan404.ru/cwshed:[^[:space:]]+#image: $GATEWAY_IMAGE_REPOSITORY:$CI_COMMIT_SHORT_SHA#g" deploy/k8s/gateway.yaml | kubectl apply -f - # Pod labels include historical ReplicaSets; wait for the Deployment's usable database replica instead. - kubectl -n "$KUBE_NAMESPACE" wait --for=condition=Available deployment/belki-postgres --timeout=180s - kubectl -n "$KUBE_NAMESPACE" rollout status deployment/belki-web --timeout=180s - kubectl -n "$KUBE_NAMESPACE" rollout status deployment/belki-bot --timeout=180s - kubectl -n "$KUBE_NAMESPACE" rollout status deployment/cwshed --timeout=180s - kubectl -n "$KUBE_NAMESPACE" get pods,svc after_script: - rm -rf "$CI_PROJECT_DIR/.kube"