# Variable 'static_dictation_deployment_token' was defined in the Variables tab
trigger:
  branches:
    include:
    - refs/heads/develop
name: $(date:yyyyMMdd)$(rev:.r)
jobs:
- job: Job_1
  displayName: Dictation App Service Deploy
  pool:
    vmImage: ubuntu-latest
  steps:
  - checkout: self
    fetchDepth: 2
  - task: Npm@1
    displayName: npm ci
    inputs:
      command: ci
      workingDir: dictation_server
      verbose: false
  - task: AzureKeyVault@2
    displayName: 'Azure Key Vault: kv-odms-secret-stg'
    enabled: False
    inputs:
      ConnectedServiceName: 82047c72-0cfe-43b0-bb2f-cdfd8b165e68
      KeyVaultName: kv-odms-secret-stg
      SecretsFilter: token-public-key
  - task: Bash@3
    displayName: Bash Script (Test)
    inputs:
      targetType: inline
      script: cd dictation_server && npm run test
  - task: Docker@0
    displayName: build
    inputs:
      azureSubscriptionEndpoint: 827be338-1846-4102-b006-f457c92883d3
      azureContainerRegistry: '{"loginServer":"crodmsregistrymaintenance.azurecr.io", "id" : "/subscriptions/108fb131-cdca-4729-a2be-e5bd8c0b3ba7/resourceGroups/maintenance-rg/providers/Microsoft.ContainerRegistry/registries/crOdmsRegistryMaintenance"}'
      dockerFile: DockerfileServerDictation.dockerfile
      imageName: $(Build.Repository.Name)/staging/dictation:latest
  - task: Docker@0
    displayName: Push
    inputs:
      azureSubscriptionEndpoint: 827be338-1846-4102-b006-f457c92883d3
      azureContainerRegistry: '{"loginServer":"crodmsregistrymaintenance.azurecr.io", "id" : "/subscriptions/108fb131-cdca-4729-a2be-e5bd8c0b3ba7/resourceGroups/maintenance-rg/providers/Microsoft.ContainerRegistry/registries/crOdmsRegistryMaintenance"}'
      action: Push an image
      imageName: $(Build.Repository.Name)/staging/dictation:latest
- job: Job_2
  displayName: Dictation Static App Service Deploy
  pool:
    vmImage: ubuntu-latest
  steps:
  - checkout: self
    fetchDepth: 2
  - task: Npm@1
    displayName: npm ci
    inputs:
      command: ci
      workingDir: dictation_client
      verbose: false
  - task: Bash@3
    displayName: Bash Script
    inputs:
      targetType: inline
      script: cd dictation_client && cp -f .env.staging .env && npm run build
  - task: AzureStaticWebApp@0
    displayName: 'Static Web App: '
    inputs:
      app_location: /dictation_client/build
      config_file_location: /dictation_client
      skip_app_build: true
      skip_api_build: true
      is_static_export: false
      verbose: false
      azure_static_web_apps_api_token: $(static_dictation_deployment_token)
- job: Job_3
  displayName: DB migration
  dependsOn:
  - Job_1
  - Job_2
  pool:
    name: db-migrate-pipelines
  steps:
  - checkout: self
    fetchDepth: 2
  - task: GoTool@0
    displayName: Use Go 1.18
    inputs:
      version: 1.18
  - task: Go@0
    displayName: go install
    inputs:
      command: custom
      customCommand: install
      arguments: github.com/rubenv/sql-migrate/sql-migrate@latest
  - task: AzureKeyVault@2
    displayName: 'Azure Key Vault: kv-odms-secret-stg'
    inputs:
      ConnectedServiceName: 82047c72-0cfe-43b0-bb2f-cdfd8b165e68
      KeyVaultName: kv-odms-secret-stg
  - task: CmdLine@2
    displayName: migration
    inputs:
      script: >2
         # DB接続情報書き換え
           sed -i -e "s/DB_NAME/$(db-name)/g"  ./dictation_server/db/dbconfig.yml
           sed -i -e "s/DB_PASS/$(db-pass)/g"  ./dictation_server/db/dbconfig.yml
           sed -i -e "s/DB_USERNAME/$(db-user)/g"  ./dictation_server/db/dbconfig.yml
           sed -i -e "s/DB_PORT/$(db-port)/g"  ./dictation_server/db/dbconfig.yml
           sed -i -e "s/DB_HOST/$(db-host)/g"  ./dictation_server/db/dbconfig.yml
           /home/azureuser/go/bin/sql-migrate --version
           cat ./dictation_server/db/dbconfig.yml
           # migration実行
           /home/azureuser/go/bin/sql-migrate up -config=dictation_server/db/dbconfig.yml -env=ci
...
