
variables:
  CONTAINER_BRANCH_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG

stages:
  - compile
  - test
  - package
  - analyse
  - publish
  - image
  - release
  - deploy

.maven:
  image: registry.esss.lu.se/ics-docker/maven:openjdk-17
  tags:
    - docker

.docker:
  image: docker:latest
  tags:
    - docker

compile:
  stage: compile
  extends: .maven
  script:
    - mvn --batch-mode compile
  artifacts:
    paths:
      - target/
    expire_in: 1 day

test:
  stage: test
  extends: .maven
  script:
    - mvn --batch-mode test
  artifacts:
    reports:
      junit:
      - target/surefire-reports/TEST-*.xml

package:
  stage: package
  extends: .maven
  script:
    - mvn --batch-mode install
  artifacts:
    paths:
      - target/naming-backend-*.jar

# sonar:
#   stage: analyse
#   extends: .maven
#   script:
#     - mvn --batch-mode sonar:sonar -Dsonar.login=$SONARQUBE_TOKEN -Dsonar.branch.name=$CI_COMMIT_REF_SLUG
#   only:
#     - branches@ics-software/naming-backend

docker-build:
  stage: image
  extends: .docker
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker build -t $CONTAINER_BRANCH_IMAGE .
    - docker push $CONTAINER_BRANCH_IMAGE
  only:
    - branches

docker-release:
  stage: release
  extends: .docker
  before_script:
    - apk add xmlstarlet
    - POM_VERSION=$(xmlstarlet sel -N pom=http://maven.apache.org/POM/4.0.0 -t -v "/pom:project/pom:version" pom.xml)
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker pull $CONTAINER_BRANCH_IMAGE
    - docker tag $CONTAINER_BRANCH_IMAGE $CI_REGISTRY_IMAGE:$POM_VERSION
    - docker tag $CONTAINER_BRANCH_IMAGE $CI_REGISTRY_IMAGE:latest
    - docker push $CI_REGISTRY_IMAGE:$POM_VERSION
    - docker push $CI_REGISTRY_IMAGE:latest
  only:
    - main

.deploy:
  stage: deploy
  image: registry.esss.lu.se/ics-docker/awxkit
  tags:
    - docker
  script:
    - >
      awx job_templates launch ${AWX_JOB_TEMPLATE}
      --extra_vars "naming_container_image_tag: $CI_COMMIT_REF_SLUG"
      --monitor
  dependencies: []
  only:
    - branches@ics-software/naming-backend
  when: manual

deploy-test-02:
  extends: .deploy
  variables:
    AWX_JOB_TEMPLATE: deploy-naming-test-02
  environment:
    name: test-02
    url: https://naming-test-02.cslab.esss.lu.se
