stages:
 - install
 - test
 - build_containers
 - zenodo
 - deploy

.junit_template: &junit_definition
    artifacts:
      reports:
        junit: "junit*.xml"


install_py37:
  stage: install
  image: python:3.7-buster
  script:
    - apt-get -y update
    - python setup.py install
    - eossr-codemeta2zenodo --help
    - eossr-codemeta2zenodo --input_codemeta_file ./codemeta.json
    - cat .zenodo.json
  only:
    - branches

test_py37:
    stage: test
    image: python:3.7-buster
    script:
      - apt-get -y update
      - pip install pytest pytest-cov
      - pip install -e .
      - pytest eossr/
        --junitxml=junit_py37.xml
        --color=yes
        --verbose
        --cov=eossr
        --cov-report=xml
        --cov-report=term
    <<: *junit_definition

build_docker:
    stage: build_containers
    image: docker:19.03.12
    services:
        - docker:19.03.12-dind
    before_script:
        - cat /etc/os-release  # "Alpine Linux v3.12"
        - apk add git
        - export LAST_RELEASE=`git ls-remote --tags --refs --sort="v:refname" $CI_PROJECT_URL.git | tail -n1 | sed 's/.*\///'`
        - echo $LAST_RELEASE
    script:
        - cd Docker
        - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
        - docker build -t $CI_REGISTRY_IMAGE:$LAST_RELEASE .
        - docker push $CI_REGISTRY_IMAGE:$LAST_RELEASE
        # Save docker image into a tar file to be uploaded in next stage
        - docker save -o Docker_${CI_PROJECT_NAME}_$LAST_RELEASE.tar $CI_REGISTRY_IMAGE:$LAST_RELEASE
        - cd .. && mkdir -p build && cp Docker/*.tar build
        - ls build
    artifacts:
      paths:
        - build/Docker_*.tar
    only:
        - tags

deploy_zenodo:
  stage: zenodo
  image: gitlab-registry.in2p3.fr/escape2020/wp3/eossr:v0.2
  dependencies:
    - build_docker
  before_script:
    - eossr-check-connection-zenodo --token $ZENODO_TOKEN --sandbox False -p $CI_PROJECT_DIR
  script:
    - mkdir -p build
    - parse_last_release_git.sh $CI_PROJECT_NAME $CI_PROJECT_URL
    - if [[ -f ./codemeta.json ]]; then cp ./codemeta.json ./build; fi
    - ls ./build\
    # - eossr-upload-new-deposit --token $ZENODO_TOKEN --sandbox False --input-dir ./build
    - eossr-upload-new-version-deposit -t $ZENODO_TOKEN -s False -i ./build -id $ZENODO_RECORD_ID
  only:
    - tags

pages:
    stage: deploy
    image: ubuntu:latest
    script:
      - apt update --yes && apt install --yes pandoc pip
      - pip install -U -r docs/doc_requirements.txt
      - sphinx-build -b html docs public
    artifacts:
      paths:
        - public
    only:
      - master
