# Pipeline側でKeyVaultやDocker、AppService等に対する操作権限を持ったServiceConenctionを作成しておくこと
# また、環境変数 STATIC_DICTATION_DEPLOYMENT_TOKEN の値として静的WebAppsのデプロイトークンを設定しておくこと
trigger:
  tags:
    include:
    - release-*

jobs:
- job: initialize
  displayName: Initialize
  pool:
    vmImage: ubuntu-latest
  steps:
  - checkout: self
    clean: true
    fetchDepth: 1
    persistCredentials: true
  - script: |
      git fetch origin main:main
      if git merge-base --is-ancestor $(Build.SourceVersion) main; then
        echo "This commit is in the main branch."
      else
        echo "This commit is not in the main branch."
        exit 1
      fi
    displayName: 'タグが付けられたCommitがmainブランチに存在するか確認'
- job: backend_deploy
  dependsOn: initialize
  condition: succeeded('initialize')
  displayName: Backend Deploy
  pool:
    vmImage: ubuntu-latest
  steps:
  - checkout: self
    clean: true
    fetchDepth: 1
  - task: AzureWebAppContainer@1
    inputs:
      azureSubscription: 'omds-service-connection-prod'
      appName: 'app-odms-dictation-prod'
      deployToSlotOrASE: true
      resourceGroupName: 'odms-prod-rg'
      slotName: 'staging'
      containers: 'crodmsregistrymaintenance.azurecr.io/odmscloud/staging/dictation:$(Build.SourceVersion)'
- job: frontend_deploy
  dependsOn: backend_deploy
  condition: succeeded('backend_deploy')
  displayName: Deploy Frontend Files
  variables:
    storageAccountName: saomdspipeline
    environment: production
  pool:
    vmImage: ubuntu-latest
  steps:
  - checkout: self
    clean: true
    fetchDepth: 1
  - task: AzureCLI@2
    inputs:
      azureSubscription: 'omds-service-connection-prod'
      scriptType: 'bash'
      scriptLocation: 'inlineScript'
      inlineScript: |
        az storage blob download \
          --auth-mode login \
          --account-name $(storageAccountName) \
          --container-name $(environment) \
          --name $(Build.SourceVersion).zip \
          --file $(Build.SourcesDirectory)/$(Build.SourceVersion).zip
  - task: Bash@3
    displayName: Bash Script
    inputs:
      targetType: inline
      script: unzip $(Build.SourcesDirectory)/$(Build.SourceVersion).zip -d $(Build.SourcesDirectory)/$(Build.SourceVersion)
  - task: AzureStaticWebApp@0
    displayName: 'Static Web App: '
    inputs:
      workingDirectory: '$(Build.SourcesDirectory)'
      app_location: '/$(Build.SourceVersion)'
      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: function_deploy
  dependsOn: frontend_deploy
  condition: succeeded('frontend_deploy')
  displayName: function Deploy
  pool:
    vmImage: ubuntu-latest
  steps:
  - checkout: self
    clean: true
    fetchDepth: 1
  - task: AzureFunctionAppContainer@1
    inputs:
      azureSubscription: 'omds-service-connection-prod'
      appName: 'func-odms-dictation-prod'
      imageName: 'crodmsregistrymaintenance.azurecr.io/odmscloud/staging/dictation_function:$(Build.SourceVersion)'
- job: smoke_test
  dependsOn: function_deploy
  condition: succeeded('function_deploy')
  displayName: 'smoke test'
  pool:
    name: odms-deploy-pipeline
  steps:
  - checkout: self
    clean: true
    fetchDepth: 1
    # スモークテスト用にjobを確保
- job: swap_slot
  dependsOn: smoke_test
  condition: succeeded('smoke_test')
  displayName: 'Swap Staging and Production'
  pool:
    name: odms-deploy-pipeline
  steps:
  - checkout: self
    clean: true
    fetchDepth: 1
  - task: AzureAppServiceManage@0
    displayName: 'Azure App Service Manage: app-odms-dictation-prod'
    inputs:
      azureSubscription: 'omds-service-connection-prod'
      action: 'Swap Slots'
      WebAppName: 'app-odms-dictation-prod'
      ResourceGroupName: 'odms-prod-rg'
      SourceSlot: 'staging'
      SwapWithProduction: true
- job: migration
  dependsOn: swap_slot
  condition: succeeded('swap_slot')
  displayName: DB migration
  pool:
    name: odms-deploy-pipeline
  steps:
  - checkout: self
    clean: true
    fetchDepth: 1
  - task: AzureKeyVault@2
    displayName: 'Azure Key Vault: kv-odms-secret-prod'
    inputs:
      ConnectedServiceName: 'omds-service-connection-prod'
      KeyVaultName: kv-odms-secret-prod
  - 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/$(admin-db-pass)/g"  ./dictation_server/db/dbconfig.yml
          sed -i -e "s/DB_USERNAME/$(admin-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
          sql-migrate --version
          cat ./dictation_server/db/dbconfig.yml
          # migration実行
          sql-migrate up -config=./dictation_server/db/dbconfig.yml -env=ci