diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index be73b7688d05b7c6725a0fe8c604941158fb1845..8057fab4fca97805b696adcdfc27330701b03707 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -2,38 +2,148 @@ variables: DATA_CONUS_2018A: conus-2018a DATA_URL: ${S3_BUCKET}/nshmp_2018a_v1.1-CONUS-hazards-with-preliminary-PGV-fv0.3-1x1.nc IMAGE_NAME: ${CODE_REGISTRY_IMAGE}/${CI_PROJECT_NAME}:${ENVIRONMENT}-${CI_COMMIT_SHORT_SHA} + JACOCO_HTML_DIR: ${REPORTS_DIR}/jacoco/test/html + JUNIT_FILES: build/test-results/test/TEST-*.xml + REPORTS_DIR: build/reports # TODO: Remove hard coded url S3_BUCKET: https://nshmp-netcdf-lp-development-rbucket-1of3d1x45yfd9.s3-us-west-2.amazonaws.com +stages: + - build + - trigger + # Do not run for merge requests workflow: rules: - if: $CI_COMMIT_TAG - if: $CI_COMMIT_BRANCH -include: - - project: 'ghsc/nshmp/nshmp-pipeline-templates' - ref: 'main' - file: 'templates/library.yml' +#### +# Environment Templates +#### -stages: - - init - - build - - trigger +## +# 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 -.gradle: - image: ${DEVOPS_REGISTRY}usgs/java:11-jdk +## +# 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 @@ -56,98 +166,48 @@ Build Image 2018: Build Project: extends: - - .gradle - rules: - - - changes: - - 'src/**/java/**' - - 'src/**/resources/**' - - '**/*.gradle' - - '*gradle*' - when: on_success - - - allow_failure: true - when: manual + - .java script: - ./gradlew assemble - stage: build + +Markdown Lint: + extends: + - .java + script: + - ./gradlew markdownlint Unit Tests: artifacts: paths: - - ${REPORTS_DIR} + - ${JACOCO_HTML_DIR} reports: junit: ${JUNIT_FILES} coverage: '/Total.*?([0-9]{1,3})%/' extends: - - .gradle - needs: - - Init - rules: - - - changes: - - 'src/**/java/**' - - 'src/**/resources/**' - - '**/*.gradle' - - '*gradle*' - when: on_success - - - allow_failure: true - when: manual + - .java script: - ./gradlew check - cat ${JACOCO_HTML_DIR}/index.html - stage: build - -Markdown Lint: - extends: - - .gradle - needs: - - Init - rules: - - - changes: - - '**/*.md' - - '.markdownlint*' - when: on_success - - - allow_failure: true - when: manual - script: - - ./gradlew markdownlint; - stage: build YAML Lint: extends: - - .gradle - needs: - - Init - rules: - - - changes: - - '**/*.yml' - when: on_success - - - allow_failure: true - when: manual + - .java script: - - ./gradlew yamllint; - stage: build + - ./gradlew yamllint #### # Stage: trigger #### -Trigger nshmp-webapps: +Trigger AWS Deployment: needs: - Build Image 2018 rules: - - if: !reference [.development-env, if] - variables: !reference [.development-env, variables] + <<: *development-env when: manual - - !reference [.staging-env] - - !reference [.production-env] + - *staging-env + - *production-env script: - apk add curl - |