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


.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 -e ".[tests]"
    - pytest eossr/
      --junitxml=junit_py37.xml
      --color=yes
      --verbose
      --cov=eossr
      --cov-report=xml
      --cov-report=term
      --capture=no
    - apt update --yes && apt install --yes pandoc
    - pip install -U -r docs/doc_requirements.txt
    - sphinx-build docs _public
  artifacts:
    when: always
    paths:
      - junit_py37.xml
    reports:
      junit: junit_py37.xml


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

test_codemeta_ingestion:
  stage: test
  image: gitlab-registry.in2p3.fr/escape2020/wp3/eossr:latest
  script:
    - eossr-check-connection-zenodo --token $ZENODO_TOKEN --sandbox False -p $CI_PROJECT_DIR
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH'
    - changes:
        - codemeta.json

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:
    - 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
  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 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
  script:
    - eossr-upload-repository -t $ZENODO_TOKEN -s False -i $CI_PROJECT_DIR --archive-name $CI_PROJECT_NAME.zip -id $ZENODO_RECORD_ID
  only:
    - tags


pages:
  stage: deploy_pages
  image: python:slim
  script:
    - apt update --yes && apt install --yes pandoc pip git
    - pip install --upgrade setuptools pip
    - git fetch --all --tags
    - git checkout $CI_COMMIT_REF_NAME
    - pip install -U -r docs/doc_requirements.txt
    - pip install -e .
    - sphinx-multiversion docs public -D nbsphinx_allow_errors=1
    - cd public
    - cp -r $CI_COMMIT_REF_NAME/* .
    - 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


pypi_publish:
  stage: pypi_publish
  image: python:3.8
  before_script:
    - apt-get update -y; apt-get install git -y;
    - pip install --upgrade pip; pip install twine;
    - export LAST_RELEASE=`git ls-remote --tags --refs --sort="v:refname" $CI_PROJECT_URL.git | tail -n1 | sed 's/.*\///'`
    - echo $LAST_RELEASE
  script:
    - python setup.py sdist
    - echo "upload to pypi registry"
    - twine upload --repository-url https://upload.pypi.org/legacy/ dist/* -u __token__ -p ${PYPI_TOKEN}
    - echo "upload to gitlab registry"
    - python -m twine upload --repository-url ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/pypi/ --verbose -u gitlab-ci-token -p ${CI_JOB_TOKEN} dist/*
  only:
    - tags