Skip to content
Snippets Groups Projects
.gitlab-ci.yml 5.26 KiB
Newer Older
image: ${DEVOPS_REGISTRY}usgs/centos:latest

stages:
  - test
  - integration
Jeremy M Fee's avatar
Jeremy M Fee committed
  - scan
  - deploy

variables:
  CI_REGISTRY: ${CODE_REGISTRY}
  CI_REGISTRY_IMAGE: ${CODE_REGISTRY_IMAGE}
  TRIVY_VERSION: "0.11.0"
Jeremy M Fee's avatar
Jeremy M Fee committed
  # docker variables
  DOCKER_DRIVER: overlay2
Jeremy M Fee's avatar
Jeremy M Fee committed
  FROM_IMAGE: ${CODE_REGISTRY}/devops/images/usgs/centos:latest
  # environment variables
  APP_NAME: geomag-algorithms
Jeremy M Fee's avatar
Jeremy M Fee committed
  DATA_HOST: "cwbpub.cr.usgs.gov"
  DATA_PORT: "2060"
  DATA_TYPE: "edge"

## --------------------------------------------------
# Templates
## --------------------------------------------------

.adjust_image_names:
  before_script:
    - IMAGE_NAME=usgs/${APP_NAME}:${CI_COMMIT_REF_SLUG}
    - IMAGE_NAME=${IMAGE_NAME/:master/:latest}
    - INTERNAL_IMAGE_NAME=${CI_REGISTRY_IMAGE}/${IMAGE_NAME}
    - STACK_NAME=${APP_NAME}

.build_docker_image:
  cache: {}
  extends:
    - .adjust_image_names
Jeremy M Fee's avatar
Jeremy M Fee committed
  image: docker:19.03-git
  only:
    - master@ghsc/geomag/geomag-algorithms
    - production@ghsc/geomag/geomag-algorithms
    - tags@ghsc/geomag/geomag-algorithms
  script:
    - docker build
      --pull
      --build-arg FROM_IMAGE=${FROM_IMAGE}
      --build-arg GIT_BRANCH_NAME=${CI_COMMIT_REF_NAME}
      --build-arg GIT_COMMIT_SHA=${CI_COMMIT_SHA}
      --tag local/${IMAGE_NAME} .

    - docker tag local/${IMAGE_NAME} ${IMAGE_NAME}
    - docker push ${IMAGE_NAME}
    - docker image rm ${IMAGE_NAME}

    - docker tag local/${IMAGE_NAME} ${INTERNAL_IMAGE_NAME}
    - docker push ${INTERNAL_IMAGE_NAME}
    - docker image rm ${INTERNAL_IMAGE_NAME}
Jeremy M Fee's avatar
Jeremy M Fee committed
  services:
    - docker:19.03-dind
  stage: integration
  tags:
    - build

.check_code:
  cache: {}
Jeremy M Fee's avatar
Jeremy M Fee committed
  image: ${DEVOPS_REGISTRY}usgs/conda:latest
    - export PYTHON_VERSION=${PYTHON_VERSION:-3.8}
    - scripts/ci_check_code.sh
  artifacts:
    paths:
      - cov.xml
    reports:
      junit: cov.xml
  stage: test
  tags:
    - development

.deploy:
  cache: {}
  extends:
    - .adjust_image_names
  image: ${CODE_REGISTRY}/ghsc/hazdev/cloud-formation/hazdev-build-runner:latest
  script:
    - git clone ${GENERIC_SWARM_DEPLOY_REPO} generic-deploy
    - cp -v
      generic-deploy/default.config.sh
      generic-deploy/default.funcs.sh
      generic-deploy/deploy.sh
      scripts/.
Jeremy M Fee's avatar
Jeremy M Fee committed

    - export APP_NAME=${APP_NAME}
    - export IMAGE_NAME=${IMAGE_NAME}
    - export REGISTRY=${CI_REGISTRY_IMAGE}
    - export STACK_NAME=${APP_NAME}
    - ./scripts/deploy.sh
  stage: deploy
  tags:
    - deploy
    - swarm

.mage:
  variables:
