variables:
  DOCKER_HOST: $DOCKER_HOST_BIO
  PROJECT_NAME: "backend"


stages:
  - build
  - verification
  - publish
  - deploy



# ===========================================================================
# Stage: build
# ===========================================================================

gradlew-bootJar:
  stage: build
  image: eclipse-temurin:11-jdk-alpine
  script:
    - ./gradlew bootJar


docker-build-latest:
  stage: build
  image: docker:git
  only:
    refs:
      - main
  except:
    variables:
      - $CI_COMMIT_TAG
  before_script:
    - mkdir -p build
  script:
    - docker build -t $DOCKER_NAMESPACE/$PROJECT_NAME:latest .
    - docker save $DOCKER_NAMESPACE/$PROJECT_NAME:latest | bzip2 > build/image.tar.bz2
  artifacts:
    paths:
      - build/image.tar.bz2
    expire_in: 10 mins  # Optional: Set an expiration time for the artifact


docker-build-tag:
  stage: build
  image: docker:git
  only:
    variables:
      - $CI_COMMIT_TAG
  before_script:
    - mkdir -p build
  script:
    - docker build -t $DOCKER_NAMESPACE/$PROJECT_NAME:$CI_COMMIT_TAG .
    - docker save $DOCKER_NAMESPACE/$PROJECT_NAME:$CI_COMMIT_TAG | bzip2 > build/image.tar.bz2
  artifacts:
    paths:
      - build/image.tar
    expire_in: 10 mins  # Optional: Set an expiration time for the artifact



# ===========================================================================
# Stage: test
# ===========================================================================

verification-unit:
  stage: verification
  image: eclipse-temurin:11-jdk-alpine
  allow_failure: true
  script:
    - ./gradlew test
  artifacts:
    when: always
    reports:
      junit: build/test-results/test/**/TEST-*.xml
    expire_in: 10 mins

# ===========================================================================
# Stage: publish
# ===========================================================================

docker-push-latest:
  stage: publish
  image: docker
  only:
    - main
  before_script:
    - docker login -u $DOCKER_USERNAME -p $DOCKER_TOKEN $DOCKER_REGISTRY
  script:
    - bzip2 -d -c build/image.tar.bz2 > build/image.tar
    - docker load -i build/image.tar
    - docker push $DOCKER_NAMESPACE/$PROJECT_NAME:latest

docker-push-tag:
  stage: publish
  image: docker
  only:
    variables:
      - $CI_COMMIT_TAG
  before_script:
    - docker login -u $DOCKER_USERNAME -p $DOCKER_TOKEN $DOCKER_REGISTRY
  script:
    - bzip2 -d -c build/image.tar.bz2 > build/image.tar
    - docker load -i build/image.tar
    - docker push $DOCKER_NAMESPACE/$PROJECT_NAME:$CI_COMMIT_TAG

# ===========================================================================
# Stage: deploy
# ===========================================================================

deploy-bioinformatika.pef.mendelu.cz:
  stage: deploy
  only:
    - main
  script:
    - cp .env /home/bioinformatics/
    - cd /home/bioinformatics/
    - docker-compose config > docker-swarm.yml
#    - docker stack deploy -c docker-compose.yml test_stack
#  after_script:
#    - docker system prune -f

deploy-bioinformatics.ibp.cz:
  stage: deploy
  only:
    variables:
      - $CI_COMMIT_TAG
  script:
    - scp .env root@bioinformatics.ibp.cz:/opt/
    - scp docker-compose.yml root@bioinformatics.ibp.cz:/opt/
    - ssh root@bioinformatics.ibp.cz "cd /opt && export $(xargs < .env) && docker stack deploy dna-analyser -c docker-compose.yml"



