# GitLab CI/CD Pipeline for wrkflw
# This pipeline will build and test the Rust project

stages:
  - build
  - test
  - deploy

variables:
  RUST_VERSION: "1.70.0"
  CARGO_TERM_COLOR: always

# Cache settings
cache:
  key: "$CI_COMMIT_REF_SLUG"
  paths:
    - target/
  script:
    - echo "This is a placeholder - the cache directive doesn't need a script"

# Lint job - runs rustfmt and clippy
lint:
  stage: test
  image: rust:${RUST_VERSION}
  script:
    - rustup component add clippy
    - cargo clippy -- -D warnings
  allow_failure: true

# Build job - builds the application
build:
  stage: build
  image: rust:${RUST_VERSION}
  script:
    - cargo build --verbose
  artifacts:
    paths:
      - target/debug
    expire_in: 1 week

# Test job - runs unit and integration tests
test:
  stage: test
  image: rust:${RUST_VERSION}
  script:
    - cargo test --verbose
  dependencies:
    - build

# Release job - creates a release build
release:
  stage: deploy
  image: rust:${RUST_VERSION}
  script:
    - cargo build --release --verbose
  artifacts:
    paths:
      - target/release/wrkflw
    expire_in: 1 month
  rules:
    - if: $CI_PIPELINE_SOURCE == "web" && $BUILD_RELEASE == "true"
      when: always
    - if: $CI_COMMIT_TAG
      when: always
    - when: never

# Custom job for documentation
docs:
  stage: deploy
  image: rust:${RUST_VERSION}
  script:
    - cargo doc --no-deps
    - mkdir -p public
    - cp -r target/doc/* public/
  artifacts:
    paths:
      - public
  only:
    - main

format:
  stage: test
  image: rust:${RUST_VERSION}
  script:
    - rustup component add rustfmt
    - cargo fmt --check
  allow_failure: true

pages:
  stage: deploy
  image: rust:${RUST_VERSION}
  script:
    - cargo doc --no-deps
    - mkdir -p public
    - cp -r target/doc/* public/
  artifacts:
    paths:
      - public
  only:
    - main 