diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 53599a177bfdc3017c26554c55fe4fbb20f36c0f..ac7cda7171bf680697637ca20f651c386ea0c30c 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,5 +1,8 @@ variables: + JACOCO_HTML_DIR: ${REPORTS_DIR}/jacoco/test/html + JUNIT_FILES: build/test-results/test/TEST-*.xml NSHMP_HAZ_WS_IMAGE: ${CODE_REGISTRY_IMAGE}/nshmp-haz-ws:${ENVIRONMENT}-${CI_COMMIT_SHORT_SHA} + REPORTS_DIR: build/reports # Do not run for merge requests workflow: @@ -7,33 +10,136 @@ workflow: - if: $CI_COMMIT_TAG - if: $CI_COMMIT_BRANCH -include: - - project: 'ghsc/nshmp/nshmp-pipeline-templates' - ref: 'main' - file: 'templates/library.yml' - stages: - - init - build - trigger #### -# Templates +# Environment Templates #### -.gradle: - image: ${DEVOPS_REGISTRY}usgs/amazoncorretto:11 +## +# 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: ${DEVOPS_REGISTRY}docker:19.03-git + services: + - alias: docker + name: ${DEVOPS_REGISTRY}docker:19.03-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"; + fi + - | + printf " + -------- + Image Name - %s:%s + -------- + " "${CI_PROJECT_NAME}" "${IMAGE_TAG}"; stage: build tags: - - development + - build + variables: + DOCKERFILE: Dockerfile + DOCKER_BUILD_ARGS: | + BUILD_IMAGE=${DEVOPS_REGISTRY}usgs/centos:latest + FROM_IMAGE=${DEVOPS_REGISTRY}usgs/centos:latest + IMAGE_TAG: ${ENVIRONMENT}-${CI_COMMIT_SHORT_SHA} #### -# Stage: init +# Java Templates #### -Init: - extends: - - .gradle-init +## +# General Java setup +## +.java: + image: ${DEVOPS_REGISTRY}usgs/amazoncorretto:11 + stage: build + tags: + - development #### # Stage: build @@ -64,36 +170,15 @@ Build Image WS: Build Project: extends: - - .gradle - needs: - - Init - rules: - - - changes: - - 'src/**' - - '*gradle*' - when: on_success - - - allow_failure: true - when: manual + - .java script: - ./gradlew assemble Markdown Lint: extends: - - .gradle - needs: - - Init - rules: - - - changes: - - '**/*.md' - when: on_success - - - allow_failure: true - when: manual + - .java script: - - ./gradlew markdownlint; + - ./gradlew markdownlint Unit Tests: artifacts: @@ -103,18 +188,7 @@ Unit Tests: junit: ${JUNIT_FILES} coverage: '/Total.*?([0-9]{1,3})%/' extends: - - .gradle - needs: - - Init - rules: - - - changes: - - 'src/**/*' - - '*gradle*' - when: on_success - - - allow_failure: true - when: manual + - .java script: - ./gradlew check - cat ${JACOCO_HTML_DIR}/index.html @@ -128,12 +202,10 @@ Trigger nshmp-haz-ws CDK: - Build Image WS rules: - - if: !reference [.development-env, if] - variables: !reference [.development-env, variables] + <<: *development-env when: manual - allow_failure: true - - !reference [.staging-env] - - !reference [.production-env] + - *staging-env + - *production-env script: - apk add curl - |