variables:
  DEBIAN_FRONTEND: noninteractive
  IMAGE_NAME:      $CI_REGISTRY_IMAGE/base

stages:
  - docker
  - install
  - 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
      changes:
        paths:
        - Dockerfile-base
    - if: $COMPILE_DOCKER

install:
  stage: install
  image: registry.git.laquadrature.net/bastien/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: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $COMPILE_DOCKER == null

.default_pref:
  stage: test
  image: registry.git.laquadrature.net/bastien/raaspotter/base:latest
  tags:
  - unprivileged
  needs: [install]
  script:
  - curl --silent --location --output artifacts.zip "${CI_SERVER_PROTOCOL}://${CI_SERVER_HOST}:${CI_SERVER_PORT}/api/v4/projects/${CI_PROJECT_ID}/jobs/artifacts/${CI_COMMIT_BRANCH}/download?job=${CI_JOB_NAME}&job_token=${CI_JOB_TOKEN}" || true
  - unzip -q artifacts.zip data/"${PREF}"/* || true
  - rm artifacts.zip || true
  - source bin/activate
  - /etc/init.d/tor start
  - python ./cli.py --pref "${PREF}"
  retry: 2
  cache:
    key: $CI_COMMIT_REF_SLUG
    paths:
    - bin/
    - lib/
    - pyvenv.cfg
  artifacts:
    paths:
    - data/${PREF}/*.txt
    - output.log
    expire_in: 1 hour
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $COMPILE_DOCKER == null

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

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

test_pref13:
  variables:
    PREF: "pref13"
  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_pref69:
  variables:
    PREF: "pref69"
  extends: .default_pref
