# Node.js
# Build a general Node.js project with npm.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/javascript

trigger:
- master

pool: Default
#  vmImage: ubuntu-latest
steps:
- task: NodeTool@0
  inputs:
    versionSpec: '16.x'
  displayName: 'Install Node.js'

- script: |
    npm install
    npm run build
    rm -rf node_modules
  displayName: 'npm install and build'
- task: ArchiveFiles@2
  inputs:
    rootFolderOrFile: './'
    includeRootFolder: false
    archiveType: 'zip'
    archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
    replaceExistingArchive: true
- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
    ArtifactName: '$(Build.BuildId).zip'
    publishLocation: 'Container'

- task: CopyFilesOverSSH@0
  inputs:
    sshEndpoint: 'NGWEB1'
    sourceFolder: '$(Build.ArtifactStagingDirectory)'
    contents: '$(Build.BuildId).zip'
    targetFolder: '/apps/tgd/photo-service'
    cleanTargetFolder: true
    readyTimeout: '20000'
- task: SSH@0
  inputs:
    sshEndpoint: 'NGWEB1'
    runOptions: 'inline'
    inline: |
      unzip -qo /apps/tgd/photo-service/$(Build.BuildId).zip -d /apps/tgd/photo-service/ 
      rm -f /apps/tgd/photo-service/$(Build.BuildId).zip
      cd /apps/tgd/photo-service && npm install >/dev/null 2>&1
      pm2 restart tgd-photo-service
    readyTimeout: '20000'