Skip to content
Snippets Groups Projects
.gitlab-ci.yml 5.05 KiB
Newer Older
variables:
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
  IMAGE_NAME: ${CODE_REGISTRY_IMAGE}/${CI_PROJECT_NAME}:${ENVIRONMENT}-${CI_COMMIT_SHORT_SHA}
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
  JACOCO_HTML_DIR: ${REPORTS_DIR}/jacoco/test/html
  JUNIT_FILES: src/lib/build/test-results/test/TEST-*.xml
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
  REPORTS_DIR: src/lib/build/reports
image: ${CI_REGISTRY}/devops/images/usgs/java:11

Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
stages:
  - build
  - trigger

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

default:
  tags:
    - nshmp

Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
####
# Environment Templates
####
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed

Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
##
# 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
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
  services:
    - alias: docker
      name: ${CI_REGISTRY}/devops/images/docker:20-dind
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
  variables:
    DOCKER_DRIVER: overlay2
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
##
# 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
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
          docker tag "usgs/${CI_PROJECT_NAME}:${ENVIRONMENT}-latest" "usgs/${CI_PROJECT_NAME}:latest";
          docker push "usgs/${CI_PROJECT_NAME}:latest";
        fi
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
      fi
    - |
      printf "
        --------
        Image Name - %s:%s
        --------
      " "${CI_PROJECT_NAME}" "${IMAGE_TAG}";
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
  stage: build
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
  tags:
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
    - build
  variables:
    DOCKERFILE: Dockerfile
    IMAGE_TAG: ${ENVIRONMENT}-${CI_COMMIT_SHORT_SHA}
####
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
# Java Templates
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
# Stage: build
Build Image:
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
  extends:
    - .docker-build
  variables:
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
    DOCKER_BUILD_ARGS: |
      BUILD_IMAGE=${CI_REGISTRY}/devops/images/usgs/java:11
      FROM_IMAGE=${CI_REGISTRY}/devops/images/usgs/java:11
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
      CI_COMMIT_BRANCH=${CI_COMMIT_BRANCH}
      CI_JOB_TOKEN=${CI_JOB_TOKEN}
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
      CI_PROJECT_URL=${CI_PROJECT_URL}
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
    UPSTREAM_PATH: ghsc/nshmp/nshmp-ws-static
  script:
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
    - ./gradlew assemble
  stage: build
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed

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:
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
    - ./gradlew check
    # - cat ${JACOCO_HTML_DIR}/index.html
  stage: build
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
YAML Lint:
  script:
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
    - ./gradlew yamllint
  stage: build
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
####
# Stage: trigger
####

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