trigger:
- main-dev
- master

variables:
  - group: deployment-credentials
  - name: dockerImage
    value: 'developertoyosa/laravel:v8'

pool:
  vmImage: ubuntu-latest

stages:
- stage: Build
  jobs:
  - job: BuildJob
    steps:
    - checkout: self
    - task: Docker@2
      displayName: "Ejecutar build"
      inputs:
        command: run
        arguments: >
          --rm
          -v $(System.DefaultWorkingDirectory):/app
          -w /app
          $(dockerImage)
          /bin/sh -c
          "composer install"
    - script: |
        tar -czf $(Build.ArtifactStagingDirectory)/artifact.tar.gz -C $(System.DefaultWorkingDirectory) .
      displayName: "Comprimir artefactos"
    - task: PublishBuildArtifacts@1
      displayName: "Publicar artefactos"
      inputs:
        PathtoPublish: $(Build.ArtifactStagingDirectory)/artifact.tar.gz
        ArtifactName: drop
        publishLocation: Container

- stage: Deploy
  dependsOn: Build
  jobs:
  - deployment: DeployTesting
    environment: testing
    condition: and(
                    eq(variables['Build.SourceBranch'], 'refs/heads/main-dev'),
                    ne(variables['QA_PATH'], ''),
                    ne(variables['QA_COMMAND'], '')
                  )
    variables:
      dockerCommands: |
        apt-get install rsync sshpass -y &&
        find -type d -exec chmod 755 {} \; &&
        find -type f -exec chmod 644 {} \; &&
        chmod -R 777 storage &&
        mv .env.qa .env &&
        chmod 444 .env &&
        rsync -urvlzc --delete-after --exclude-from='.rsyncignore' -e 'sshpass -p $(QA_SSH_PASSWORD) ssh -t -o StrictHostKeyChecking=no' ./ $(QA_SSH_USERNAME):$(QA_PATH) &&
        sshpass -p $(QA_SSH_PASSWORD) ssh -t -o StrictHostKeyChecking=no $(QA_SSH_USERNAME) '$(QA_COMMAND)' &&
        exit
    strategy:
      runOnce:
        deploy:
          steps:
          - download: current
            artifact: drop
          - script: |
              tar -xzf $(Pipeline.Workspace)/drop/artifact.tar.gz -C $(System.DefaultWorkingDirectory)
            displayName: "Descomprimir artefactos"
          - task: Docker@2
            displayName: "Deploy en server Testing"
            inputs:
              command: run
              arguments: >
                --rm
                -v $(System.DefaultWorkingDirectory):/app
                -w /app
                $(dockerImage)
                /bin/sh -c
                "$(dockerCommands)"

  - deployment: DeployProduction
    environment: production
    condition: and(
                    succeeded(),
                    eq(variables['Build.SourceBranch'], 'refs/heads/master'),
                    ne(variables['PROD_PATH'], ''),
                    ne(variables['PROD_COMMAND'], '')
                  )
    variables:
      dockerCommands: |
        apt-get install rsync sshpass -y &&
        find -type d -exec chmod 755 {} \; &&
        find -type f -exec chmod 644 {} \; &&
        chmod -R 777 storage &&
        mv .env.production .env &&
        chmod 444 .env &&
        rsync -urvlzc --delete-after --exclude-from='.rsyncignore' -e 'sshpass -p $(PROD_SSH_PASSWORD_API) ssh -t -o StrictHostKeyChecking=no' ./ $(PROD_SSH_USERNAME_API):$(PROD_PATH) &&
        sshpass -p $(PROD_SSH_PASSWORD_API) ssh -t -o StrictHostKeyChecking=no $(PROD_SSH_USERNAME_API) '$(PROD_COMMAND)' &&
        exit
    strategy:
      runOnce:
        deploy:
          steps:
          - download: current
            artifact: drop
          - script: |
              tar -xzf $(Pipeline.Workspace)/drop/artifact.tar.gz -C $(System.DefaultWorkingDirectory)
            displayName: "Descomprimir artefactos"
          - task: Docker@2
            displayName: "Deploy en server Production"
            inputs:
              command: run
              arguments: >
                --rm
                -v $(System.DefaultWorkingDirectory):/app
                -w /app
                $(dockerImage)
                /bin/sh -c
                "$(dockerCommands)"
