stages:
  - Tests
  - Documentation
  - Release
  - Build Release
#  - Other

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"

services:
  - name: ghcr.io/griefed/gitlab-ci-cd:2.2.28
    alias: docker

image: ghcr.io/griefed/gitlab-ci-cd:2.2.28

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

Build Test:
  stage: Tests
  image: eclipse-temurin:21-jdk
  before_script:
    - echo "**** Running in $CI_JOB_ID ****"
    - echo "**** Java version ****"
    - java -version
    - echo "**** Allowing execution of gradlew ****"
    - chmod +x gradlew
  script:
    - echo "**** Building ServerPackCreator ****"
    - "./gradlew build --info --full-stacktrace"
    - "./gradlew cyclonedxBom --info --full-stacktrace"
    - echo "**** Uploading BOM ****"
    - |
      curl -X "POST" "$DEPENDENCYTRACK_URL" \
        -H 'Content-Type: multipart/form-data' \
        -H "X-Api-Key: $DEPENDENCYTRACK_TOKEN" \
        -F "autoCreate=true" \
        -F "projectName=ServerPackCreator" \
        -F "projectVersion=$CI_COMMIT_BRANCH" \
        -F "parentName=ServerPackCreator" \
        -F "parentVersion=branches" \
        -F "bom=@build/reports/bom.xml"
  retry: 1
  rules:
    - if: '$CI_SERVER_HOST == "git.griefed.de"'
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      variables:
        POLICY: pull-push
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
      variables:
        POLICY: pull
  artifacts:
    when: always
    name: "$CI_COMMIT_BRANCH-$CI_COMMIT_SHORT_SHA-$CI_JOB_NAME"
    expose_as: "Gradle-Test-Artifacts"
    paths:
      - serverpackcreator-api/build
      - serverpackcreator-app/build
      - serverpackcreator-web-frontend/dist
      - serverpackcreator-plugin-example/build
    expire_in: 1 week
    reports:
      junit:
        - serverpackcreator-api/build/test-results/test/TEST-*.xml
        - serverpackcreator-app/build/test-results/test/TEST-*.xml

Docker Test:
  stage: Tests
  before_script:
    - docker login -u "$DOCKERHUB_USER" -p "$DOCKERHUB_TOKEN" docker.io
    - docker login -u "$DOCKERHUB_USER" -p "$GITHUB_TOKEN" ghcr.io
    - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
    - docker buildx create --use --name grfdbuilder
  script:
    - docker buildx build --push --no-cache --platform linux/amd64
      --tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:$CI_COMMIT_SHORT_SHA"
      --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:$CI_COMMIT_SHORT_SHA"
      --file Dockerfile .
  retry: 1
  rules:
    - if: '$CI_SERVER_HOST == "git.griefed.de"' # Remove once GitLab no longer throws javax.net.ssl.SSLHandshakeException: Remote host terminated the handshake

Generate Release:
  stage: Release
  needs:
    - job: Build Test
      artifacts: false
#    - job: Docker Test
#      artifacts: false
  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"'

