Skip to content
Snippets Groups Projects
.gitlab-ci.yml 3.1 KiB
Newer Older
variables:
  OUTPUT_DIR: target
  WAR_FILE: $OUTPUT_DIR/arsnova-backend-*.war

stages:
  - build
  - post-build
  - deploy

default:
  image: maven:3-jdk-11-slim

.maven_cache: &maven_cache
  paths:
    - .m2/

checkstyle:
  stage: build
  cache: *maven_cache
  script:
    - mvn -B checkstyle:check -Dcheckstyle.missing-javadoc.severity=info

checkstyle_javadoc:
  stage: build
  cache: *maven_cache
  allow_failure: true
  script:
    - mvn -B checkstyle:check -Dcheckstyle.missing-javadoc.severity=warning
compile:
  stage: build
  artifacts:
    paths:
      - $OUTPUT_DIR
  cache: *maven_cache
  script:
    - mvn -B test-compile
unit_test:
  stage: post-build
  dependencies:
    - compile
  artifacts:
    paths:
      - $OUTPUT_DIR
  cache: *maven_cache
  coverage: '/Code coverage: \d+\.\d+/'
  script:
    - mvn -B jacoco:prepare-agent surefire:test jacoco:report
    - awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print "Instructions covered:", covered, "/", instructions; print "Code coverage:", 100 * covered / instructions "%" }' "$OUTPUT_DIR/site/jacoco/jacoco.csv"

package:
  stage: post-build
  dependencies:
    - compile
  artifacts:
    name: package
    paths:
      - $WAR_FILE
  cache: *maven_cache
  script:
    - mvn -B war:war
.deploy: &deploy
  stage: deploy
  image: python:3
  when: manual
  variables:
    DEPLOY_CONTEXT: api
    GIT_STRATEGY: none
  dependencies:
    - package
  script:
    # Do some variable magic to access host-specific variables
    - PREFIX=$(echo $CI_ENVIRONMENT_NAME | tr '.:/-' '_')
    - HOST_VAR=${PREFIX}__HOST TOMCAT_PASSWORD_VAR=${PREFIX}__TOMCAT_PASSWORD TOMCAT_USER_VAR=${PREFIX}__TOMCAT_USER
    - "[ -z \"${!HOST_VAR}\" ] && echo \"No configuration for $DEPLOY_HOST found.\" && exit 1"
    # Abort if there are too many users online
    - USER_COUNT=$(curl -fsSL ${CI_ENVIRONMENT_URL}statistics | python -c "import sys, json; data=json.loads(sys.stdin.read()); print(data['activeUsers']);")
    - "[ \"$USER_COUNT\" -ge 10 ] && [ -z \"$FORCE\" ] && echo \"Too many users ($USER_COUNT) online.\" && exit 1"
    # Deploy .war file to Tomcat
    - curl -fsS --upload-file $WAR_FILE "https://${!TOMCAT_USER_VAR}:${!TOMCAT_PASSWORD_VAR}@${!HOST_VAR}/manager/text/deploy?path=%2F${DEPLOY_CONTEXT}&update=true"

tomcat_production:
  <<: *deploy
  environment:
    name: production/$PROD_DEPLOY_HOST
    url: https://$PROD_DEPLOY_HOST/$DEPLOY_CONTEXT/
  only:
    variables:
      - $PROD_DEPLOY_HOST
      # GitLab 11.0+
      #- $PROD_DEPLOY_HOST =~ /^([a-z0-9-]+\.)*[a-z0-9-]+(:[0-9]+)?$/
    refs:
      - /^v[0-9]+/
      - /^[0-9]+\.[0-9]+$/
  before_script:
    - DEPLOY_HOST=$PROD_DEPLOY_HOST

tomcat_development:
  <<: *deploy
  environment:
    name: development/$DEV_DEPLOY_HOST
    url: https://$DEV_DEPLOY_HOST/api/
  only:
    variables:
      - $DEV_DEPLOY_HOST
      # GitLab 11.0+
      #- $DEV_DEPLOY_HOST =~ /^([a-z0-9-]+\.)*[a-z0-9-]+(:[0-9]+)?$/
  before_script:
    - DEPLOY_HOST=$DEV_DEPLOY_HOST

sonarqube:
  stage: deploy
  only:
    - master
  dependencies:
    - compile
    - unit_test
  cache: *maven_cache
  allow_failure: true
  script:
    - mvn -B sonar:sonar