jobs:
- job: BuildPackage
  displayName: 'Build Packages'

  strategy:
    matrix:
      CentOS.amd64:
        BuildConfiguration: centos.amd64
      Fedora.amd64:
        BuildConfiguration: fedora.amd64
      Debian.amd64:
        BuildConfiguration: debian.amd64
      Debian.arm64:
        BuildConfiguration: debian.arm64
      Debian.armhf:
        BuildConfiguration: debian.armhf
      Ubuntu.amd64:
        BuildConfiguration: ubuntu.amd64
      Ubuntu.arm64:
        BuildConfiguration: ubuntu.arm64
      Ubuntu.armhf:
        BuildConfiguration: ubuntu.armhf
      Linux.amd64:
        BuildConfiguration: linux.amd64
      Linux.amd64-musl:
        BuildConfiguration: linux.amd64-musl
      Linux.arm64:
        BuildConfiguration: linux.arm64
      Linux.musl-linux-arm64:
        BuildConfiguration: linux.musl-linux-arm64
      Linux.armhf:
        BuildConfiguration: linux.armhf
      Windows.amd64:
        BuildConfiguration: windows.amd64
      MacOS:
        BuildConfiguration: macos
      Portable:
        BuildConfiguration: portable

  pool:
    vmImage: 'ubuntu-latest'

  steps:
  - script: echo "##vso[task.setvariable variable=JellyfinVersion]$( awk -F '/' '{ print $NF }' <<<'$(Build.SourceBranch)' | sed 's/^v//' )"
    displayName: Set release version (stable)
    condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/v')

  - script: 'docker build -f deployment/Dockerfile.$(BuildConfiguration) -t jellyfin-server-$(BuildConfiguration) deployment'
    displayName: 'Build Dockerfile'

  - script: 'docker image ls -a && docker run -v $(pwd)/deployment/dist:/dist -v $(pwd):/jellyfin -e IS_UNSTABLE="yes" -e BUILD_ID=$(Build.BuildNumber) jellyfin-server-$(BuildConfiguration)'
    displayName: 'Run Dockerfile (unstable)'
    condition: startsWith(variables['Build.SourceBranch'], 'refs/heads/master')

  - script: 'docker image ls -a && docker run -v $(pwd)/deployment/dist:/dist -v $(pwd):/jellyfin -e IS_UNSTABLE="no" -e BUILD_ID=$(Build.BuildNumber) jellyfin-server-$(BuildConfiguration)'
    displayName: 'Run Dockerfile (stable)'
    condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/v')

  - task: PublishPipelineArtifact@1
    displayName: 'Publish Release'
    inputs:
      targetPath: '$(Build.SourcesDirectory)/deployment/dist'
      artifactName: 'jellyfin-server-$(BuildConfiguration)'

  - task: SSH@0
    displayName: 'Create target directory on repository server'
    inputs:
      sshEndpoint: repository
      runOptions: 'inline'
      inline: 'mkdir -p /srv/repository/incoming/azure/$(Build.BuildNumber)/$(BuildConfiguration)'

  - task: CopyFilesOverSSH@0
    displayName: 'Upload artifacts to repository server'
    inputs:
      sshEndpoint: repository
      sourceFolder: '$(Build.SourcesDirectory)/deployment/dist'
      contents: '**'
      targetFolder: '/srv/repository/incoming/azure/$(Build.BuildNumber)/$(BuildConfiguration)'

- job: OpenAPISpec
  dependsOn: Test
  condition: or(startsWith(variables['Build.SourceBranch'], 'refs/heads/master'),startsWith(variables['Build.SourceBranch'], 'refs/tags/v'))
  displayName: 'Push OpenAPI Spec to repository'

  pool:
    vmImage: 'ubuntu-latest'

  steps:
  - script: echo "##vso[task.setvariable variable=JellyfinVersion]$( awk -F '/' '{ print $NF }' <<<'$(Build.SourceBranch)' | sed 's/^v//' )"
    displayName: Set release version (stable)
    condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/v')

  - task: DownloadPipelineArtifact@2
    displayName: 'Download OpenAPI Spec'
    inputs:
      source: 'current'
      artifact: "OpenAPI Spec"
      path: "$(System.ArtifactsDirectory)/openapispec"
      runVersion: "latest"

  - task: SSH@0
    displayName: 'Create target directory on repository server'
    inputs:
      sshEndpoint: repository
      runOptions: 'inline'
      inline: 'mkdir -p /srv/repository/incoming/azure/$(Build.BuildNumber)'

  - task: CopyFilesOverSSH@0
    displayName: 'Upload artifacts to repository server'
    inputs:
      sshEndpoint: repository
      sourceFolder: '$(System.ArtifactsDirectory)/openapispec'
      contents: 'openapi.json'
      targetFolder: '/srv/repository/incoming/azure/$(Build.BuildNumber)'

