workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == 'merge_request_event'
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
    - if: $CI_COMMIT_BRANCH == 'develop'
    - if: $CI_COMMIT_BRANCH =~ /^v3*/
    - if: $CI_COMMIT_BRANCH =~ /^v4*/

default:
  tags:
    - ovh

stages:
  - default_validate
  - lint
  - build
  - test
  - deliver

include:
  - project: 'digit-c4/digitc4-template-cicd'
    file: 'gitlab-ci-ansible.yml'
    ref: main

lint-job:
  stage: lint
  before_script:
    - python3 -m venv "$CI_PROJECT_DIR/plugins/venv"
    - source "$CI_PROJECT_DIR/plugins/venv/bin/activate"
    - pip install 'pylint==3.2'
  script:
    - pylint "$CI_PROJECT_DIR/tests"
  after_script:
    - deactivate
    - rm -rf "$CI_PROJECT_DIR/plugins/venv"

build-job:       # This job runs in the build stage, which runs first.
  stage: build
  script:
    - ansible-playbook ansible/build.yml

run-test:
  stage: test
  trigger:
    include:
      - local: '.gitlab/test.yml'
    strategy: depend
  # Create 'resource_group', to avoid collision when executing many pipelines at the same time.
  resource_group: test-group

deliver-image-job:
  stage: deliver
  variables:
    IMAGE_TAG: "testing-${CI_COMMIT_BRANCH}"
    IMAGE_TAG_POSTGRES: "15-alpine"
    IMAGE_TAG_REDIS: "7-alpine"
  script:
    - ansible-playbook ansible/deliver.yml
  rules:
    # deliver latest
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      variables:                              # Override IMAGE_TAG variable
        IMAGE_TAG: "latest"
    # deliver tagged image
    - if: $CI_COMMIT_TAG
      variables:                              # Override IMAGE_TAG variable
        IMAGE_TAG: $CI_COMMIT_TAG
    # For custom Testing images, keep standard, naming your branch as v4.1-someSquadNameRef
    - if: $CI_COMMIT_BRANCH == 'develop'
    - if: $CI_COMMIT_BRANCH =~ /^v3*/
    - if: $CI_COMMIT_BRANCH =~ /^v4*/