Search code examples
azure-devopsazure-web-app-serviceazure-webjobsazure-pipelinesazure-webjobssdk

Multiple Jobs Deployment in Visual Studio Team Services


I have a VS solution with multiple projects, each project is an independent WebJob which I am hosting in a Single AppServices. I am trying to automate this to Continuous deployment.What would be the best strategy to deploy only job in the solution which is changed? Is there a way to find out the project which is changed as part of the merge to CI? We are using git and this solution is in a single repository. We are using azure-webjobsdk for the projects.


Solution

  • You can follow below steps to deploy the only changed webjob:

    Assume your file structure as below:

    Git root
       |___WebJob1
              |___WebJob1.sln
              |___webJob1
                     |___WebJob1.csproj
              |___WebJob2
                     |___WebJob2.csproj
              |___ …
              |___WebJobn
                     |___WebJobn.csproj
    

    1. Add a variable (such as buildporj with default value none) to record the changed webJob project package.

    2. Add NuGet restore task for the WebJob1/WebJob1.sln.

    3. Add Visual studio Build task with the MSBuild arguments:

    /p:DeployOnBuild=true /p:WebPublishMethod=Package /p:SkipInvalidConfigurations=true /p:PackageLocation="$(Build.BinariesDirectory)\\"
    

    enter image description here

    4. Add PowerShell Task to detect which WebJob project changed and copy the package from $(Build.BinariesDirectory) to $(Build.ArtifactStagingDirectory) by the script as below:

    $files=$(git diff HEAD HEAD~ --name-only)
    echo "changed files: $files"
    for ($i=0;$i -lt $files.Length; $i++)
    {
      $file=$files[$i] -split '/'
      $files[$i]=$file[1]
    }
    $uni=$files | Get-Unique
    
    $uni=@($uni | Where-Object {$_ -notmatch ".sln"})
    echo "You changed the project (if more than one projects were changed, only deploy the first one): $uni"
    $proj=$uni[0]+".zip"
    Write-Host "##vso[task.setvariable variable=buildproj;]$proj"
    Copy-Item $(Build.BinariesDirectory)\$proj $(Build.ArtifactStagingDirectory)
    
    1. Add Azure App Service Deploy task by specifing the Package or folder option as $(Build.ArtifactStagingDirectory)\**\$(buildproj).

    enter image description here