variables:
  - &build_image 'opencloudeu/desktop-client-build:ubuntu-24.04-qt6.10'
  - &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
    CACHE_BUCKET:
      from_secret: cache_s3_bucket
    MC_HOST: 'https://s3.ci.opencloud.eu'
  - &trigger_path
    include:
      - test/gui/**
      - src/**
      - cmake/**
      - THEME.cmake
      - VERSION.cmake
      - CMakeLists.txt
      - QSFERA.cmake
      - .woodpecker/**
    exclude:
      - .woodpecker/ready-release-go.yaml
      - .woodpecker/translation.yaml
      - test/gui/manual-test-plan

when:
  - branch:
      - main
      - stable-*
    event:
      - push
      - manual
    path:
      <<: *trigger_path
  - event: tag
  - event: pull_request
    path:
      <<: *trigger_path
    evaluate: |
      !(CI_COMMIT_SOURCE_BRANCH matches "next-release/(main|stable-*)" && CI_COMMIT_AUTHOR == "openclouders")
  - event: cron
    cron: nightly*

workspace:
  base: /woodpecker/
  path: desktop

steps:
  - name: check-caches
    image: *minio_image
    environment:
      <<: *minio_environment
    commands:
      - mc alias set s3 $MC_HOST $AWS_ACCESS_KEY_ID $AWS_SECRET_ACCESS_KEY
      - bash test/gui/woodpecker/script.sh check_python_cache
      - bash test/gui/woodpecker/script.sh check_browsers_cache

  - name: install-python-modules
    image: *build_image
    commands:
      - . ./.woodpecker.env
      - if $PYTHON_CACHE_FOUND; then exit 0; fi
      - cd test/gui/
      - python3 -m venv .venv --system-site-packages
      - . .venv/bin/activate
      - make pip-install
      - tar -czf python-cache.tar.gz .venv

  - name: install-browsers
    image: *build_image
    environment:
      PLAYWRIGHT_BROWSERS_PATH: /woodpecker/desktop/test/gui/.playwright
    commands:
      - . ./.woodpecker.env
      - if $BROWSER_CACHE_FOUND; then exit 0; fi
      - cd test/gui/
      - python3 -m venv .venv --system-site-packages
      - . .venv/bin/activate
      - grep "^playwright" requirements.txt | pip install -r /dev/stdin
      - make install-chromium
      - tar -czf playwright-browsers.tar.gz .playwright

  - name: upload-browsers-cache
    image: *minio_image
    environment:
      <<: *minio_environment
    commands:
      - . ./.woodpecker.env
      - if $BROWSER_CACHE_FOUND; then exit 0; fi
      - playwright_version=$(bash test/gui/woodpecker/script.sh get_playwright_version)
      - mc alias set s3 $MC_HOST $AWS_ACCESS_KEY_ID $AWS_SECRET_ACCESS_KEY
      - mc cp -r -a $CI_WORKSPACE/test/gui/playwright-browsers.tar.gz s3/$CACHE_BUCKET/desktop/browsers-cache/$playwright_version/

  - name: upload-python-cache
    image: *minio_image
    environment:
      <<: *minio_environment
    commands:
      - . ./.woodpecker.env
      - if $PYTHON_CACHE_FOUND; then exit 0; fi
      - requirements_sha=$(bash test/gui/woodpecker/script.sh get_requirementstxt_hash)
      - mc alias set s3 $MC_HOST $AWS_ACCESS_KEY_ID $AWS_SECRET_ACCESS_KEY
      - mc cp -r -a $CI_WORKSPACE/test/gui/python-cache*.tar.gz s3/$CACHE_BUCKET/desktop/python-cache/$requirements_sha/
