variables:
  - &minio_image 'minio/mc:RELEASE.2021-10-07T04-19-58Z'
  - &minio_environment
    AWS_ACCESS_KEY_ID:
      from_secret: cache_s3_access_key
    AWS_SECRET_ACCESS_KEY:
      from_secret: cache_s3_secret_key
    MC_HOST: 'https://s3.ci.opencloud.eu'
    CACHE_BUCKET:
      from_secret: cache_s3_bucket
    PUBLIC_BUCKET: public

when:
  - event:
      - push
      - manual
    branch: ${CI_REPO_DEFAULT_BRANCH}
    status:
      - success
      - failure
  - event: pull_request
    evaluate: |
      !(CI_COMMIT_SOURCE_BRANCH matches "next-release/(main|stable-*)" && CI_COMMIT_AUTHOR == "openclouders")
    status:
      - success
      - failure
  - event: cron
    cron: nightly*
    status:
      - success
      - failure

depends_on:
  - ui-tests

skip_clone: true

matrix:
  include:
    - JOB_NAME: purge-desktop-build
      PURGE_PATH: desktop/client-build/
      TTL: 1d
    - JOB_NAME: purge-python-cache
      PURGE_PATH: desktop/python-cache/
      TTL: 14d
    - JOB_NAME: purge-browsers-cache
      PURGE_PATH: desktop/browsers-cache/
      TTL: 14d
    - JOB_NAME: purge-logs
      PURGE_PATH: desktop/testlogs/
      USE_PUBLIC_BUCKET: true
      TTL: 7d

steps:
  - name: ${JOB_NAME}
    image: *minio_image
    environment:
      <<: *minio_environment
    commands:
      - mc alias set s3 "$MC_HOST" "$AWS_ACCESS_KEY_ID" "$AWS_SECRET_ACCESS_KEY"
      - if [ "$USE_PUBLIC_BUCKET" = "true" ]; then CACHE_BUCKET=$PUBLIC_BUCKET; fi
      - to_delete=$(mc find "s3/$CACHE_BUCKET/${PURGE_PATH}" --older-than "${TTL}")
      - if [ "$to_delete" = "" ]; then exit 0; fi
      - mc rm $to_delete
