image: openjdk:11-jdk

# Disable the Gradle daemon for Continuous Integration servers as correctness
# is usually a priority over speed in CI environments. Using a fresh
# runtime for each build is more reliable since the runtime is completely
# isolated from any previous builds.
variables:
  GRADLE_OPTS: "-Dorg.gradle.daemon=false"

cache:
  paths:
    - .gradle

before_script:
  - export GRADLE_USER_HOME=`pwd`/.gradle

stages:
  - build
  - test
  - check
  - deploy

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

test:
  stage: test
  tags:
    - exec-docker
  script: ./gradlew test --continue
  artifacts:
    reports:
      junit:
        - "**/build/test-results/test/TEST-*.xml"

checkstyle:
  stage: check
  tags:
    - exec-docker
  script: ./gradlew checkstyle --continue
  allow_failure: true
  artifacts:
    paths:
      - "*/build/reports/checkstyle/main.html"
    when: on_failure
    expire_in: 1 day

pmd:
  stage: check
  tags:
    - exec-docker
  script: ./gradlew pmd --continue
  allow_failure: true
  artifacts:
    paths:
      - "*/build/reports/pmd/*.html"
    when: on_failure
    expire_in: 1 day

spotbugs:
  stage: check
  tags:
    - exec-docker
  script: ./gradlew spotbugs --continue
  allow_failure: true
  artifacts:
    paths:
      - "*/build/reports/spotbugs/*.html"
    when: on_failure
    expire_in: 1 day


.deploy:
  stage: deploy
  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"
  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 $DOCKERHUB_ORG/$IMAGE_NAME:${DOCKER_TAG_NAME}latest"
    - "[ ! $CI_COMMIT_TAG ] && docker tag $IMAGE_NAME $DOCKERHUB_ORG/$IMAGE_NAME:$DOCKER_TAG_NAME$CI_COMMIT_SHORT_SHA"
    - "[ $CI_COMMIT_TAG ] && docker tag $IMAGE_NAME $DOCKERHUB_ORG/$IMAGE_NAME:CI_COMMIT_TAG"
    - echo $DOCKERHUB_PW | docker login -u $DOCKERHUB_ID --password-stdin
    - docker push $DOCKERHUB_ORG/$IMAGE_NAME
    - docker logout
  rules:
    - if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME && $CI_COMMIT_TAG"
      when: always
    - changes:
      # - $JAVA_PROJECT_NAME/**/* # hope this can be simplified soon, see #51
      - application-kafkastreams-commons/**/*
      if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: always
    - if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: manual
      allow_failure: true

deploy-uc1-kstreams-app:
  extends: .deploy
  variables:
    IMAGE_NAME: "theodolite-uc1-kstreams-app"
    JAVA_PROJECT_NAME: "uc1-application"
  rules: # hope this can be simplified soon, see #51
    - if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME && $CI_COMMIT_TAG"
      when: always
    - changes:
      - uc1-application/**/*
      - application-kafkastreams-commons/**/*
      if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: always
    - if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: manual
      allow_failure: true

deploy-uc2-kstreams-app:
  extends: .deploy
  variables:
    IMAGE_NAME: "theodolite-uc2-kstreams-app"
    JAVA_PROJECT_NAME: "uc2-application"
  rules: # hope this can be simplified soon, see #51
    - if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME && $CI_COMMIT_TAG"
      when: always
    - changes:
      - uc2-application/**/*
      - application-kafkastreams-commons/**/*
      if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: always
    - if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: manual
      allow_failure: true

deploy-uc3-kstreams-app:
  extends: .deploy
  variables:
    IMAGE_NAME: "theodolite-uc3-kstreams-app"
    JAVA_PROJECT_NAME: "uc3-application"
  rules: # hope this can be simplified soon, see #51
    - if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME && $CI_COMMIT_TAG"
      when: always
    - changes:
      - uc3-application/**/*
      - application-kafkastreams-commons/**/*
      if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: always
    - if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: manual
      allow_failure: true

deploy-uc4-kstreams-app:
  extends: .deploy
  variables:
    IMAGE_NAME: "theodolite-uc4-kstreams-app"
    JAVA_PROJECT_NAME: "uc4-application"
  rules: # hope this can be simplified soon, see #51
    - if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME && $CI_COMMIT_TAG"
      when: always
    - changes:
      - uc4-application/**/*
      - application-kafkastreams-commons/**/*
      if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: always
    - if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: manual
      allow_failure: true

deploy-uc1-workload-generator:
  extends: .deploy
  variables:
    IMAGE_NAME: "theodolite-uc1-workload-generator"
    JAVA_PROJECT_NAME: "uc1-workload-generator"
  rules: # hope this can be simplified soon, see #51
    - if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME && $CI_COMMIT_TAG"
      when: always
    - changes:
      - uc1-workload-generator/**/*
      - application-kafkastreams-commons/**/*
      if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: always
    - if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: manual
      allow_failure: true

deploy-uc2-workload-generator:
  extends: .deploy
  variables:
    IMAGE_NAME: "theodolite-uc2-workload-generator"
    JAVA_PROJECT_NAME: "uc2-workload-generator"
  rules: # hope this can be simplified soon, see #51
    - if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME && $CI_COMMIT_TAG"
      when: always
    - changes:
      - uc2-workload-generator/**/*
      - application-kafkastreams-commons/**/*
      if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: always
    - if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: manual
      allow_failure: true

deploy-uc3-workload-generator:
  extends: .deploy
  variables:
    IMAGE_NAME: "theodolite-uc3-workload-generator"
    JAVA_PROJECT_NAME: "uc3-workload-generator"
  rules: # hope this can be simplified soon, see #51
    - if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME && $CI_COMMIT_TAG"
      when: always
    - changes:
      - uc3-workload-generator/**/*
      - application-kafkastreams-commons/**/*
      if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: always
    - if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: manual
      allow_failure: true

deploy-uc4-workload-generator:
  extends: .deploy
  variables:
    IMAGE_NAME: "theodolite-uc4-workload-generator"
    JAVA_PROJECT_NAME: "uc4-workload-generator"
  rules: # hope this can be simplified soon, see #51
    - if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME && $CI_COMMIT_TAG"
      when: always
    - changes:
      - uc4-workload-generator/**/*
      - application-kafkastreams-commons/**/*
      if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: always
    - if: "$DOCKERHUB_ORG && $DOCKERHUB_ID && $DOCKERHUB_PW && $IMAGE_NAME && $JAVA_PROJECT_NAME"
      when: manual
      allow_failure: true
