image: registry.edugit.org/teckids/docker-images/python-pimped:master

stages:
  - test
  - build
  - deploy

variables:
  GIT_SUBMODULE_STRATEGY: recursive
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

cache:
  key:
    files:
      - poetry.lock
      - pyproject.toml
  paths:
    - .cache/pip
    - .tox

test:
  stage: test
  before_script:
    - adduser --disabled-password --gecos "Test User" testuser
  script:
    - sudo -u testuser tox -e py38
  artifacts:
    paths:
      - htmlcov/

lint:
  stage: test
  script:
    - tox -e lint,security
  allow_failure: true

build_dist:
  stage: build
  script:
    - tox -e build
  artifacts:
    paths:
      - dist/

pages:
  stage: deploy
  script:
    - tox -e docs -- BUILDDIR=../public/docs
  artifacts:
    paths:
    - public/
  only:
  - master

build_docker:
  stage: build
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  script:
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" >/kaniko/.docker/config.json
    - /kaniko/executor
       --context $CI_PROJECT_DIR
       --dockerfile $CI_PROJECT_DIR/Dockerfile
       --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
       --cache=true
       --cleanup
    - /kaniko/executor
       --context $CI_PROJECT_DIR/docker/nginx
       --dockerfile $CI_PROJECT_DIR/docker/nginx/Dockerfile
       --destination $CI_REGISTRY_IMAGE/nginx:$CI_COMMIT_REF_NAME
       --cache=true
       --cleanup
  only:
    - master
    - tags

deploy_demo-master:
  stage: deploy
  environment:
    name: demo/master
    url: http://demo-master.biscuit-sis.org  
  before_script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo "$SSH_KNOWN_HOSTS" >~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
  script:
    - grep -v "build:" docker-compose.yml | ssh root@demo-master.biscuit-sis.org
       env BISCUIT_IMAGE_TAG=${CI_COMMIT_REF_NAME}
       docker-compose
        -p biscuit-${CI_ENVIRONMENT_SLUG}
        -f /dev/stdin
        pull
    - grep -v "build:" docker-compose.yml | ssh root@demo-master.biscuit-sis.org
       env BISCUIT_IMAGE_TAG=${CI_COMMIT_REF_NAME}
           NGINX_HTTP_PORT=80
           BISCUIT_maintenance__debug=true
       docker-compose
        -p biscuit-${CI_ENVIRONMENT_SLUG}
        -f /dev/stdin
        up -d
  only:
    - master