Jeremy M Fee's avatar
Jeremy M Fee committed
    DATA_HOST: "cwbpub.cr.usgs.gov"
    DATA_PORT: "2060"
    DATA_TYPE: "edge"
Jeremy M Fee's avatar
Jeremy M Fee committed
.staging:
  only:
    - master@ghsc/geomag/geomag-algorithms
    - tags@ghsc/geomag/geomag-algorithms
Jeremy M Fee's avatar
Jeremy M Fee committed

.production:
  except:
    - ^.*beta.*$
    - ^.*-rc.*$
  only:
    - tags@ghsc/geomag/geomag-algorithms
    - production@ghsc/geomag/geomag-algorithms
Jeremy M Fee's avatar
Jeremy M Fee committed
  when: manual
## --------------------------------------------------
# Test Stage
## --------------------------------------------------

Check Python 3.6:
  extends:
    - .check_code
  script:
    - export PYTHON_VERSION=3.6
    - scripts/ci_check_code.sh

Check Python 3.7:
  extends:
    - .check_code
  script:
    - export PYTHON_VERSION=3.7
    - scripts/ci_check_code.sh

Check Python 3.8:
  extends:
    - .check_code
  script:
    - export PYTHON_VERSION=3.8
    - scripts/ci_check_code.sh
## --------------------------------------------------
# Integration Stage
## --------------------------------------------------

Jeremy M Fee's avatar
Jeremy M Fee committed
Build Docker Image:
  extends:
    - .build_docker_image
  variables:
    APP_NAME: geomag-algorithms

Jeremy M Fee's avatar
Jeremy M Fee committed
## --------------------------------------------------
# Scanning Stage (e.g. OWASP ZAP etc...)
## --------------------------------------------------
Jeremy M Fee's avatar
Jeremy M Fee committed

Jeremy M Fee's avatar
Jeremy M Fee committed
Scan Docker Image:
  cache: {}
  extends:
    - .adjust_image_names
  image: docker:19.03-git
  only:
    - master@ghsc/geomag/geomag-algorithms
    - production@ghsc/geomag/geomag-algorithms
    - tags@ghsc/geomag/geomag-algorithms
  script:
    # install trivy
    - wget https://github.com/aquasecurity/trivy/releases/download/v${TRIVY_VERSION}/trivy_${TRIVY_VERSION}_Linux-64bit.tar.gz
    - tar zxvf trivy_${TRIVY_VERSION}_Linux-64bit.tar.gz
Jeremy M Fee's avatar
Jeremy M Fee committed
    # run trivy
Jeremy M Fee's avatar
Jeremy M Fee committed
    - ./trivy --exit-code 0 --no-progress ${INTERNAL_IMAGE}
Jeremy M Fee's avatar
Jeremy M Fee committed
  services:
    - docker:19.03-dind
  stage: scan
  tags:
    - build
## --------------------------------------------------
# Deploy Stage
## --------------------------------------------------

Deploy Mage01:
  extends:
    - .deploy
    - .staging
Jeremy M Fee's avatar
Jeremy M Fee committed
  tags:
    - deploy
    - swarm
    - mage01

Deploy Mage02:
  extends:
    - .deploy
Jeremy M Fee's avatar
Jeremy M Fee committed
  tags:
    - deploy
    - swarm
    - mage02
  variables:
    APP_NAME: geomag-algorithms

Deploy Algorithms Production01:
  extends:
    - .deploy
    - .production
  tags:
    - deploy
    - swarm
    - production01
  variables:
    APP_NAME: geomag-algorithms

Deploy Algorithms Production02:
  extends:
    - .deploy
    - .production
  tags:
    - deploy
    - swarm
    - production02
  variables:
    APP_NAME: geomag-algorithms

Deploy Algorithms Staging01:
  extends:
    - .deploy
    - .staging
  tags:
    - deploy
    - swarm
    - staging01
  variables:
    APP_NAME: geomag-algorithms

Deploy Algorithms Staging02:
  extends:
    - .deploy
    - .staging
  tags:
    - deploy
    - swarm
    - staging02
    APP_NAME: geomag-algorithms