stages:
 - install
 - test
 - build_containers
 - zenodo
 - deploy_pages

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


install:
  stage: install
  image: python:slim
  script:
    - apt-get -y update
    - pip install .
    - eossr-codemeta2zenodo --help
    - eossr-codemeta2zenodo --input_codemeta_file ./codemeta.json
    - cat .zenodo.json
  only:
    - branches


test:
    stage: test
    image: python:slim
    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
        --capture=no
    <<: *junit_definition


test_ci_eossr:
  stage: test
  trigger: escape2020/wp3/test_eossr


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 --no-cache 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
        - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" "$CI_REGISTRY" --password-stdin
        - docker build -t $CI_REGISTRY_IMAGE:$LAST_RELEASE .
        - docker push $CI_REGISTRY_IMAGE:$LAST_RELEASE
        - docker tag $CI_REGISTRY_IMAGE:$LAST_RELEASE $CI_REGISTRY_IMAGE:latest
        - docker push $CI_REGISTRY_IMAGE:latest
        # 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 container_build && cp Docker/*.tar container_build
        - ls container_build
    artifacts:
      paths:
        - container_build/Docker_*.tar
    only:
        - tags

build_docker_main:
    stage: build_containers
    image: docker:19.03.12
    services:
        - docker:19.03.12-dind
    before_script:
        - cat /etc/os-release  # "Alpine Linux v3.12"
    script:
        - echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" "$CI_REGISTRY" --password-stdin
        - docker build -t $CI_REGISTRY_IMAGE:dev Docker
        - docker push $CI_REGISTRY_IMAGE:dev
    only:
        - master


deploy_zenodo:
  stage: zenodo
  image: gitlab-registry.in2p3.fr/escape2020/wp3/eossr:latest
  dependencies:
    - build_docker
  before_script:
    - eossr-check-connection-zenodo --token $ZENODO_TOKEN --sandbox False -p $CI_PROJECT_DIR
    - mkdir -p /zenodo_build && cp container_build/* /zenodo_build
    - rm -rf container_build
  script:
    - gitlab_prepare_upload_zenodo.sh $CI_PROJECT_NAME $CI_PROJECT_DIR /zenodo_build
    - ls /zenodo_build

    - eossr-upload-repository -t $ZENODO_TOKEN -s False -i /zenodo_build -id $ZENODO_RECORD_ID
  only:
    - tags


pages:
    stage: deploy_pages
    image: ubuntu:latest
    script:
      - apt update --yes && apt install --yes pandoc pip git
      - git fetch --all --tags
      - pip install -U -r docs/doc_requirements.txt
      - pip install .
      - sphinx-multiversion docs public
      - cd public
      - cp -r master/* .
      - for t in `git tag`; do echo $t; sed -i -e "s~../$t/index.html~$t/index.html~" "index.html" ; done
      - cd -
    artifacts:
      paths:
        - public
    only:
      - master

