image: 'maven:3.6.3-jdk-11-slim'

variables:
  PROJECT_ARTIFACT_ID: kypo-adaptive-training
  DEPLOYMENT_INFO_VERSION_FILE: VERSION.txt

stages:
  - create_tag
  - build
  - tests
  - docker_image_push

create_tag:
  stage: create_tag
  before_script:
    # ssh config
    - which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh -o 'StrictHostKeyChecking no' "$SSH_HOST_CHECKING_DOMAIN"
    # git config
    - apt-get install git -y
    - git config --global user.email "$GIT_USER_EMAIL"
    - git config --global user.name "$GIT_USER_NAME"
    - git remote set-url origin "$GIT_CLONE_URL"
    - git fetch
    - git checkout master
  script:
    - export VERSION=$(grep -oP '^([^\s]*)' $DEPLOYMENT_INFO_VERSION_FILE)
    - export TAG_MESSAGE=$(grep -oP '(?<=\s)[^\s].*' $DEPLOYMENT_INFO_VERSION_FILE)
    # update project in this project
    - mvn versions:set -DnewVersion=$VERSION -DgenerateBackupPoms=false -Dproprietary-repo-url=$PROPRIETARY_REPO_URL
    - git commit -am "Update pom.xml version based on GitLab tag. Done by CI."
    # generate swagger documentation
    - mvn clean compile -DskipTests -Dswagger.skip=false -Dproprietary-repo-url=$PROPRIETARY_REPO_URL
    - git add doc-files/kypo-adaptive-training-open-api.yaml
    - git commit -m "Updated Swagger documentation generated."
    # update change log
    - LAST_TAG=$(git tag | head -1)
    - git log  --pretty=format:'%h -- %s' $LAST_TAG..HEAD --graph > CHANGELOG.md
    - git add CHANGELOG.md
    - git commit -m "CHANGELOG.md file updated with commits between the current and previous tag. Done by CI."
    # create new tag
    - git tag -a v$VERSION -m "$TAG_MESSAGE"
    - git config --global push.followTags true
    - git push
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
      changes:
        - VERSION.txt
  artifacts:
    paths:
      - ./pom.xml
      - ./*/pom.xml
    expire_in: 1 hour
    when: on_success

build:
  stage: build
  script:
    - mvn clean install -DskipTests -Dproprietary-repo-url=$PROPRIETARY_REPO_URL
  rules:
    - if: '$CI_COMMIT_BRANCH'
  artifacts:
    paths:
      - target/$PROJECT_ARTIFACT_ID-*.jar
    expire_in: 1 hour
    when: on_success

tests:
  stage: tests
  script:
    - mvn test -Dproprietary-repo-url=$PROPRIETARY_REPO_URL
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
      changes:
        - VERSION.txt
      when: never
    - if: '$CI_COMMIT_BRANCH'

docker_image_push:
  before_script: [ ] #prevent global before_script from running
  variables:
    CI_CUSTOM_IMAGE_NAME: kypo-adaptive-training-service
  stage: docker_image_push
  image:
    name: gcr.io/kaniko-project/executor:v1.3.0-debug
    entrypoint: [ "" ]
  script:
    - export CI_CUSTOM_REGISTRY_IMAGE="${CI_REGISTRY}/${CI_CUSTOM_REGISTRY_PATH}/${CI_CUSTOM_IMAGE_NAME}"
    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_CUSTOM_REGISTRY_USER\",\"password\":\"$CI_CUSTOM_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
    - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_CUSTOM_REGISTRY_IMAGE:$CI_COMMIT_TAG --build-arg PROPRIETARY_REPO_URL=$PROPRIETARY_REPO_URL
  rules:
    - if: '$CI_COMMIT_TAG'
    - if: '$CI_COMMIT_BRANCH'
      when: never