- job: BuildDocker
  displayName: 'Build Docker'

  strategy:
    matrix:
      amd64:
        BuildConfiguration: amd64
      arm64:
        BuildConfiguration: arm64
      armhf:
        BuildConfiguration: armhf

  pool:
    vmImage: 'ubuntu-latest'

  variables:
  - name: JellyfinVersion
    value: 0.0.0

  steps:
  - script: echo "##vso[task.setvariable variable=JellyfinVersion]$( awk -F '/' '{ print $NF }' <<<'$(Build.SourceBranch)' | sed 's/^v//' )"
    displayName: Set release version (stable)
    condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/v')

  - task: Docker@2
    displayName: 'Push Unstable Image'
    condition: startsWith(variables['Build.SourceBranch'], 'refs/heads/master')
    inputs:
      repository: 'jellyfin/jellyfin-server'
      command: buildAndPush
      buildContext: '.'
      Dockerfile: 'deployment/Dockerfile.docker.$(BuildConfiguration)'
      containerRegistry: Docker Hub
      tags: |
        unstable-$(Build.BuildNumber)-$(BuildConfiguration)
        unstable-$(BuildConfiguration)

  - task: Docker@2
    displayName: 'Push Stable Image'
    condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/v')
    inputs:
      repository: 'jellyfin/jellyfin-server'
      command: buildAndPush
      buildContext: '.'
      Dockerfile: 'deployment/Dockerfile.docker.$(BuildConfiguration)'
      containerRegistry: Docker Hub
      tags: |
        stable-$(Build.BuildNumber)-$(BuildConfiguration)
        $(JellyfinVersion)-$(BuildConfiguration)

- job: CollectArtifacts
  timeoutInMinutes: 20
  displayName: 'Collect Artifacts'
  continueOnError: true
  dependsOn:
  - BuildPackage
  - BuildDocker

  pool:
    vmImage: 'ubuntu-latest'

  steps:
  - task: SSH@0
    displayName: 'Update Unstable Repository'
    continueOnError: true
    condition: startsWith(variables['Build.SourceBranch'], 'refs/heads/master')
    inputs:
      sshEndpoint: repository
      runOptions: 'commands'
      commands: nohup sudo /srv/repository/collect-server.azure.sh /srv/repository/incoming/azure $(Build.BuildNumber) unstable &

  - task: SSH@0
    displayName: 'Update Stable Repository'
    continueOnError: true
    condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/v')
    inputs:
      sshEndpoint: repository
      runOptions: 'commands'
      commands: nohup sudo /srv/repository/collect-server.azure.sh /srv/repository/incoming/azure $(Build.BuildNumber) $(Build.SourceBranch) &

- job: PublishNuget
  displayName: 'Publish NuGet packages'

  pool:
    vmImage: 'ubuntu-latest'

  variables:
  - name: JellyfinVersion
    value: $[replace(variables['Build.SourceBranch'],'refs/tags/v','')]

  steps:
  - task: UseDotNet@2
    displayName: 'Use .NET 6.0 sdk'
    inputs:
      packageType: 'sdk'
      version: '6.0.x'

  - task: DotNetCoreCLI@2
    displayName: 'Build Stable Nuget packages'
    condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/v')
    inputs:
      command: 'custom'
      projects: |
        Jellyfin.Data/Jellyfin.Data.csproj
        MediaBrowser.Common/MediaBrowser.Common.csproj
        MediaBrowser.Controller/MediaBrowser.Controller.csproj
        MediaBrowser.Model/MediaBrowser.Model.csproj
        Emby.Naming/Emby.Naming.csproj
        src/Jellyfin.Extensions/Jellyfin.Extensions.csproj
      custom: 'pack'
      arguments: -o $(Build.ArtifactStagingDirectory) -p:Version=$(JellyfinVersion)

  - task: DotNetCoreCLI@2
    displayName: 'Build Unstable Nuget packages'
    condition: startsWith(variables['Build.SourceBranch'], 'refs/heads/master')
    inputs:
      command: 'custom'
      projects: |
        Jellyfin.Data/Jellyfin.Data.csproj
        MediaBrowser.Common/MediaBrowser.Common.csproj
        MediaBrowser.Controller/MediaBrowser.Controller.csproj
        MediaBrowser.Model/MediaBrowser.Model.csproj
        Emby.Naming/Emby.Naming.csproj
        src/Jellyfin.Extensions/Jellyfin.Extensions.csproj
      custom: 'pack'
      arguments: '--version-suffix $(Build.BuildNumber) -o $(Build.ArtifactStagingDirectory) -p:Stability=Unstable'

  - task: PublishBuildArtifacts@1
    displayName: 'Publish Nuget packages'
    inputs:
      pathToPublish: $(Build.ArtifactStagingDirectory)
      artifactName: Jellyfin Nuget Packages

  - task: NuGetCommand@2
    displayName: 'Push Nuget packages to stable feed'
    condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/v')
    inputs:
      command: 'push'
      packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg'
      nuGetFeedType: 'external'
      publishFeedCredentials: 'NugetOrg'
      allowPackageConflicts: true # This ignores an error if the version already exists

  - task: NuGetAuthenticate@0
    displayName: 'Authenticate to unstable Nuget feed'
    condition: startsWith(variables['Build.SourceBranch'], 'refs/heads/master')

  - task: NuGetCommand@2
    displayName: 'Push Nuget packages to unstable feed'
    condition: startsWith(variables['Build.SourceBranch'], 'refs/heads/master')
    inputs:
      command: 'push'
      packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg' # No symbols since Azure Artifact does not support it
      nuGetFeedType: 'internal'
      publishVstsFeed: '7cce6c46-d610-45e3-9fb7-65a6bfd1b671/a5746b79-f369-42db-93ff-59cd066f9327'
      allowPackageConflicts: true # This ignores an error if the version already exists
