variables:
  IMAGE_NAME: ${CODE_REGISTRY_IMAGE}/${CI_PROJECT_NAME}:${ENVIRONMENT}-${CI_COMMIT_SHORT_SHA}
  JACOCO_HTML_DIR: ${REPORTS_DIR}/jacoco/test/html
  JUNIT_FILES: src/lib/build/test-results/test/TEST-*.xml
  REPORTS_DIR: src/lib/build/reports

image: ${CI_REGISTRY}/devops/images/usgs/java:11

stages:
  - build
  - trigger

# Do not run for merge requests
workflow:
  rules:
    - if: $CI_COMMIT_TAG
    - if: $CI_COMMIT_BRANCH

default:
  tags:
    - nshmp

####
# Environment Templates
####

##
# Rule for development environment
##
.development-env: &development-env
  if: >
    $CI_PROJECT_PATH != $UPSTREAM_PATH
    || (
      $CI_PROJECT_PATH == $UPSTREAM_PATH
      && (
        $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
        && $CI_COMMIT_BRANCH != 'production'
        && $CI_COMMIT_TAG == null
      )
    )
  variables:
    ENVIRONMENT: development

##
# Rule for staging environment
##
.staging-env: &staging-env
  if: >
    $CI_PROJECT_PATH == $UPSTREAM_PATH
    && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  variables:
    ENVIRONMENT: staging

##
# Rule for production envrionment
##
.production-env: &production-env
  if: >
    $CI_PROJECT_PATH == $UPSTREAM_PATH
    && (
      $CI_COMMIT_BRANCH == 'production'
      || ( $CI_COMMIT_TAG && $CI_COMMIT_TAG != '' )
    )
  variables:
    ENVIRONMENT: production

####
# Docker Templates
####

##
# Docker in Docker
##
.dind:
  image: ${CI_REGISTRY}/devops/images/docker:20
  services:
    - alias: docker
      name: ${CI_REGISTRY}/devops/images/docker:20-dind
  variables:
    DOCKER_DRIVER: overlay2

##
# Build Docker image and push to registry.
#
# Pushes to internal registry for all branches and Docker registry
# on default upstream and production upstream branches.
##
.docker-build:
  extends:
    - .dind
  needs: []
  rules:
    - *development-env
    - *staging-env
    - *production-env
  script:
    - BUILD_ARGS='';
    - |
      for arg in ${DOCKER_BUILD_ARGS}; do
        BUILD_ARGS="${BUILD_ARGS} --build-arg ${arg}";
      done
    - |
      docker build \
        ${BUILD_ARGS} \
        --pull \
        --tag "${CODE_REGISTRY_IMAGE}/${CI_PROJECT_NAME}:${IMAGE_TAG}" \
        --file "${CI_PROJECT_DIR}/${DOCKERFILE}" \
        "${CI_PROJECT_DIR}/.";
    - docker push "${CODE_REGISTRY_IMAGE}/${CI_PROJECT_NAME}:${IMAGE_TAG}";
    - latest_image_name="${CODE_REGISTRY_IMAGE}/${CI_PROJECT_NAME}:${ENVIRONMENT}-latest";
    - docker tag "${CODE_REGISTRY_IMAGE}/${CI_PROJECT_NAME}:${IMAGE_TAG}" "${latest_image_name}";
    - docker push "${latest_image_name}";
    - if [[
          ${CI_COMMIT_REF_SLUG} == "${CI_DEFAULT_BRANCH}" ||
          ${CI_COMMIT_REF_SLUG} == "production" ||
          -n "${CI_COMMIT_TAG}"
      ]]; then
        docker tag "${latest_image_name}" "usgs/${CI_PROJECT_NAME}:${ENVIRONMENT}-latest";
        docker push "usgs/${CI_PROJECT_NAME}:${ENVIRONMENT}-latest";

        if [[
          ${CI_COMMIT_REF_SLUG} == "${CI_DEFAULT_BRANCH}" &&
          "${CI_PROJECT_PATH}" == "${UPSTREAM_PATH}"
        ]]; then
          docker tag "usgs/${CI_PROJECT_NAME}:${ENVIRONMENT}-latest" "usgs/${CI_PROJECT_NAME}:latest";
          docker push "usgs/${CI_PROJECT_NAME}:latest";
        fi
      fi
    - |
      printf "
        --------
        Image Name - %s:%s
        --------
      " "${CI_PROJECT_NAME}" "${IMAGE_TAG}";
  stage: build
  tags:
    - build
  variables:
    DOCKERFILE: Dockerfile
    IMAGE_TAG: ${ENVIRONMENT}-${CI_COMMIT_SHORT_SHA}

####
# Java Templates
####


####
# Stage: build
####

Build Image:
  extends:
    - .docker-build
  variables:
    DOCKER_BUILD_ARGS: |
      BUILD_IMAGE=${CI_REGISTRY}/devops/images/usgs/java:11
      FROM_IMAGE=${CI_REGISTRY}/devops/images/usgs/java:11
      CI_COMMIT_BRANCH=${CI_COMMIT_BRANCH}
      CI_JOB_TOKEN=${CI_JOB_TOKEN}
      CI_PROJECT_URL=${CI_PROJECT_URL}
    UPSTREAM_PATH: ghsc/nshmp/nshmp-ws-static

Build Project:
  script:
    - ./gradlew assemble
  stage: build

Markdown Lint:
  script:
    - ./gradlew markdownlint
  stage: build

Unit Tests:
  # artifacts:
  #   paths:
  #     - ${JACOCO_HTML_DIR}
  #   reports:
  #     junit: ${JUNIT_FILES}
  # coverage: '/Total.*?([0-9]{1,3})%/'
  script:
    - ./gradlew check
    # - cat ${JACOCO_HTML_DIR}/index.html
  stage: build

YAML Lint:
  script:
    - ./gradlew yamllint
  stage: build

####
# Stage: trigger
####

Trigger AWS Deployment:
  needs:
    - Build Image
  rules:
    -
      <<: *development-env
      when: manual
    - *staging-env
    - *production-env
  script:
    - |
      if [ "${ENVIRONMENT}" == 'production' ]; then
        REF="production";
      fi
    - |
     curl --request POST \
        --form token=${NSHMP_CDK_TRIGGER_TOKEN} \
        --form ref=${REF} \
        --form "variables[description]=Triggered by nshmp-ws-static" \
        --form "variables[ENVIRONMENT]=${ENVIRONMENT}" \
        --form "variables[NSHMP_WS_STATIC_IMAGE]=${IMAGE_NAME}" \
        --form "variables[SOURCE_PROJECT_NAME]=${CI_PROJECT_NAME}" \
        "https://${PRIVATE_GITLAB}/api/v4/projects/${NSHMP_CDK_PROJECT_ID}/trigger/pipeline"
  stage: trigger
  variables:
    REF: main
    UPSTREAM_PATH: ghsc/nshmp/nshmp-ws-static
