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
  allow_failure: true
  only:
    changes:
      - Dockerfile-base


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

test_pref06:
  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/pref06/* || true
  - rm artifacts.zip || true
  - source bin/activate
  - /etc/init.d/tor start
  - python ./pref06.py
  retry: 10
  only:
  - main
  cache:
    key: $CI_COMMIT_REF_SLUG
    paths:
    - bin/
    - lib/
    - pyvenv.cfg
  artifacts:
    paths:
    - data/pref06/*.txt
    - output.log
    expire_in: 1 hour

test_pref13:
  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/pref13/* || true
  - rm artifacts.zip || true
  - source bin/activate
  - /etc/init.d/tor start
  - python ./pref13.py
  retry: 10
  only:
  - main
  cache:
    key: $CI_COMMIT_REF_SLUG
    paths:
    - bin/
    - lib/
    - pyvenv.cfg
  artifacts:
    paths:
    - data/pref13/*.txt
    - output.log
    expire_in: 1 hour

test_pref35:
  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/pref35/* || true
  - rm artifacts.zip || true
  - source bin/activate
  - /etc/init.d/tor start
  - python ./pref35.py
  retry: 10
  only:
  - main
  cache:
    key: $CI_COMMIT_REF_SLUG
    paths:
    - bin/
    - lib/
    - pyvenv.cfg
  artifacts:
    paths:
    - data/pref35/*.txt
    - output.log
    expire_in: 1 hour

test_ppparis:
  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/ppparis/* || true
  - rm artifacts.zip || true
  - source bin/activate
  - python ./ppparis.py
  retry: 10
  only:
  - main
  cache:
    key: $CI_COMMIT_REF_SLUG
    paths:
    - bin/
    - lib/
    - pyvenv.cfg
  artifacts:
    paths:
    - data/ppparis/*.txt
    - output.log
    expire_in: 1 hour
