# GitLab CI configuration for rmcp-actix-web

# Include Rust-specific CI configuration
include:
  - local: '.gitlab/ci/rust.gitlab-ci.yml'

# Global variables for package versions
variables:
  COMMITLINT_CLI_VERSION: "17.6.1"
  COMMITLINT_CONFIG_VERSION: "17.6.1"
  SEMANTIC_RELEASE_VERSION: "21.0.7"
  SEMANTIC_RELEASE_CHANGELOG_VERSION: "6.0.3"
  SEMANTIC_RELEASE_GIT_VERSION: "10.0.1"
  SEMANTIC_RELEASE_GITLAB_VERSION: "12.1.1"
  SEMANTIC_RELEASE_EXEC_VERSION: "6.0.3"
  SEMANTIC_RELEASE_CARGO_VERSION: "2.4.44"
  CONVENTIONAL_CHANGELOG_CONVENTIONALCOMMITS_VERSION: "7.0.2"

# Workflow rules for automatic pipeline cancellation
workflow:
  auto_cancel:
    on_new_commit: interruptible
    on_job_failure: none
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
    - if: $CI_COMMIT_TAG

# Semantic release job template - common setup for semantic-release jobs
.semantic-release-template: &semantic-release-template
  image: rust:1.91.1@sha256:ad8c72c693b517ed60c930839daed91a5696fa6118f031d888cd0b7055a921a3
  before_script:
    # Install Node.js in Rust image
    - apt-get update -qq && apt-get install -y -qq curl
    - curl -fsSL https://nodejs.org/dist/v20.11.1/node-v20.11.1-linux-x64.tar.xz | tar -xJ -C /usr/local --strip-components=1
    - node --version
    - npm --version
    # Install semantic-release and plugins
    - npm install -g semantic-release@${SEMANTIC_RELEASE_VERSION} @semantic-release/changelog@${SEMANTIC_RELEASE_CHANGELOG_VERSION} @semantic-release/git@${SEMANTIC_RELEASE_GIT_VERSION} @semantic-release/gitlab@${SEMANTIC_RELEASE_GITLAB_VERSION} @semantic-release/exec@${SEMANTIC_RELEASE_EXEC_VERSION} conventional-changelog-conventionalcommits@${CONVENTIONAL_CHANGELOG_CONVENTIONALCOMMITS_VERSION}
    # Install semantic-release-cargo static binary
    - curl -L https://github.com/semantic-release-cargo/semantic-release-cargo/releases/download/v${SEMANTIC_RELEASE_CARGO_VERSION}/semantic-release-cargo-x86_64-unknown-linux-musl -o /usr/local/bin/semantic-release-cargo
    - chmod +x /usr/local/bin/semantic-release-cargo
    - semantic-release-cargo --version
  interruptible: true

# Pipeline stages
stages:
  - build
  - test
  - lint
  - docs
  - examples
  - integration
  - release

# Semantic release config lint job - validate semantic-release configuration
semantic-release-lint:
  <<: *semantic-release-template
  stage: lint
  script:
    - semantic-release --dry-run --no-ci

# Commit lint job - check commit message format
commitlint:
  image: node:22.21.1@sha256:4ad2c2b350ab49fb637ab40a269ffe207c61818bb7eb3a4ea122001a0c605e1f
  stage: lint
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push"'
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: '$CI_COMMIT_TAG'
      when: never
  variables:
    GIT_FETCH_EXTRA_FLAGS: '+refs/heads/main:refs/remotes/origin/main'
  before_script:
    - npm install -g @commitlint/cli@${COMMITLINT_CLI_VERSION} @commitlint/config-conventional@${COMMITLINT_CONFIG_VERSION}
  script:
    - |
      echo "module.exports = {
        extends: ['@commitlint/config-conventional'],
        rules: {
          'header-max-length': [2, 'always', 150],
        },
      }" > ./commitlint.config.js
      commitlint --from=$(git merge-base origin/main HEAD) --to=$CI_COMMIT_SHA
  interruptible: true

# Semantic release job - automated versioning and releases
semantic-release:
  <<: *semantic-release-template
  stage: release
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $RELEASE_ENABLED == "true"'
    - if: '$CI_PIPELINE_SOURCE == "web" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $RELEASE_ENABLED == "true"'
  variables:
    GIT_FETCH_EXTRA_FLAGS: '+refs/heads/main:refs/remotes/origin/main'
  before_script:
    - !reference [.semantic-release-template, before_script]
    # Configure git
    - git config user.name "${GITLAB_USER_NAME}"
    - git config user.email "${GITLAB_USER_EMAIL}"
  script:
    - semantic-release
  interruptible: false