stages:
  - build
  - test
  - check
  - deploy

.dind:
  tags:
    - exec-dind
  # see https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#tls-enabled
  # for image usage and settings for building with TLS and docker in docker
  image: docker:19.03.1
  services:
    - docker:19.03.1-dind
  variables:
    DOCKER_TLS_CERTDIR: "/certs"


# Theodolite Benchmarks

.benchmarks:
  image: openjdk:11-jdk
  tags:
    - exec-docker
  variables:
    GRADLE_OPTS: "-Dorg.gradle.daemon=false"
  cache:
    paths:
      - .gradle
  before_script:
    - cd benchmarks
    - export GRADLE_USER_HOME=`pwd`/.gradle

build-benchmarks:
  stage: build
  extends: .benchmarks
  script: ./gradlew --build-cache assemble
  artifacts:
    paths:
      - "benchmarks/build/libs/*.jar"
      - "benchmarks/*/build/distributions/*.tar"
    expire_in: 1 day

test-benchmarks:
  stage: test
  extends: .benchmarks
  needs:
    - build-benchmarks
  script: ./gradlew test --continue
  artifacts:
    reports:
      junit:
        - "benchmarks/**/build/test-results/test/TEST-*.xml"

checkstyle-benchmarks:
  stage: check
  extends: .benchmarks
  needs:
    - build-benchmarks
    - test-benchmarks
  script: ./gradlew checkstyle --continue
  artifacts:
    paths:
      - "benchmarks/*/build/reports/checkstyle/main.html"
    when: on_failure
    expire_in: 1 day

pmd-benchmarks:
  stage: check
  extends: .benchmarks
  needs:
    - build-benchmarks
    - test-benchmarks
  script: ./gradlew pmd --continue
  artifacts:
    paths:
      - "benchmarks/*/build/reports/pmd/*.html"
    when: on_failure
    expire_in: 1 day

spotbugs-benchmarks:
  stage: check
  extends: .benchmarks
  needs:
    - build-benchmarks
    - test-benchmarks
  script: ./gradlew spotbugs --continue
  artifacts:
    paths:
      - "benchmarks/*/build/reports/spotbugs/*.html"
    when: on_failure
    expire_in: 1 day

.deploy-benchmarks:
  stage: deploy
  extends:
    - .benchmarks
    - .dind
  needs:
    - build-benchmarks
    - checkstyle-benchmarks
    - pmd-benchmarks
    - spotbugs-benchmarks
  script:
    - DOCKER_TAG_NAME=$(echo $CI_COMMIT_REF_SLUG- | sed 's/^master-$//')
    - docker build --pull -t $IMAGE_NAME ./$JAVA_PROJECT_NAME
    - "[ ! $CI_COMMIT_TAG ] && docker tag $IMAGE_NAME $CR_HOST/$CR_ORG/$IMAGE_NAME:${DOCKER_TAG_NAME}latest"
    - "[ ! $CI_COMMIT_TAG ] && docker tag $IMAGE_NAME $CR_HOST/$CR_ORG/$IMAGE_NAME:$DOCKER_TAG_NAME$CI_COMMIT_SHORT_SHA"
    - "[ $CI_COMMIT_TAG ] && docker tag $IMAGE_NAME $CR_HOST/$CR_ORG/$IMAGE_NAME:$CI_COMMIT_TAG"
    - echo $CR_PW | docker login $CR_HOST -u $CR_USER --password-stdin
    - docker push $CR_HOST/$CR_ORG/$IMAGE_NAME
    - docker logout
  rules:
    - if: "$CR_HOST && $CR_ORG && $CR_USER && $CR_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME && $CI_COMMIT_TAG"
      when: always
    - changes:
      - benchmarks/*
      - benchmarks/$JAVA_PROJECT_NAME/**/*
      - benchmarks/application-kafkastreams-commons/**/*
      - benchmarks/workload-generator-commons/**/*
      if: "$CR_HOST && $CR_ORG && $CR_USER && $CR_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: always
    - if: "$CR_HOST && $CR_ORG && $CR_USER && $CR_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: manual
      allow_failure: true

deploy-uc1-kstreams-app:
  extends: .deploy-benchmarks
  variables:
    IMAGE_NAME: "theodolite-uc1-kstreams-app"
    JAVA_PROJECT_NAME: "uc1-application"

deploy-uc2-kstreams-app:
  extends: .deploy-benchmarks
  variables:
    IMAGE_NAME: "theodolite-uc2-kstreams-app"
    JAVA_PROJECT_NAME: "uc2-application"

