image: {image}

variables:
  GIT_DEPTH: 10
  # R_LIBS is not empty in rocker images
  R_LIBS: ${CI_PROJECT_DIR}/ci/lib:${R_LIBS}
  R_LIBS_USER: ${CI_PROJECT_DIR}/ci/lib

cache:
  - key: cache-default
    paths:
      - ${R_LIBS_USER}

stages:
  - build
  - test
  - pkgdown
  - pkgdown-move
  - deploy

building:
  stage: build
  script:
    - lsb_release -c
    - R -e "sessionInfo()"
    - if [[ $CI_DEFAULT_BRANCH == "master" || $CI_DEFAULT_BRANCH = "main" ]]; then echo "OK - Default branch is master or main"; else echo "Default branch is not master or main; please add yours ($CI_DEFAULT_BRANCH) where needed, as well as in the present line of code" ; exit 1; fi
    - echo "Library path for packages :" $R_LIBS_USER
    - mkdir -p $R_LIBS_USER
    - Rscript -e 'Sys.getenv("R_LIBS_USER");dir.exists(Sys.getenv("R_LIBS_USER"))'
    - Rscript -e 'Sys.getenv("R_LIBS")'
    - Rscript -e '.libPaths()'
    - Rscript -e 'if(!requireNamespace("pak", quietly = TRUE)) {install.packages("pak")}'
    - Rscript -e 'pak::pak(pkg = ".", upgrade = {upgrade})'
    - Rscript -e 'pak::pkg_install(pkg = c("rcmdcheck"), upgrade = {upgrade})'
    - R -e 'rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check")'
  cache:
    - key: cache-$CI_COMMIT_REF_SLUG
      fallback_keys:
        - cache-$CI_DEFAULT_BRANCH
        - cache-default
      paths:
        - ${R_LIBS_USER}
  artifacts:
    paths:
      - check
    expire_in: 30 days

# For gitlab versions below 15.0,
# in order to have the coverage percentage appear as a gitlab badge,
# you will need to:
# - Go to your project Settings > CI/CD
# - Set the coverage parsing string to: Coverage: \d+\.\d+

coverage:
  stage: test
  allow_failure: true
  when: on_success
  only:
    - main
    - master
    - production
  script:
    # Install pak by security, although should be in cache
    - Rscript -e 'if(!requireNamespace("pak", quietly = TRUE)) {install.packages("pak")}'
    - Rscript -e 'pak::pak(pkg = ".", upgrade = {upgrade})'
    - Rscript -e 'pak::pkg_install(pkg = c("covr", "DT"), upgrade = {upgrade})'
    - Rscript -e 'covr::gitlab(quiet = FALSE)'
  cache:
    - key: cache-$CI_COMMIT_REF_SLUG
      fallback_keys:
        - cache-$CI_DEFAULT_BRANCH
        - cache-default
      paths:
        - ${R_LIBS_USER}
  coverage: '/Coverage: \d+\.\d+/'
  artifacts:
    paths:
      - public
    expire_in: 30 days

# To produce a code coverage report as a GitLab page see
# https://about.gitlab.com/2016/11/03/publish-code-coverage-report-with-gitlab-pages/

integration:
  stage: pkgdown
  allow_failure: true
  when: on_success
  only:
    - main
    - master
    - production
    - test-ci
  script:
    # Install pak by security, although should be in cache
    - Rscript -e 'if(!requireNamespace("pak", quietly = TRUE)) {install.packages("pak")}'
    - Rscript -e 'pak::pak(pkg = ".", upgrade = {upgrade})'
    - Rscript -e 'pak::pkg_install(c("pkgdown"), upgrade = {upgrade})'
    - Rscript -e 'pkgdown::build_site()'
  cache:
    - key: cache-$CI_COMMIT_REF_SLUG
      fallback_keys:
        - cache-$CI_DEFAULT_BRANCH
        - cache-default
      paths:
        - ${R_LIBS_USER}
  artifacts:
    paths:
      - docs
    expire_in: 30 days

integration-test:
  stage: pkgdown-move
  dependencies:
    - integration
  only:
    - test-ci
  script:
    - mkdir -p public/test
    - 'curl --location --output artifacts.zip --header "JOB-TOKEN: $CI_JOB_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/jobs/artifacts/$CI_DEFAULT_BRANCH/download?job=pages" &&
      unzip artifacts.zip &&
      rm artifacts.zip &&
      echo "copied $CI_DEFAULT_BRANCH artifacts" ||
      echo "copied $CI_DEFAULT_BRANCH artifacts failed"'
    - 'curl --location --output artifacts.zip --header "JOB-TOKEN: $CI_JOB_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/jobs/artifacts/production/download?job=pages" &&
      unzip artifacts.zip &&
      rm artifacts.zip &&
      echo "copied production artifacts" ||
      echo "copied production artifacts failed"'
    - cp -r docs/* public/test
  artifacts:
    paths:
      - public
    expire_in: 30 days

integration-production:
  stage: pkgdown-move
  dependencies:
    - integration
  only:
    - production
  script:
    - mkdir -p public/production
    - 'curl --location --output artifacts.zip --header "JOB-TOKEN: $CI_JOB_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/jobs/artifacts/$CI_DEFAULT_BRANCH/download?job=pages" &&
      unzip artifacts.zip &&
      rm artifacts.zip &&
      echo "copied $CI_DEFAULT_BRANCH artifacts" ||
      echo "copied $CI_DEFAULT_BRANCH artifacts failed"'
    - cp -r docs/* public/production
  artifacts:
    paths:
      - public
    expire_in: 30 days

integration-main:
  stage: pkgdown-move
  dependencies:
    - integration
  only:
    - main
    - master
  script:
    - mkdir -p public
    - 'curl --location --output artifacts.zip --header "JOB-TOKEN: $CI_JOB_TOKEN" "$CI_API_V4_URL/projects/$CI_PROJECT_ID/jobs/artifacts/production/download?job=pages" &&
      unzip artifacts.zip &&
      rm artifacts.zip &&
      echo "copied production artifacts" ||
      echo "copied production artifacts failed"'
    - cp -r docs/* public
  artifacts:
    paths:
      - public
    expire_in: 30 days

pages:
  stage: deploy
  script:
    - echo "deployment with cache"
  artifacts:
    paths:
      - public
  only:
    - main
    - master
    - production
    - test-ci
