.gitlab-ci.yml 4.06 KB
Newer Older
variables:
  IMAGE_NAME: 'automation'
  IMAGE_TAG: '0.1.0'
  REPORTS_PATH: "src/${IMAGE_NAME}/reports"

# Package application needed to run tests & build the image on next stage
build automation:
  stage: build
  script:
    - docker build -t "$IMAGE_NAME:$IMAGE_TAG" -f ./src/$IMAGE_NAME/src/main/docker/Dockerfile.multistage.jvm ./src/$IMAGE_NAME/ --target builder
  after_script:
    - docker images --filter="dangling=true" --quiet | xargs -r docker rmi
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH)'
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "develop"'
    - changes:
        - src/$IMAGE_NAME/**
        - manifests/${IMAGE_NAME}service.yaml
        - .gitlab-ci.yml

# Run tests, build & push the image
unit_test automation:
  variables:
    REPORTS_CONTAINER: "${IMAGE_NAME}-reports"
  stage: test
  needs:
    - build automation
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - docker rm ${REPORTS_CONTAINER} || true
  script:
    - docker build -t "$IMAGE_NAME:$IMAGE_TAG" -f ./src/$IMAGE_NAME/src/main/docker/Dockerfile.multistage.jvm ./src/$IMAGE_NAME/ --target unit-test
    # Transfer JaCoCo and Surefire reports from within tests image
    - docker create --name ${REPORTS_CONTAINER} "$IMAGE_NAME:$IMAGE_TAG"
    - mkdir -p ${REPORTS_PATH}
    - docker cp ${REPORTS_CONTAINER}:/app/target/site/jacoco/index.html ${REPORTS_PATH}/coverage.html
    - docker cp ${REPORTS_CONTAINER}:/app/target/site/jacoco/jacoco.xml ${REPORTS_PATH}/jacoco.xml
    - docker cp ${REPORTS_CONTAINER}:/app/target/surefire-reports/ ${REPORTS_PATH}/
    - cat ${REPORTS_PATH}/coverage.html | grep -o 'Total[^%]*%' | sed 's/<.*>/ /; s/Total/JaCoCo Coverage Total:/'
    # Build final image
    - docker build -t "$IMAGE_NAME:$IMAGE_TAG" -f ./src/$IMAGE_NAME/src/main/docker/Dockerfile.multistage.jvm ./src/$IMAGE_NAME/ --target release
    - docker tag "$IMAGE_NAME:$IMAGE_TAG" "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
    - docker push "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
  after_script:
    - docker rm ${REPORTS_CONTAINER}
    - docker rm -f "$IMAGE_NAME:$IMAGE_TAG"
  coverage: '/JaCoCo Coverage Total: ([0-9]{1,3})%/'
  artifacts:
    when: always
    paths:
      - ${REPORTS_PATH}/jacoco.xml
      - ${REPORTS_PATH}/coverage.html
    reports:
      junit:
        - ${REPORTS_PATH}/surefire-reports/TEST-*.xml
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH)'
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "develop"'
    - changes:
        - src/$IMAGE_NAME/**
        - manifests/${IMAGE_NAME}service.yaml
        - .gitlab-ci.yml

visualize_coverage automation:
  stage: unit_test
  needs:
    - unit_test automation
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - stat ${REPORTS_PATH}/jacoco.xml || true
    - docker run -v "$(pwd)/src/${IMAGE_NAME}:/${IMAGE_NAME}" --rm registry.gitlab.com/haynes/jacoco2cobertura:1.0.7 python /opt/cover2cover.py ${IMAGE_NAME}/reports/jacoco.xml ${IMAGE_NAME}/src/main/java > ${REPORTS_PATH}/cobertura.xml
  artifacts:
    reports:
      cobertura: ${REPORTS_PATH}/cobertura.xml

# Deployment of automation service in Kubernetes Cluster
deploy automation:
  stage: deploy
  needs:
    - build automation
    - unit_test automation
  script:
    - kubectl version
    - kubectl get all
    - kubectl delete -f "manifests/automationservice.yaml"
    - kubectl apply -f "manifests/automationservice.yaml"
    - kubectl delete pods --selector app=automationservice
    - kubectl get all
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH)'
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "develop"'
      when: manual