stages:
  - build
  - debug
  - test



variables:
  RAILS_ENV: test

  # Docker image containing Rails app and all Ruby gems
  RAILS_IMAGE: registry.gitlab.com/gumtu/phluid_rails/rails-playwright:latest

  # Docker image extending RAILS_IMAGE, adding Node.js and Playwright
  CI_IMAGE: registry.gitlab.com/gumtu/phluid_rails/rails-playwright-ci:latest





build:image:
  stage: build
  tags:
    - self-hosted
  image: docker:24.0.7

  rules:
  #  # Only build on changes to Rails code or Dockerfiles
  #  - changes:
  #    - rails/**/*     # Rails app code that affects image
  #    - Dockerfile*    # Image configuration
  #    - .gitlab-ci.yml # CI/CD behavior
  #  - when: always

    # Never run otherwise
    - when: never

  script:
    # 1. Log in to GitLab Container Registry for image push/pull access
    - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin registry.gitlab.com

    # 2. Pull existing Rails image for Docker layer caching (ignore errors if image does not exist)
    - docker pull $RAILS_IMAGE || true

    # 3. Build Rails image using cache from previous build
    - docker build --cache-from $RAILS_IMAGE -t $RAILS_IMAGE ./rails

    # 4. Push Rails image to the container registry
    - docker push $RAILS_IMAGE

    # 5. Pull existing CI image (with Node.js & Playwright) for caching
    - docker pull $CI_IMAGE || true

    # 6. Build CI image using both previous images as build cache
    - docker build --cache-from $RAILS_IMAGE --cache-from $CI_IMAGE -t $CI_IMAGE -f Dockerfile.ci .

    # 7. Push CI image to the container registry
    - docker push $CI_IMAGE





debug:db:
  stage: debug
  tags: [self-hosted]
  image:
    name: $CI_IMAGE
    entrypoint: [""]  # Override entrypoint to allow custom command execution
  rules:
    # Allow manual trigger for database inspection
    - when: manual
      allow_failure: true

  before_script:
    # Navigate into the Rails application directory
    - cd rails

    # Set seed mode for test-specific data
    - export SEED=-seed:test

    # Rebuild the database from scratch
    - bin/rails db:drop
    - bin/rails db:create
    - bin/rails db:migrate
    - bin/rails db:seed

  script:
    # Print key database metrics for sanity checks
    - |
      echo "🔍 Debugging database..."
      bin/rails runner '
        puts "ENV: #{Rails.env}"
        puts "Tables: #{ActiveRecord::Base.connection.tables.sort}"
        puts "Users: #{User.count}"
        puts "Emails: #{Email.count}"
        puts "Tasks: #{Task.count}"
        puts "UIDs: #{Uid.count}"
        puts "Views: #{View.count}"
      '





test:e2e:
  stage: test
  needs:
    - job: build:image
  tags:
    - self-hosted
  image:
    name: $CI_IMAGE
    entrypoint: [""]  # Clear default entrypoint to run custom commands

  rules:
  #  # Only run tests for merge requests targeting the `test` branch
  #  - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "test"'
  #    when: always
      
    # Skip all other cases
    - when: never  

  before_script:
    # 1. Navigate into the Rails application directory
    - cd rails

    # 2. Drop, recreate, migrate, and seed test database with test seed data
    - export SEED=-seed:test
    - bin/rails db:drop
    - bin/rails db:create
    - bin/rails db:migrate
    - bin/rails db:seed

    # 3. Start Rails server in test mode, background, on port 3000
    - bin/rails server -b 0.0.0.0 -p 3000 -d

    # 4. Wait up to 20 seconds for Rails /healthcheck, fail CI job if Rails doesn't start
    - n=0
    - until curl --silent --fail http://localhost:3000/healthcheck 2>/dev/null || [ $n -ge 10 ]; do echo "Waiting for Rails to start…"; n=$((n+1)); sleep 2; done
    - if [ $n -ge 10 ]; then echo "Rails never started—aborting."; exit 1; fi

    # 5. Return to repo root so Playwright finds package.json and tests
    - cd ..

    # 6. Install JavaScript dependencies as locked in package-lock.json
    - npm ci

  script:

    # 7. Run Playwright end-to-end test suite against local Rails server
    - npx playwright test --config=playwright/playwright.config.js


  