image: php:8.0-fpm

stages:
  - quality
  - bundle
  - release

cache:
  paths:
    - vendor/
    - node_modules/

before_script:
  - apt-get update -y

  # Install git (the php image doesn't have it) which is required by composer
  - apt-get install git -y

  - apt-get install -y libicu-dev

  # Install intl PHP extension
  - docker-php-ext-install intl

  # install rsync for file transfers
  - apt-get install rsync -y

  # install wget to download archives
  - apt-get install wget

  # Install composer
  - apt-get install zip unzip
  - php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
  - php composer-setup.php
  - php -r "unlink('composer-setup.php');"

  # Install NodeJS for NPM
  - curl -sL https://deb.nodesource.com/setup_12.x | bash -
  - apt-get update && apt-get install -y nodejs

  # Install all php and js dependencies
  - php composer.phar install --prefer-dist --no-ansi --no-interaction --no-progress --ignore-platform-reqs
  - npm install

tests:
  stage: quality
  script:
    - composer test

analysis:
  stage: quality
  script:
    # run phpstan
    - composer analyse

code-review:
  stage: quality
  script:
    - composer rector

bundle_app:
  stage: bundle
  script:
    # delete installed git hooks because captainhooks (dev dependency) will be removed
    - rm -rf ./.git/hooks

    # remove dev dependencies using the --no-dev option
    - php composer.phar install --no-dev --prefer-dist --no-ansi --no-interaction --no-progress --ignore-platform-reqs

    # build all UI assets
    - npm run build

    # download GeoLite2-City archive and extract it to writable/uploads
    - wget -c "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=$MAXMIND_LICENCE_KEY&suffix=tar.gz" -O - | tar -xz -C ./writable/uploads/

    # rename extracted archives' folders
    - mv ./writable/uploads/GeoLite2-City* ./writable/uploads/GeoLite2-City

    # create bundle folder: uses .rsync-filter (-F) file to copy only needed files
    - rsync -aF --progress . ./castopod-host
  artifacts:
    name: "castopod-host-${CI_COMMIT_REF_SLUG}_${CI_COMMIT_SHORT_SHA}"
    paths:
      - castopod-host
  except:
    - main
    - beta
    - alpha

release_app:
  stage: release
  script:
    # install required packages for prepare-release.sh script
    - apt-get install jq -y
    - apt-get install zip -y

    # make scripts/prepare-release.sh executable
    - chmod +x ./scripts/prepare-release.sh

    # IMPORTANT: delete local git tags before release to prevent eventual script failure (ie. tag already exists)
    - git tag | xargs git tag -d

    # run semantic-release script (configured in `.releaserc.json` file)
    - npm run release
  only:
    - main
    - alpha
    - beta