Search code examples
azureazure-devopsazure-web-app-serviceazure-pipelinesazure-webjobs

How to deploy a WebJob from a pipeline


I'm attempting to deploy a WebJob for an App Service, alongside the app service itself. But no method I've found has worked. All of the online info on the topic seem to be out of date (with no Microsoft documentation that I can find).

Everything else works- I've verified that the artifact has all the correct files, in the correct places. The app service itself gets uploaded fine. The issue is purely the WebJob. (I've edited this for clarity and security)

trigger:
- development

pool:
  vmImage: windows-latest

variables:
  BuildConfiguration: 'Release'
  AzureSubscription: 'Subscription'

stages:
- stage: Build
  jobs:
  - job: BuildJob
    steps:
    - task: NuGetCommand@2
      inputs:
        command: 'restore'
        restoreSolution: '**/*.sln'
        feedsToUse: 'select'

    - task: VSBuild@1
      inputs:
        solution: '**/API.csproj'
        configuration: '$(BuildConfiguration)'
        msbuildArgs: '/p:OutputPath=$(Build.ArtifactStagingDirectory)/APIOutput'
        platform: 'any cpu'

    - task: VSBuild@1
      inputs:
        solution: '**/WebJob.csproj'
        configuration: '$(BuildConfiguration)'
        msbuildArgs: '/p:OutputPath=$(Build.ArtifactStagingDirectory)/WebJobOutput'
        platform: 'any cpu'

    - task: PublishBuildArtifacts@1
      inputs:
        pathtoPublish: '$(Build.ArtifactStagingDirectory)'
        artifactName: 'drop'
        publishLocation: 'Container'

- stage: Deploy
  dependsOn: Build
  jobs:
  - job: DeployJob
    steps:
    - download: current
      artifact: 'drop'

    - task: AzureRmWebAppDeployment@4
      inputs:
        ConnectionType: 'AzureRM'
        azureSubscription: '$(AzureSubscription)'
        appType: 'webApp'
        WebAppName: 'WebAppName'
        packageForLinux: '$(Pipeline.Workspace)/drop/APIOutput'

    - task: AzureRmWebAppDeployment@4
      inputs:
        ConnectionType: 'AzureRM'
        azureSubscription: '$(AzureSubscription)'
        appType: 'webApp'
        WebAppName: 'WebAppName'
        packageForLinux: '$(Pipeline.Workspace)/drop/WebJobOutput'

Solution

  • I have referred this blog by Rahul Nath and Deployed Azure Web App + Azure WebJob together using Azure DevOps Build and Release pipeline and also Deployed it in a single Build YAML Pipeline

    Refer my Github Repository, Referenced from Rahul Nath's repository with my own Azure YAML Pipeline to deploy the Web App + WebJob in a single yaml pipeline.

    I Pushed this repository in Azure DevOps repos and then built my Azure Web app + Azure WebJob solution like below:-

    My Build YAML pipeline:-

    
    trigger:
    - master
    
    pool:
      vmImage: 'windows-latest'
    
    steps:
    - task: DotNetCoreCLI@2
      inputs:
        command: 'build'
        projects: '**/*.csproj'
        arguments: '--configuration Release'
    
    - task: DotNetCoreCLI@2
      inputs:
        command: 'publish'
        publishWebProjects: false
        projects: '**/WebJobExample.WebJob.csproj'
        arguments: '--output $(Build.BinariesDirectory)/publish_output/App_Data/jobs/continuous/YoutubeWebJob'
        zipAfterPublish: false
        modifyOutputPath: false
    
    - task: DotNetCoreCLI@2
      inputs:
        command: 'publish'
        publishWebProjects: true
        arguments: '--output $(Build.BinariesDirectory)/publish_output'
        zipAfterPublish: false
        modifyOutputPath: false
    
    - task: ArchiveFiles@2
      inputs:
        rootFolderOrFile: '$(Build.BinariesDirectory)/publish_output'
        includeRootFolder: false
        archiveType: 'zip'
        archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
        replaceExistingArchive: true
    
    - task: PublishBuildArtifacts@1
      inputs:
        PathtoPublish: '$(Build.ArtifactStagingDirectory)'
        ArtifactName: 'drop'
        publishLocation: 'Container'
    

    Output:-

    enter image description here

    Now, Use build Artifact in your release pipeline.

    Select your build artifact above in the Release pipeline:-

    enter image description here

    In the Stage select Deploy Azure App Service task and add the required configurations :-

    I created one .Net 6.0 Windows Azure Web app and selected it in my Task like below:-

    enter image description here

    In Deploy Azure App Service step added **/appsettings.json in the File Transform step and kept rest of the settings as it is:-

    enter image description here

    Added one variable with AzureWebJobStorage connection string which is the connection string of my Storage account:-

    enter image description here

    Now, I saved the pipeline and created a Release:-

    Release was successful and Azure Web App + Azure Web Job got deployed successfully:-

    Output:-

    enter image description here

    enter image description here

    enter image description here

    If you want to run this in a single YAML pipeline refer the code below:-

    trigger:
    - master
    
    pool:
      vmImage: 'windows-latest'
    
    steps:
    - task: DotNetCoreCLI@2
      inputs:
        command: 'build'
        projects: '**/*.csproj'
        arguments: '--configuration Release'
    
    - task: DotNetCoreCLI@2
      inputs:
        command: 'publish'
        publishWebProjects: false
        projects: '**/WebJobExample.WebJob.csproj'
        arguments: '--output $(Build.BinariesDirectory)/publish_output/App_Data/jobs/continuous/YoutubeWebJob'
        zipAfterPublish: false
        modifyOutputPath: false
    
    - task: DotNetCoreCLI@2
      inputs:
        command: 'publish'
        publishWebProjects: true
        arguments: '--output $(Build.BinariesDirectory)/publish_output'
        zipAfterPublish: false
        modifyOutputPath: false
    
    - task: ArchiveFiles@2
      inputs:
        rootFolderOrFile: '$(Build.BinariesDirectory)/publish_output'
        includeRootFolder: false
        archiveType: 'zip'
        archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
        replaceExistingArchive: true
    
    - task: PublishBuildArtifacts@1
      inputs:
        PathtoPublish: '$(Build.ArtifactStagingDirectory)'
        ArtifactName: 'drop'
        publishLocation: 'Container'
    
    
    - task: AzureRmWebAppDeployment@4
      inputs:
        ConnectionType: 'AzureRM'
        azureSubscription: 'xx subscription (xxxxxxxxxxxe97cb2a7)'
        appType: 'webApp'
        WebAppName: 'valleywebappjob'
        packageForLinux: '$(Agent.BuildDirectory)/**/*.zip'
        WebConfigParameters: '**/appsettings.json'
    

    Output:-

    enter image description here