Build Release:
  stage: Build Release
  image: eclipse-temurin:21-jdk
  needs:
    - job: Build Test
      artifacts: false
      optional: false
    - job: release_job
      optional: true
      artifacts: false
  before_script:
    - echo "**** Running in $CI_JOB_ID ****"
    - echo "**** Java version ****"
    - java -version
    - echo "**** Allowing execution of gradlew ****"
    - chmod +x gradlew
  script:
    - echo "**** Building ServerPackCreator ****"
    - "./gradlew generateLicenseReport -Pversion=${CI_COMMIT_TAG} --info --full-stacktrace"
    - "./gradlew build -Pversion=${CI_COMMIT_TAG} --info --full-stacktrace -x :serverpackcreator-api:test -x :serverpackcreator-app:test"
    - "./gradlew cyclonedxBom -Pversion=${CI_COMMIT_TAG} --info --full-stacktrace"
    - echo "**** Listing build directory ****"
    - LC_COLLATE=C ls -ahl --group-directories-first --color=auto */build
    - LC_COLLATE=C ls -ahl --group-directories-first --color=auto */build/*
    - echo "**** Uploading packages ****"
    - 'curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" --upload-file serverpackcreator-app/build/libs/serverpackcreator-app-${CI_COMMIT_TAG}.jar "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${CI_PROJECT_NAME}/${CI_COMMIT_TAG}/${CI_PROJECT_NAME}-${CI_COMMIT_TAG}.jar"'
    - echo "**** Create asset links ****"
    - 'curl --request POST --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --data tag_name="${CI_COMMIT_TAG}" --data name="${CI_PROJECT_NAME}-${CI_COMMIT_TAG}.jar" --data url="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${CI_PROJECT_NAME}/${CI_COMMIT_TAG}/${CI_PROJECT_NAME}-${CI_COMMIT_TAG}.jar" --data link_type="package" "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/releases/${CI_COMMIT_TAG}/assets/links"'
    - echo "**** Uploading BOM ****"
    - |
      curl -X "POST" "$DEPENDENCYTRACK_URL" \
        -H 'Content-Type: multipart/form-data' \
        -H "X-Api-Key: $DEPENDENCYTRACK_TOKEN" \
        -F "autoCreate=true" \
        -F "projectName=ServerPackCreator" \
        -F "projectVersion=${CI_COMMIT_TAG%%.*}" \
        -F "bom=@build/reports/bom.xml"
    - |
      curl -X "POST" "$DEPENDENCYTRACK_URL" \
        -H 'Content-Type: multipart/form-data' \
        -H "X-Api-Key: $DEPENDENCYTRACK_TOKEN" \
        -F "autoCreate=true" \
        -F "projectName=ServerPackCreator" \
        -F "projectVersion=${CI_COMMIT_TAG}" \
        -F "parentName=ServerPackCreator" \
        -F "parentVersion=${CI_COMMIT_TAG%%.*}" \
        -F "bom=@build/reports/bom.xml"
  rules:
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+-(alpha|beta)\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'

Sign Java Publication:
  stage: Build Release
  image: eclipse-temurin:21-jdk
  needs:
    - job: Build Release
      artifacts: false
      optional: false
  before_script:
    - echo "**** Running in $CI_JOB_ID ****"
    - echo "**** Java version ****"
    - java -version
    - echo "**** Allowing execution of gradlew ****"
    - chmod +x gradlew
  script:
    - echo "**** Publishing Maven Artifacts ****"
    - "./gradlew dokkaJavadocJar :serverpackcreator-api:signMavenJavaPublication -Pversion=${CI_COMMIT_TAG} --info --stacktrace -x :serverpackcreator-api:test -x :serverpackcreator-app:test"
  rules:
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+-beta\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'

Publish GitHub:
  stage: Build Release
  image: eclipse-temurin:21-jdk
  needs:
    - job: Sign Java Publication
      optional: false
      artifacts: true
  script:
    - echo "**** Publishing Maven Artifacts to GitHub ****"
    - "./gradlew :serverpackcreator-api:publishMavenJavaPublicationToGitHubPackagesRepository -Pversion=${CI_COMMIT_TAG} --info --stacktrace -x :serverpackcreator-api:test -x :serverpackcreator-app:test"
  rules:
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+-beta\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'

Publish GitLab:
  stage: Build Release
  image: eclipse-temurin:21-jdk
  needs:
    - job: Sign Java Publication
      optional: false
      artifacts: true
  script:
    - echo "**** Publishing Maven Artifacts to GitLab ****"
    - "./gradlew :serverpackcreator-api:publishMavenJavaPublicationToGitLabRepository -Pversion=${CI_COMMIT_TAG} --info --stacktrace -x :serverpackcreator-api:test -x :serverpackcreator-app:test"
  rules:
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+-beta\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'

Publish GitGriefed:
  stage: Build Release
  image: eclipse-temurin:21-jdk
  needs:
    - job: Sign Java Publication
      optional: false
      artifacts: true
  script:
    - echo "**** Publishing Maven Artifacts GitGriefed ****"
    - "./gradlew :serverpackcreator-api:publishMavenJavaPublicationToGitGriefedRepository -Pversion=${CI_COMMIT_TAG} --info --stacktrace -x :serverpackcreator-api:test -x :serverpackcreator-app:test"
  rules:
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+-beta\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'

Publish OSSRH:
  stage: Build Release
  image: eclipse-temurin:21-jdk
  needs:
    - job: Sign Java Publication
      optional: false
      artifacts: true
  script:
    - echo "**** Publishing Maven Artifacts to OSSRH ****"
    - "./gradlew :serverpackcreator-api:publishToSonatype closeAndReleaseSonatypeStagingRepository -Pversion=${CI_COMMIT_TAG} --info --stacktrace -x :serverpackcreator-api:test -x :serverpackcreator-app:test"
  rules:
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+-beta\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'

Build Docker Release:
  stage: Build Release
  needs:
    - job: Build Test
      artifacts: true
      optional: false
  before_script:
    - docker login -u "$DOCKERHUB_USER" -p "$DOCKERHUB_TOKEN" docker.io
    - docker login -u "$DOCKERHUB_USER" -p "$GITHUB_TOKEN" ghcr.io
    - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
    - docker buildx create --use --name grfdbuilder
  script:
    - docker buildx build --push --no-cache --platform linux/amd64
      --tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:$CI_COMMIT_TAG"
      --tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:latest"
      --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:$CI_COMMIT_TAG"
      --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:latest"
      --build-arg VERSION=$CI_COMMIT_TAG
      --file Dockerfile .
  rules:
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'

Build Docker PreRelease:
  stage: Build Release
  needs:
    - job: Build Test
      artifacts: true
      optional: false
  before_script:
    - docker login -u "$DOCKERHUB_USER" -p "$DOCKERHUB_TOKEN" docker.io
    - docker login -u "$DOCKERHUB_USER" -p "$GITHUB_TOKEN" ghcr.io
    - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
    - docker buildx create --use --name grfdbuilder
  script:
    - docker buildx build --push --no-cache --platform linux/amd64
      --tag "ghcr.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:$CI_COMMIT_TAG"
      --tag "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:$CI_COMMIT_TAG"
      --build-arg VERSION=$CI_COMMIT_TAG
      --file Dockerfile .
  rules:
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+-(alpha|beta)\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'

Writerside Build:
  stage: Documentation
  image: registry.jetbrains.team/p/writerside/builder/writerside-builder:241.15989
  variables:
    ARTIFACT: webHelpSPCH2-all.zip
    INSTANCE: Writerside/spch
  script:
    - cp -f img/*.png          serverpackcreator-help/Writerside/topics/img/
    - cp -f img/*.webp         serverpackcreator-help/Writerside/topics/img/
    - cp -f img/*.gif          serverpackcreator-help/Writerside/topics/img/
    - cp -f CHANGELOG.md       serverpackcreator-help/Writerside/topics/CHANGELOG.md
    - cp -f CODE_OF_CONDUCT.md serverpackcreator-help/Writerside/topics/CODE_OF_CONDUCT.md
    - cp -f CONTRIBUTING.md    serverpackcreator-help/Writerside/topics/CONTRIBUTING.md
    - cp -f HELP.md            serverpackcreator-help/Writerside/topics/HELP.md
    - cp -f LICENSE            serverpackcreator-help/Writerside/topics/LICENSE.md
    - cp -f README.md          serverpackcreator-help/Writerside/topics/README.md
    - cp -f SECURITY.md        serverpackcreator-help/Writerside/topics/SECURITY.md
    - rm -f /tmp/.X99-lock || true
    - set -e
    - export DISPLAY=:99
    - Xvfb :99 &
    - /opt/builder/bin/idea.sh helpbuilderinspect -source-dir serverpackcreator-help -product $INSTANCE --runner gitlab -output-dir public/ || true
    - echo "Test existing of $ARTIFACT artifact"
    - test -e public/$ARTIFACT
  artifacts:
    paths:
      - public/$ARTIFACT
      - public/report.json
    expire_in: 1 week
  rules:
    - if: '$CI_SERVER_HOST == "git.griefed.de"'

Writerside Docker:
  stage: Documentation
  needs:
    - job: Writerside Build
      artifacts: true
      optional: false
  before_script:
    - docker login -u "$DOCKERHUB_USER" -p "$DOCKERHUB_TOKEN" docker.io
    - docker login -u "$DOCKERHUB_USER" -p "$GITHUB_TOKEN" ghcr.io
    - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
    - docker buildx create --use --name grfdbuilder
  script:
    - docker buildx build --push --no-cache --platform linux/amd64
      --tag "ghcr.io/$DOCKERHUB_USER/serverpackcreator-help:$CI_COMMIT_SHORT_SHA"
      --tag "index.docker.io/$DOCKERHUB_USER/serverpackcreator-help:$CI_COMMIT_SHORT_SHA"
      --build-arg VERSION=$CI_COMMIT_SHORT_SHA
      --file Dockerfile.help .
  rules:
    - if: '$CI_COMMIT_TAG !~ /^\d+\.\d+\.\d+-beta\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'
    - if: '$CI_COMMIT_TAG !~ /^\d+\.\d+\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'

Writerside Docker Latest:
  stage: Documentation
  needs:
    - job: Writerside Build
      artifacts: true
      optional: false
  before_script:
    - docker login -u "$DOCKERHUB_USER" -p "$DOCKERHUB_TOKEN" docker.io
    - docker login -u "$DOCKERHUB_USER" -p "$GITHUB_TOKEN" ghcr.io
    - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
    - docker buildx create --use --name grfdbuilder
  script:
    - docker buildx build --push --no-cache --platform linux/amd64
      --tag "ghcr.io/$DOCKERHUB_USER/serverpackcreator-help:$CI_COMMIT_TAG"
      --tag "ghcr.io/$DOCKERHUB_USER/serverpackcreator-help:latest"
      --tag "index.docker.io/$DOCKERHUB_USER/serverpackcreator-help:$CI_COMMIT_TAG"
      --tag "index.docker.io/$DOCKERHUB_USER/serverpackcreator-help:latest"
      --build-arg VERSION=$CI_COMMIT_TAG
      --file Dockerfile.help .
  rules:
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'

Writerside Docker Prerelease:
  stage: Documentation
  needs:
    - job: Writerside Build
      artifacts: true
      optional: false
  before_script:
    - docker login -u "$DOCKERHUB_USER" -p "$DOCKERHUB_TOKEN" docker.io
    - docker login -u "$DOCKERHUB_USER" -p "$GITHUB_TOKEN" ghcr.io
    - docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
    - docker buildx create --use --name grfdbuilder
  script:
    - docker buildx build --push --no-cache --platform linux/amd64
      --tag "ghcr.io/$DOCKERHUB_USER/serverpackcreator-help:$CI_COMMIT_TAG"
      --tag "index.docker.io/$DOCKERHUB_USER/serverpackcreator-help:$CI_COMMIT_TAG"
      --build-arg VERSION=$CI_COMMIT_TAG
      --file Dockerfile.help .
  rules:
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+-beta\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'

pages:
  stage: Documentation
  image: eclipse-temurin:21-jdk
  allow_failure: true
  before_script:
    - chmod +x gradlew
  script:
    - "./gradlew :serverpackcreator-api:dokkaHtml -Pversion='' --full-stacktrace --info -x test"
    - cp -Rf serverpackcreator-api/build/dokka public
    - LC_COLLATE=C ls -ahl --group-directories-first --color=auto public
  artifacts:
    paths:
      - public
    expire_in: 1 week
  rules:
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.\d+$/ && $CI_SERVER_HOST == "git.griefed.de"'

Update README:on-schedule:
  stage: Documentation
  only:
    - schedules
  before_script:
    - |-
      echo "#### Ensuring act-config ####"
      echo "-P ubuntu-latest=catthehacker/ubuntu:act-latest" > ~/.actrc
      echo "-P ubuntu-22.04=catthehacker/ubuntu:act-22.04" >> ~/.actrc
      echo "-P ubuntu-20.04=catthehacker/ubuntu:act-20.04" >> ~/.actrc
      echo "-P ubuntu-18.04=catthehacker/ubuntu:act-18.04" >> ~/.actrc
  script:
    - |-
      echo "#### Updating Sponsors and Blog Posts and Contribution Graph ####"
      act -v \
        -s GITHUB_TOKEN=$GITHUB_TOKEN \
        -s GIT_USER=$GIT_USER \
        -s GIT_MAIL=$GIT_MAIL \
        -s GITLAB_TOKEN=$GITLAB_TOKEN \
        -s CI_SERVER_HOST=$CI_SERVER_HOST \
        -s CI_PROJECT_TITLE=$CI_PROJECT_TITLE \
        -W '.github/workflows/update_readme.yml'
  after_script:
    - |-
      echo "Done."

release_job:
  stage: Release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  script:
    - echo "Running the release job to mirror release generation from parent repository."
  release:
    tag_name: $CI_COMMIT_TAG
    name: 'Release $CI_COMMIT_TAG'
    description: '## Changelogs for this release available at:<br><br>1. **[GitHub](https://github.com/Griefed/ServerPackCreator/releases/tag/$CI_COMMIT_TAG)**<br>2. **[GitGriefed](https://git.griefed.de/Griefed/ServerPackCreator/-/releases/$CI_COMMIT_TAG)**<br>3. **[CHANGELOG.md](https://gitlab.com/Griefed/ServerPackCreator/-/blob/$CI_COMMIT_TAG/CHANGELOG.md)**<br>'
  rules:
    - if: '$CI_COMMIT_TAG && $CI_SERVER_HOST == "gitlab.com"'