# GitLab CI/CD configuration for mod_reqin_log
# Uses Docker-in-Docker (dind) for building and testing

stages:
  - build
  - test
  - package
  - verify

# =============================================================================
# Variables
# =============================================================================
variables:
  DOCKER_TLS_CERTDIR: "/certs"
  DOCKER_DRIVER: overlay2
  VERSION: "1.0.2"

# =============================================================================
# Build Stage - Compile all RPM packages
# =============================================================================

build-packages:
  stage: build
  image: docker:24
  services:
    - docker:24-dind
  script:
    # Build all RPM packages (el8, el9, el10)
    - docker build -f Dockerfile.package --target output --build-arg VERSION=$VERSION -t mod_reqin_log:packages .

    # Create output directories
    - mkdir -p dist/rpm

    # Extract packages from Docker image
    - docker run --rm -v $(pwd)/dist:/output mod_reqin_log:packages sh -c 'cp -r /packages/rpm/* /output/rpm/'

    # List built packages
    - echo "=== RPM Packages ==="
    - ls -la dist/rpm/
  artifacts:
    paths:
      - dist/rpm/
    expire_in: 30 days

# =============================================================================
# Test Stage - Unit tests
# =============================================================================

unit-tests:
  stage: test
  image: docker:24
  services:
    - docker:24-dind
  script:
    # Build test image
    - docker build -f Dockerfile.tests -t mod_reqin_log:tests .

    # Run unit tests
    - docker run --rm mod_reqin_log:tests ctest --output-on-failure

# =============================================================================
# Package Stage - Already done in build-packages
# =============================================================================

# =============================================================================
# Verify Stage - Test RPM package installation on each target distribution
# =============================================================================

verify-rpm-el8:
  stage: verify
  image: docker:24
  services:
    - docker:24-dind
  needs: [build-packages]
  script:
    - docker run --rm -v $(pwd)/dist:/packages rockylinux:8 sh -c "dnf install -y /packages/rpm/*.el8.*.rpm && httpd -M 2>&1 | grep reqin_log && echo 'RPM el8 verification OK'"

verify-rpm-el9:
  stage: verify
  image: docker:24
  services:
    - docker:24-dind
  needs: [build-packages]
  script:
    - docker run --rm -v $(pwd)/dist:/packages rockylinux:9 sh -c "dnf install -y /packages/rpm/*.el9.*.rpm && httpd -M 2>&1 | grep reqin_log && echo 'RPM el9 verification OK'"

verify-rpm-el10:
  stage: verify
  image: docker:24
  services:
    - docker:24-dind
  needs: [build-packages]
  script:
    - docker run --rm -v $(pwd)/dist:/packages almalinux:10 sh -c "dnf install -y /packages/rpm/*.el10.*.rpm && httpd -M 2>&1 | grep reqin_log && echo 'RPM el10 verification OK'"
