stages:
  - test

variables:
  DOCKER_TLS_CERTDIR: "/certs"
  DOCKER_HOST: "tcp://docker:2376"
  DOCKER_TLS_VERIFY: "1"
  DOCKER_CERT_PATH: "$DOCKER_TLS_CERTDIR/client"
  DOCKER_DRIVER: overlay2

image: docker:27.5.1

services:
  - name: docker:27.5.1-dind
    command: [
      "--registry-mirror=http://gitlab.prodcontest.ru:5015",
      "--registry-mirror=https://dockerhub.timeweb.cloud"
    ]

test:
  stage: test
  tags: 
    - backend
  only:
    - master
  before_script:
    - for try in {1..10}; do sleep 1; docker info && break ; done
    - echo "$DOCKER_DEPLOY_KEY" | docker login -u "backend" --password-stdin $CI_REGISTRY
    - docker compose version
    - |
      find solution -type f -name "Dockerfile*" ! -name "*.*" -print0 | while IFS= read -r -d '' file; do
        echo "Обрабатываем файл: $file"
        if ! grep -q "^ENV http_proxy" "$file"; then
          sed -i '/^FROM.*alpine/ a RUN apk --update --no-cache add openssl wget' "$file"
          sed -i '/^FROM/ a ENV http_proxy=http:\/\/84.201.181.188:15000\nENV https_proxy=http:\/\/84.201.181.188:15000\nENV no_proxy=localhost' "$file"
          echo -e "\nENV http_proxy=\nENV https_proxy=\nENV no_proxy=" >> "$file"
          echo "Добавлены прокси-переменные в $file"
        else
          echo "Прокси-переменные уже присутствуют в $file"
        fi
      done
    - cd solution && docker compose up -d
  script:
    - docker run -v $CI_PROJECT_DIR:/app --network host gitlab.prodcontest.ru:5050/2025-final-indiv-repos/backend-checker:v2.2
  after_script:
    - cd solution && docker compose logs > ../compose.log && docker compose down
  artifacts:
    when: always
    access: 'developer'
    paths:
      - events.csv
      - compose.log
