stages:
  - Gradle Test
  - Documentation
  - Release
  - Build Release

variables:
  project_name: "$CI_PROJECT_NAME"
  SEMANTIC_RELEASE_PACKAGE: "$CI_PROJECT_NAME"
  GRADLE_OPTS: "-Dorg.gradle.daemon=false"
  GRADLE_USER_HOME: "$CI_PROJECT_DIR/.gradle"

workflow:
  rules:
    - if: '$CI_MERGE_REQUEST_EVENT_TYPE == "detached"'
      when: never
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
      when: never
    - if: '$CI_COMMIT_TITLE =~ /^RELEASE:.+$/ && $CI_COMMIT_BRANCH' # && $CI_COMMIT_TAG == null
      when: never
    - when: always

Gradle Test:
  image: griefed/baseimage-ubuntu-jdk-8:2.0.4
  stage: Gradle Test
  before_script:
    - echo "**** Running in $CI_JOB_ID ****"
    - echo "**** Java location ****"
    - which java
    - echo "**** Java version ****"
    - java -version
    - echo "**** Allowing execution of gradlew ****"
    - chmod +x gradlew
  script:
    - echo "**** Building ServerPackCreator ****"
    - ./gradlew build --info --full-stacktrace
  artifacts:
    when: always
    name: "$CI_COMMIT_BRANCH-$CI_COMMIT_SHORT_SHA-$CI_JOB_NAME"
    expose_as: "Gradle-Test-Artifacts"
    paths:
      - build
    expire_in: 1 week
  cache:
    - key:
        files:
          - build.gradle
          - gradle/gradle-wrapper.properties
      paths:
        - .gradle

Release:
  needs:
    - job: 'Gradle Test'
      artifacts: false
  stage: Release
  image: ghcr.io/griefed/gitlab-ci-cd:2.2.2
  script:
    - npx semantic-release
  rules:
    - if: '$CI_COMMIT_BRANCH == "alpha" && $CI_COMMIT_TITLE !~ /^RELEASE:.+$/ && $CI_SERVER_HOST == "git.griefed.de"'
    - if: '$CI_COMMIT_BRANCH == "beta" && $CI_COMMIT_TITLE !~ /^RELEASE:.+$/ && $CI_SERVER_HOST == "git.griefed.de"'
    - if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_TITLE !~ /^RELEASE:.+$/ && $CI_SERVER_HOST == "git.griefed.de"'

pages:
  image: griefed/baseimage-ubuntu-jdk-8:2.0.4
  stage: Documentation
  before_script:
    - which java
    - chmod +x gradlew
  script:
    - "./gradlew javaDoc -Pversion='' --info --full-stacktrace -x test"
    - cp -Rf build/docs/javadoc public
    - LC_COLLATE=C ls -ahl --group-directories-first --color=auto public
  only:
    - main
  artifacts:
    paths:
      - public
    expire_in: 1 week