# Docker
# Build a Docker image
# https://docs.microsoft.com/azure/devops/pipelines/languages/docker

trigger:
  - none

variables:
  tag: 1.0.$(Build.BuildId)
  pool: 'PublishDockerAgent'

jobs:


- job: BuildAndPushJOBS
  variables:
    imageName: 'jobs/server'
    repositoryAddress: 'registry.buluttakin.com'
    #######################################
  displayName: BuildAndPushJOBS
  pool:
    name: $(pool)
  steps:
  - task: Docker@2
    inputs:
      containerRegistry: 'DockerReg'
      command: 'login'
  - task: Docker@2
    displayName: 'Build and Push'
    inputs:
      command: buildAndPush
      containerRegistry: BulutReg  ################
      repository: '$(imageName)'
      dockerfile: '$(Build.SourcesDirectory)/Dockerfile'
      tags: |
        $(tag)
        latest
  - task: SSH@0
    inputs:
      sshEndpoint: 9610 ################
      runOptions: 'commands'
      commands: |
        docker rmi -f $(repositoryAddress)/$(imageName):latest 2>/dev/null
        docker pull $(repositoryAddress)/$(imageName):latest 2>/dev/null
      readyTimeout: '20000'



- job: DockerComposeUP
  dependsOn: 
    - BuildAndPushJOBS
  variables:
    repositoryAddress: 'registry.buluttakin.com'
    composeFileLocationOnProduction: '/var/projects/jobs/compose.yml'
    #######################################
  displayName: DockerComposeUP
  pool:
    name: $(pool)
  steps:
  ### SERVICE 1
  - task: SSH@0
    inputs:
      sshEndpoint: 9610 ################
      runOptions: 'commands'
      commands: |
        docker compose -f $(composeFileLocationOnProduction) down 2>/dev/null && docker compose -f $(composeFileLocationOnProduction) up -d 2>/dev/null
        docker restart nginx  2>/dev/null
      readyTimeout: '20000'

