stages:
  - build
  - lint
  - test
  - build_site
  - deploy

variables:
  CARGO_HOME: "$CI_PROJECT_DIR/cargo"
  DEFAULT_PIPELINE_NAME: "$CI_COMMIT_BRANCH - $CI_COMMIT_MESSAGE"
  SCHEDULED_PIPELINE_NAME: "Daily scheduled build pipeline"

cache:
  paths:
    - target/
    - cargo/

build:
  image: rust:latest
  stage: build
  script:
    - cargo build

lint:
  image: rust:latest
  stage: lint
  script:
    - rustup component add clippy
    - cargo clippy --all-targets -- -D warnings

test:
  image: rust:latest
  stage: test
  script:
    - cargo test --verbose
  rules:
    - if: "$CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH"

build_site:
  image: rust:latest
  stage: build_site
  script:
    - cargo run
    - mv output public
  artifacts:
    paths:
      - public

pages:
  stage: deploy
  script:
    - echo "Publishing site..."
  dependencies:
    - build_site
  artifacts:
    paths:
      - public
  rules:
    - if: "$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH"

workflow:
  name: $CI_COMMIT_REF_NAME
  rules:
    - if: $SCHEDULED_BUILD_PIPELINE == 'true'
      variables:
        CI_COMMIT_REF_NAME: $SCHEDULED_PIPELINE_NAME
    - if: $SCHEDULED_BUILD_PIPELINE != 'true'
      variables:
        CI_COMMIT_REF_NAME: $DEFAULT_PIPELINE_NAME