image: jhipster/jhipster:v7.6.0

cache:
  key: '$CI_COMMIT_REF_NAME'
  paths:
    - .maven/
stages:
  - lint
  - build
  - test
  - analyze
  - package
  - release
  - deploy

before_script:
  - export NG_CLI_ANALYTICS="false"
  - export MAVEN_USER_HOME=`pwd`/.maven

pmd:
  stage: lint
  cache: {}
  allow_failure: true
  when: always
  before_script:
    - 'export VERSION="6.40.0"'
    - 'wget "https://github.com/pmd/pmd/releases/download/pmd_releases%2F$VERSION/pmd-bin-$VERSION.zip"'
    - 'jar -xf "pmd-bin-$VERSION.zip"'
  script:
    - '/bin/sh "pmd-bin-$VERSION/bin/run.sh" pmd -d src/main/java -f textcolor -R pmd_rules.xml 2>&1 | tee pmd.log'
  needs: []
  artifacts:
    when: on_failure
    paths:
      - pmd.log

checkstyle:
  stage: lint
  script:
    - ./mvnw -ntp checkstyle:check -Dmaven.repo.local=$MAVEN_USER_HOME
  needs: []

prettier:
  stage: lint
  cache: {}
  script:
    - npm install
    - npm run prettier:check
  allow_failure: true
  needs: []

eslint:
  stage: lint
  cache: {}
  script:
    - npm install
    # eslint exits 0 on warnings, this is a workaround to exit 1 when warnings are present.
    - 'npm run lint | tee lint.log'
    - "! grep -qE '✖ [0-9]+ problem' lint.log"
  allow_failure: true
  needs: []

maven-compile:
  stage: build
  script:
    - ./mvnw -ntp compile -P-webapp -Dmaven.repo.local=$MAVEN_USER_HOME
  artifacts:
    paths:
      - target/classes/
      - target/generated-sources/
    expire_in: 1 day
  needs: []

maven-test:
  # DinD service is required for Testcontainers
  services:
    - docker:20-dind
  variables:
    # Instruct Testcontainers to use the daemon of DinD.
    DOCKER_HOST: 'tcp://docker:2375'
    DOCKER_TLS_CERTDIR: ''
  stage: test
  script:
    - ./mvnw -ntp test -P-webapp -Dmaven.repo.local=$MAVEN_USER_HOME -Dspring.profiles.active=testcontainers || FAILED=true
    - ./mvnw -ntp integration-test -P-webapp -Dmaven.repo.local=$MAVEN_USER_HOME -Dspring.profiles.active=testcontainers || FAILED=true
  artifacts:
    reports:
      junit:
        - target/surefire-reports/TEST-*.xml
        - target/failsafe-reports/TEST-*.xml
    paths:
      - target/surefire-reports
      - target/failsafe-reports
      - target/site
    when: always
    expire_in: 30 day
  allow_failure: true
  needs: []

frontend-test:
  stage: test
  cache: {}
  script:
    - npm install
    - npm test
  artifacts:
    reports:
      junit: target/test-results/TESTS-results-jest.xml
    paths:
      - target/test-results
      - target/jacoco
    when: always
    expire_in: 1 day
  allow_failure: true
  needs: []

sonar-analyze:
  stage: analyze
  dependencies:
    - maven-test
    - frontend-test
  script:
    - ./mvnw -ntp org.jacoco:jacoco-maven-plugin:prepare-agent initialize sonar:sonar -Dsonar.organization=codeAbility -Dsonar.host.url=https://qe-sonarqube.uibk.ac.at/ -Dsonar.login=${SONAR_TOKEN_QE} -Dmaven.repo.local=$MAVEN_USER_HOME
    - ./mvnw -ntp org.jacoco:jacoco-maven-plugin:prepare-agent initialize sonar:sonar -Dsonar.organization=codeAbility -Dsonar.host.url=https://server.arctis.at/sonar/ -Dsonar.login=${SONAR_TOKEN} -Dmaven.repo.local=$MAVEN_USER_HOME
  allow_failure: true

maven-package:
  stage: package
  script:
    - ./mvnw -ntp verify -Pprod -DskipTests -Dmaven.repo.local=$MAVEN_USER_HOME
  artifacts:
    paths:
      - target/*.jar
      - target/classes
    expire_in: 1 day
  # Uncomment the following line to use gitlabs container registry. You need to adapt the REGISTRY_URL in case you are not using gitlab.com
  #docker-push:
  #    stage: release
  #    variables:
  #        REGISTRY_URL: registry.gitlab.com
  #        IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHA
  #    dependencies:
  #        - maven-package
  #    script:
  #        - ./mvnw -ntp jib:build -Pprod -Djib.to.image=$IMAGE_TAG -Djib.to.auth.username=gitlab-ci-token  -Djib.to.auth.password=$CI_BUILD_TOKEN -Dmaven.repo.local=$MAVEN_USER_HOME
  when: manual
