variables:
  DEBIAN_FRONTEND: noninteractive
  IMAGE_NAME:      $CI_REGISTRY_IMAGE/base

stages:
  - docker
  - install
  - lint
  - test

docker:
  stage: docker
  tags:
    - dind
  variables:
    DOCKER_DRIVER:      "overlay2"
    DOCKER_TLS_CERTDIR: "/certs"
  services:
    - docker:20.10.17-dind
  before_script:
  - docker info
  - docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
  script:
  - docker build -t ${IMAGE_NAME}:latest -f Dockerfile-base .
  - docker push ${IMAGE_NAME}:latest
  image: docker:20.10.17
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE != "schedule"
      changes:
        paths:
        - Dockerfile-base
    - if: $COMPILE_DOCKER

install:
  stage: install
  image: registry.git.laquadrature.net/la-quadrature-du-net/raaspotter/base:latest
  tags:
  - unprivileged
  script:
  - virtualenv --python=/usr/bin/python3 .
  - source bin/activate
  - pip3 install -r requirements.txt
  cache:
    key: $CI_COMMIT_REF_SLUG
    paths:
    - bin/
    - lib/
    - pyvenv.cfg
  rules:
    - if: $COMPILE_DOCKER == null

pep8:
  stage: lint
  image: registry.git.laquadrature.net/la-quadrature-du-net/raaspotter/base:latest
  needs: [install]
  tags:
    - unprivileged
  cache:
    key: $CI_COMMIT_REF_SLUG
    paths:
    - bin/
    - lib/
    - pyvenv.cfg
  script:
    - source bin/activate
    - make lint
  allow_failure: true
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event" || $CI_PIPELINE_SOURCE == "push"

.default_pref:
  stage: test
  image: registry.git.laquadrature.net/la-quadrature-du-net/raaspotter/base:latest
  tags:
  - unprivileged
  needs: [install]
  script:
  - source bin/activate
  - pip install --upgrade -r requirements.txt
  - /etc/init.d/tor start
  - python ./cli.py --pref "${PREF}"
  retry: 2
  cache:
    key: $CI_COMMIT_REF_SLUG-$CI_JOB_NAME_SLUG
    fallback_keys:
    - $CI_COMMIT_REF_SLUG
    paths:
    - bin/
    - lib/
    - pyvenv.cfg
    - data/${PREF}/*.txt
  artifacts:
    paths:
    - data/${PREF}/*.txt
    - output_${PREF}.log
    expire_in: 2 days
  rules:
    - if: $CI_PIPELINE_SOURCE == "schedule" && $COMPILE_DOCKER == null

test_ppparis:
  variables:
    PREF: "ppparis"
  extends: .default_pref

test_pref04:
  variables:
    PREF: "pref04"
  extends: .default_pref

test_pref05:
  variables:
    PREF: "pref05"
  extends: .default_pref

test_pref06:
  variables:
    PREF: "pref06"
  extends: .default_pref

test_pref09:
  variables:
    PREF: "pref09"
  extends: .default_pref

test_pref13:
  variables:
    PREF: "pref13"
  extends: .default_pref

test_pref31:
  variables:
    PREF: "pref31"
  extends: .default_pref

test_pref34:
  variables:
    PREF: "pref34"
  extends: .default_pref

test_pref35:
  variables:
    PREF: "pref35"
  extends: .default_pref

test_pref38:
  variables:
    PREF: "pref38"
  extends: .default_pref

test_pref59:
  variables:
    PREF: "pref59"
  extends: .default_pref

test_pref62:
  variables:
    PREF: "pref62"
  extends: .default_pref

test_pref64:
  variables:
    PREF: "pref64"
  extends: .default_pref

test_pref65:
  variables:
    PREF: "pref65"
  extends: .default_pref

test_pref66:
  variables:
    PREF: "pref66"
  extends: .default_pref

test_pref69:
  variables:
    PREF: "pref69"
  extends: .default_pref

test_pref80:
  variables:
    PREF: "pref80"
  extends: .default_pref

test_pref81:
  variables:
    PREF: "pref81"
  extends: .default_pref

test_pref83:
  variables:
    PREF: "pref83"
  extends: .default_pref

test_pref976:
  variables:
    PREF: "pref976"
  extends: .default_pref
