---
kind: pipeline
type: docker
name: Build-Pipeline

image_pull_secrets:
- dockerconfig

steps:
- name: build-smwarm-pilot-NPM${DRONE_TAG}
  image: registry.integral-systems.ch/library/node-builder:22
  environment:
    NPM_TOKEN:
      from_secret: npm-token
  settings:
    mtu: 1450
  commands:
  - npm version ${DRONE_TAG}
  - yarn 
  - yarn build
  - npm set //registry.npmjs.org/:_authToken=$${NPM_TOKEN}
  - npm publish --access public
- name: git commit changes
  image: alpine/git
  settings:
    mtu: 1450
  environment:
    KEY:
      from_secret: ssh-key
    GIT_AUTHOR_NAME: Drone CI
    GIT_AUTHOR_EMAIL: ci@integral-systems.ch
    GIT_COMMITTER_NAME: Drone CI
    GIT_COMMITTER_EMAIL: ci@integral-systems.ch
  commands:
  - mkdir -p /root/.ssh/
  - ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
  - echo "$KEY" > $HOME/.ssh/id_rsa
  - chmod 600 $HOME/.ssh/id_rsa
  - git config --global user.email "$GIT_COMMITTER_EMAIL"
  - git config --global user.name "$GIT_COMMITTER_NAME"
  - git remote update
  - git fetch origin master:master
  - git checkout master
  - echo "Checking for changes..."
  - |
    if [ -n "$(git status --porcelain)" ]; then
      echo "Changes detected. Committing update version ${DRONE_TAG}..."
      git commit -a -m "update version ${DRONE_TAG}"
    else
      echo "No changes detected in tracked files. Nothing to commit."
    fi
  - git remote set-url origin git@github.com:Integral-Systems/swarm-pilot.git # Adjust repo URL if needed
  - echo "Pushing changes..."
  - git push origin master
  when:
    status:
    - success
    ref:
      include:
      - refs/tags/*
- name: Build-Container-swarm-pilot-${DRONE_TAG}
  image: plugins/docker
  settings:
    mtu: 1450
    username:
      from_secret: docker_username
    password:
      from_secret: docker_password
    repo: registry.integral-systems.ch/integral-systems/swarm-pilot
    registry: registry.integral-systems.ch
    tags: ${DRONE_TAG}, latest
    build_args:
      - SWARM_PILOT_VERSION=${DRONE_TAG}
  when:
    ref:
      include:
      - refs/tags/*
- name: Push-docker-${DRONE_TAG}
  image: plugins/docker
  settings:
    mtu: 1450
    username:
      from_secret: docker-hub-username
    password:
      from_secret: docker-hub-password
    repo: gradlon/swarm-pilot
    purge: true
    tags:
      - ${DRONE_TAG}
      - latest
    build_args:
      - SWARM_PILOT_VERSION=${DRONE_TAG}
  when:
    ref:
      include:
      - refs/tags/*
- name: Notify-via-Telegram
  image: appleboy/drone-telegram
  settings:
    mtu: 1450
    purge: true
    token:
      from_secret: telegram_token
    to: 
      from_secret: telegram_default_groupe_id
    message_file: .drone/telegram_prod_build.tpl
  when:
    status:
    - failure
    - success
    ref:
      include:
      - refs/tags/*
trigger:
  event:
  - tag
############Production Deploy only##########
---
kind: pipeline
type: docker
name: Deploy-Production

steps:
- name: Deploy-Container-${DRONE_TAG}
  image: curlimages/curl
  environment:
    DEPLOY_URL:
      from_secret: deploy_url
  settings:
    mtu: 1450
  commands:
  - curl --max-time 240 --show-error --fail -X POST $${DEPLOY_URL}?SERVICE_TAG=${DRONE_TAG}
#############Notification###################
- name: Notify-via-Telegram
  image: appleboy/drone-telegram
  settings:
    mtu: 1450
    token:
      from_secret: telegram_token
    to: 
      from_secret: telegram_default_groupe_id
    message_file: .drone/telegram_prod_deploy.tpl
  when:
    status:
    - failure
    - success
- name: Notify-via-Mail
  image: drillster/drone-email
  settings:
    mtu: 1450
    host: smtp.gmail.com
    username:
      from_secret: gmail_user
    password:
      from_secret: gmail_pass
    from: admin@integralschweiz.org
    #recipients: [ gradlon@me.com ]
    #recipients_only: true
    body: file:///drone/src/.drone/mail_prod_deploy.hbs
    when:
      status: [ changed, failure, success ]
  when:
    status:
    - failure
    - success
trigger:
  event:
  - promote
  target:
  - production