stages:
  - test
  - chart

variables:
  DOCKER_TLS_CERTDIR: ""

# Helm chart publishing
publish-helm-chart:
  image: devth/helm:v3.6.3
  stage: chart
  script:
    # push Helm chart
    - "upload_filename=$(helm package . | sed 's/^Successfully packaged chart and saved it to: \\(.*\\)/\\1/')"
    - curl -u $HELM_CHART_REPOSITORY_CREDENTIALS -v -X POST https://charts-public.hub.eox.at/api/charts --data-binary "@${upload_filename}"
  only:
    - tags


generate-docker-compose-configs:
  stage: chart
  trigger: esa/prism/config-values
  only:
    # we trigger a regeneration on every push to main.
    # this could potentally diverge from the latest published
    # chart, but they are only applied manually when necessary
    - main


integration-test:
  image: registry.gitlab.eox.at/vs/vs-deployment/docker-base-testing:latest
  services:
    - docker:20.10.8-dind
  stage: test
  before_script:
    - k3d cluster create testgitlabci-${CI_JOB_ID} --agents 1 --wait
    - kubectl cluster-info
    - export PREPROCESSOR_RESULTS_BUCKET=$(cat /proc/sys/kernel/random/uuid)
    - kubectl apply -f testing/pvc-redis.yaml
    # default timeout is 7 min, move to 7m for some leeway
    - helm install my-helm-release . --wait --timeout 7m --values ./testing/values-testing.yaml --values "${SECRET_HELM_VALUES_TESTING}" --set global.storage.target.kwargs.container=${PREPROCESSOR_RESULTS_BUCKET}
    - kubectl get po -A
  script:
    - cd testing && ./run_integration_test.sh
  after_script:
    # print info in case anything has gone wrong
    - kubectl get po -A || true
    # fetch logs
    - mkdir "${CI_PROJECT_DIR}/logs"
    - kubectl get po -o name | while read POD ; do kubectl logs "$POD" > "${CI_PROJECT_DIR}/logs/$(echo $POD | cut -d'/' -f2).txt" ; done || true
    - k3d cluster delete testgitlabci-${CI_JOB_ID}
  artifacts:
    when: always
    expire_in: 4 week
    paths:
      - logs/*
  except:
    - main