deploy-uc3-kstreams-app:
  extends: .deploy-benchmarks
  variables:
    IMAGE_NAME: "theodolite-uc3-kstreams-app"
    JAVA_PROJECT_NAME: "uc3-application"

deploy-uc4-kstreams-app:
  extends: .deploy-benchmarks
  variables:
    IMAGE_NAME: "theodolite-uc4-kstreams-app"
    JAVA_PROJECT_NAME: "uc4-application"

deploy-uc1-load-generator:
  extends: .deploy-benchmarks
  variables:
    IMAGE_NAME: "theodolite-uc1-workload-generator"
    JAVA_PROJECT_NAME: "uc1-workload-generator"

deploy-uc2-load-generator:
  extends: .deploy-benchmarks
  variables:
    IMAGE_NAME: "theodolite-uc2-workload-generator"
    JAVA_PROJECT_NAME: "uc2-workload-generator"

deploy-uc3-load-generator:
  extends: .deploy-benchmarks
  variables:
    IMAGE_NAME: "theodolite-uc3-workload-generator"
    JAVA_PROJECT_NAME: "uc3-workload-generator"

deploy-uc4-load-generator:
  extends: .deploy-benchmarks
  variables:
    IMAGE_NAME: "theodolite-uc4-workload-generator"
    JAVA_PROJECT_NAME: "uc4-workload-generator"
      

# Theodolite Framework

.theodolite:
  image: openjdk:11-jdk
  tags:
    - exec-docker
  variables:
    GRADLE_OPTS: "-Dorg.gradle.daemon=false"
  cache:
    paths:
      - .gradle/wrapper
      - .gradle/caches
  before_script:
    - cd theodolite-quarkus
    - export GRADLE_USER_HOME=`pwd`/.gradle

build-theodolite:
  stage: build
  extends: .theodolite
  # script: ./gradlew --build-cache assemble -Dquarkus.package.type=native
  script: ./gradlew --build-cache assemble
  artifacts:
    paths:
      - "theodolite-quarkus/build/lib/*"
      - "theodolite-quarkus/build/*-runner.jar"
      # - "theodolite-quarkus/build/*-runner" # For native image
    expire_in: 1 day

test-theodolite:
  stage: test
  extends: .theodolite
  needs:
    - build-theodolite
  script: ./gradlew test --stacktrace

# Disabled for now
.ktlint-theodolite:
  stage: check
  extends: .theodolite
  needs:
    - build-theodolite
    - test-theodolite
  script: ./gradlew ktlintCheck --continue

# Disabled for now
.detekt-theodolite: 
  stage: check
  extends: .theodolite
  needs:
    - build-theodolite
    - test-theodolite
  script: ./gradlew detekt --continue

deploy-theodolite:
  stage: deploy
  extends:
    - .theodolite
    - .dind
  needs:
    - build-theodolite
    - test-theodolite
  script:
    - DOCKER_TAG_NAME=$(echo $CI_COMMIT_REF_SLUG- | sed 's/^master-$//')
    #- docker build -f src/main/docker/Dockerfile.native -t theodolite .
    - docker build -f src/main/docker/Dockerfile.jvm -t theodolite .
    - "[ ! $CI_COMMIT_TAG ] && docker tag theodolite $CR_HOST/$CR_ORG/theodolite:${DOCKER_TAG_NAME}latest"
    - "[ ! $CI_COMMIT_TAG ] && docker tag theodolite $CR_HOST/$CR_ORG/theodolite:$DOCKER_TAG_NAME$CI_COMMIT_SHORT_SHA"
    - "[ $CI_COMMIT_TAG ] && docker tag theodolite $CR_HOST/$CR_ORG/theodolite:$CI_COMMIT_TAG"
    - echo $CR_PW | docker login $CR_HOST -u $CR_USER --password-stdin
    - docker push $CR_HOST/$CR_ORG/theodolite
    - docker logout
  rules:
    - if: "$CR_HOST && $CR_ORG && $CR_USER && $CR_PW && $CI_COMMIT_TAG"
      when: always
    - changes:
      - theodolite-quarkus/**/*
      if: "$CR_HOST && $CR_ORG && $CR_USER && $CR_PW"
      when: always
    - if: "$CR_HOST && $CR_ORG && $CR_USER && $CR_PW"
      when: manual
      allow_failure: true
