image: code.podlibre.org:5050/podlibre/castopod-host:latest

stages:
  - prepare
  - quality
  - bundle
  - release

php-dependencies:
  stage: prepare
  script:
    # Install all php dependencies
    - composer install --prefer-dist --no-ansi --no-interaction --no-progress --ignore-platform-reqs
  artifacts:
    paths:
      - vendor/
    expire_in: 30 mins

js-dependencies:
  stage: prepare
  script:
    # Install all npm dependencies
    - npm ci
  artifacts:
    paths:
      - node_modules/
    expire_in: 30 mins

lint-commit-msg:
  stage: quality
  script:
    - chmod +x ./scripts/lint-commit-msg.sh
    # lint commit message
    - ./scripts/lint-commit-msg.sh
  dependencies:
    - js-dependencies

lint-php:
  stage: quality
  script:
    # check php code style
    - vendor/bin/ecs check --ansi
    # phpstan - increase memory limit to 1GB to prevent script failure
    - php -d memory_limit=1G vendor/bin/phpstan analyse --ansi
    # run rector to check for php errors
    - vendor/bin/rector process --dry-run --ansi
  dependencies:
    - php-dependencies

lint-js:
  stage: quality
  script:
    - npm run prettier
    - npm run typecheck
    - npm run lint
    - npm run lint:css
  dependencies:
    - js-dependencies

tests:
  stage: quality
  script:
    # run phpunit without code coverage
    # TODO: add code coverage
    - vendor/bin/phpunit --no-coverage
  dependencies:
    - php-dependencies

bundle:
  stage: bundle
  before_script:
    # prepare dependencies before bundling
    - chmod +x ./scripts/bundle-prepare.sh
    - ./scripts/bundle-prepare.sh
  script:
    # make scripts/bundle.sh executable
    - chmod +x ./scripts/bundle.sh

    # bundle castopod-host with commit ref as version
    - ./scripts/bundle.sh ${CI_COMMIT_REF_SLUG}_${CI_COMMIT_SHORT_SHA}
  dependencies:
    - php-dependencies
    - js-dependencies
  artifacts:
    name: "castopod-host-${CI_COMMIT_REF_SLUG}_${CI_COMMIT_SHORT_SHA}"
    paths:
      - castopod-host
  except:
    - main
    - beta
    - alpha

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

    # prepare dependencies before release, both bundle and package scripts will be run by semantic-release
    - chmod +x ./scripts/bundle-prepare.sh
    - ./scripts/bundle-prepare.sh
  script:
    # make release scripts executable
    - chmod +x ./scripts/bundle.sh
    - chmod +x ./scripts/package.sh

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