---

image: registry.ubicast.net/mediaserver/envsetup

stages:
  - docker
  - lint
  - test
  - test-pgsql-ha
  - test-mediaserver-ha
  - deploy

before_script:
  - make install-dev

docker:build:
  image: docker:stable
  stage: docker
  tags:
    - docker
  rules:
    - if: '$CI_PIPELINE_SOURCE == "push"'
      changes:
        - .devcontainer/Dockerfile
        - ansible/requirements.dev.txt
  before_script:
    - apk add bash make
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN registry.ubicast.net
  script:
    - make docker-build
    - make docker-push

docker:rebuild:
  image: docker:stable
  stage: docker
  tags:
    - docker
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
    - if: '$CI_PIPELINE_SOURCE == "web"'
  before_script:
    - apk add bash make
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN registry.ubicast.net
  script:
    - make docker-build
    - make docker-push

lint:
  image: registry.ubicast.net/mediaserver/envsetup
  stage: lint
  tags:
    - docker
  script:
    - make lint

test:
  image: registry.ubicast.net/mediaserver/envsetup:root
  stage: test
  tags:
    - docker
  rules:
    - if: '$CI_PIPELINE_SOURCE == "web"'
    - if: '$CI_PIPELINE_SOURCE == "merge_requests"'
    - if: '$CI_PIPELINE_SOURCE == "push"'
      changes:
        - ansible/**/*
  script:
    - make test

.preprod-ovh:
  image: registry.ubicast.net/mediaserver/envsetup:root
  stage: deploy
  tags:
    - docker
  before_script:
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - make install-dev
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@git.ubicast.net/mediaserver/envsetup-extra

start-ovh-preprod:
  extends: .preprod-ovh
  environment:
    name: preproduction
    url: https://test-ms.ubicast.video
    on_stop: stop-ovh-preprod
    auto_stop_in: 30 minutes
  rules:
    - if: '$CI_PIPELINE_SOURCE == "web"'
    - if: '$CI_PIPELINE_SOURCE == "schedules"'
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
  script:
    - make start-preprod-ovh

stop-ovh-preprod:
  extends: .preprod-ovh
  environment:
    name: preproduction
    action: stop
  when: manual
  script:
    - make stop-preprod-ovh

test-pgsql-ha:
  image: registry.ubicast.net/mediaserver/envsetup:root
  stage: test-pgsql-ha
  tags:
    - docker
  rules:
    - if: '$CI_PIPELINE_SOURCE == "web"'
    - if: '$CI_PIPELINE_SOURCE == "merge_requests"'
    - if: '$CI_PIPELINE_SOURCE == "push"'
      changes:
        - ansible/**/*
  script:
    - make test pgsql-ha=1

test-mediaserver-ha:
  image: registry.ubicast.net/mediaserver/envsetup:root
  stage: test-mediaserver-ha
  tags:
    - docker
  rules:
    - if: '$CI_PIPELINE_SOURCE == "web"'
    - if: '$CI_PIPELINE_SOURCE == "merge_requests"'
    - if: '$CI_PIPELINE_SOURCE == "push"'
      changes:
        - ansible/**/*
  script:
    - make test mediaserver-ha=1

...
