# This file is a template, and might need editing before it works on your project.
# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Getting-Started.gitlab-ci.yml

# This is a sample GitLab CI/CD configuration file that should run without any modifications.
# It demonstrates a basic 3 stage CI/CD pipeline. Instead of real tests or scripts,
# it uses echo commands to simulate the pipeline execution.
#
# A pipeline is composed of independent jobs that run scripts, grouped into stages.
# Stages run in sequential order, but jobs within stages run in parallel.
#
# For more information, see: https://docs.gitlab.com/ee/ci/yaml/index.html#stages

stages:          # List of stages for jobs, and their order of execution
  - build
  # - test
  - deploy
  - notify_telegram

build-job:       # This job runs in the build stage, which runs first.
  stage: build
  tags:
    - "linux_server"
  script:
    - echo "Compiling the code..."
    - echo "Compile complete."

# unit-test-job:   # This job runs in the test stage.
#   stage: test    # It only starts when the job in the build stage completes successfully.
#   script:
#     - echo "Running unit tests... This will take about 60 seconds."
#     #- sleep 60
#     - echo "Code coverage is 90%"

# lint-test-job:   # This job also runs in the test stage.
#   stage: test    # It can run at the same time as unit-test-job (in parallel).
#   script:
#     - echo "Linting code... This will take about 10 seconds."
#     #- sleep 10
#     - echo "No lint issues found."

#deploy-job:      # This job runs in the deploy stage.
#  stage: deploy  # It only runs when *both* jobs in the test stage complete successfully.
#  tags:
#    - "main_server"
#  script:
#    - echo "Deploying application..."
#    - cd /var/www/futplus_django
#    - sudo -S git reset --hard HEAD
#    - sudo -S git pull origin main
#    - sudo venv/bin/python -m pip install -r requirements.txt
#    - sudo venv/bin/python manage.py migrate
#    - sudo venv/bin/python manage.py sync_roles
#    - sudo venv/bin/python manage.py collectstatic --noinput
#    - sudo rm log/gunicorn_access_log
#    - sudo -S supervisorctl restart all
#    - echo "Application successfully deployed."

worker-servers-job:      # This job runs in the deploy stage.
  stage: deploy  # It only runs when *both* jobs in the test stage complete successfully.
  tags:
    - "worker_server"
#  parallel: 3
  script:
    - echo "Deploying application..."
    - cd /var/www/futplus_django
    - sudo -S git reset --hard HEAD
    - sudo -S git pull origin main
    - sudo venv/bin/python -m pip install -r requirements.txt
    - sudo venv/bin/python manage.py migrate
    - sudo venv/bin/python manage.py migrate --database logs
    - sudo venv/bin/python manage.py sync_roles
    - venv/bin/python manage.py compress --force
    - sudo venv/bin/python manage.py collectstatic --noinput
#    end of remove
    - sudo rm -f log/gunicorn_access_log
    - sudo -S supervisorctl restart all
    - echo "Application successfully deployed."

sbc-solvation-worker-servers-job:      # This job runs in the deploy stage.
  stage: deploy  # It only runs when *both* jobs in the test stage complete successfully.
  tags:
    - "sbc_solvation_worker_server"
#  parallel: 3
  script:
    - echo "Deploying application..."
    - cd /var/www/futplus_django
    - sudo -S git reset --hard HEAD
    - sudo -S git pull origin main
    - sudo venv/bin/python -m pip install -r requirements.txt
    - sudo -S supervisorctl restart all
    - echo "Application successfully deployed."

worker-servers-job2:      # This job runs in the deploy stage.
  stage: deploy  # It only runs when *both* jobs in the test stage complete successfully.
  tags:
    - "worker_server_2"
#  parallel: 3
  script:
    - echo "Deploying application..."
    - cd /var/www/futplus_django
    - sudo -S git reset --hard HEAD # for -S commands need to add full privilege to gitlab-runner user with sudoers
    - sudo -S git pull origin main
    - sudo venv/bin/python -m pip install -r requirements.txt
    - sudo venv/bin/python manage.py compress --force
    - sudo venv/bin/python manage.py collectstatic --noinput
#    end of remove
    - sudo rm -f log/gunicorn_access_log
    - sudo -S supervisorctl restart futplus_celery_worker:*
    - sudo -S supervisorctl restart futplus_gunicorn:*
    - echo "Application successfully deployed."

notify_telegram:
  stage: notify_telegram
  tags:
    - "linux_server"
  script:
    - |
      COMMIT_MSG=$(git log --pretty=format:"- %s" $CI_COMMIT_BEFORE_SHA..$CI_COMMIT_SHA | sed ':a;N;$!ba;s/\n/%0A/g')
      MESSAGE="🚀 New push detected!%0A%0A$COMMIT_MSG"
      curl -X POST "https://api.telegram.org/bot1980303938:AAH9LtY6_pxOHJ226xtZmJUghAOmmnSOmAE/sendMessage" \
           -d "chat_id=-885694083" \
           -d "text=$MESSAGE"
  only:
    - main