首页 > 解决方案 > 在 YAML 管道中将多个 Azure WebJobs 部署到单个 Azure AppService

问题描述

我正在尝试拥有一个部署管道,该管道可以同时部署 3 个 Azure WebJobs(连续),它们都是同一解决方案的一部分。我可以通过右键单击部署在 Visual Studio 中执行此操作,并确保我没有清除现有文件。

在 Azure Pipelines 中,我有以下脚本可成功用于单个 WebJob 部署。

但是,如果我复制它并为我的第二个 WebJob 创建一个新管道,它将替换现有的 WebJob,只留下 1 个运行。

我应该在下面的管道中修改什么来构建/部署所有 3 个 WebJobs?

trigger: none
    
pool:
  vmImage: ubuntu-latest
    
    # Modify these variables
variables:
  webJobName: 'My.WebJob.App'
  azureAppServiceName: 'my-webjobs'
  azureSPNName: 'MyRGConnection' #get it from your AzureDevOps portal
  buildConfiguration: 'Release'
  dotNetFramework: 'net6.0'
  dotNetVersion: '6.0.x'
  targetRuntime: 'win-x86'

# Build the app for .NET 6 framework  https://www.tiffanychen.dev/Azure-WebJob-Deployments-YAML/
steps:
- task: UseDotNet@2
  inputs:
    version: $(dotNetVersion)
    includePreviewVersions: true
  displayName: 'Build .NET 6 Application'

- task: DotNetCoreCLI@2
  inputs:
    command: publish
    publishWebProjects: false
    arguments: '--configuration $(BuildConfiguration) --framework $(dotNetFramework) --runtime $(targetRuntime) --self-contained --output $(Build.ArtifactStagingDirectory)/WebJob/App_Data/jobs/continuous/$(webJobName)'
    zipAfterPublish: false
    modifyOutputPath: false
    projects: '$(webJobName)/$(webJobName).csproj'

# Package the file and uploads them as an artifact of the build

- task: PowerShell@2
  displayName: Generate run.cmd For WebJob
  inputs:
    targetType: 'inline'
    script: '"dotnet $(WebJobName).dll" | Out-File run.cmd -Encoding ASCII; $LASTEXITCODE'
    pwsh: true
    workingDirectory: '$(Build.ArtifactStagingDirectory)/WebJob/App_Data/jobs/continuous/$(webJobName)'
        
- task: ArchiveFiles@2
  displayName: Zip Desired Files
  inputs:
    rootFolderOrFile: '$(Build.ArtifactStagingDirectory)/WebJob/'
    includeRootFolder: false
    archiveType: 'zip'
    archiveFile: '$(Build.ArtifactStagingDirectory)/$(webJobName).zip'
    replaceExistingArchive: true

- task: PublishPipelineArtifact@1
  displayName: Publish All Artifacts
  inputs:
    targetPath: '$(Build.ArtifactStagingDirectory)'
    publishLocation: 'pipeline'

- task: DownloadPipelineArtifact@2
  displayName: 'Download Build Artifact'
  inputs:
    path: '$(System.ArtifactsDirectory)'

- task: AzureWebApp@1
  inputs:
    azureSubscription: $(azureSPNName) #this is the name of the SPN
    appType: 'webApp'
    appName: $(azureAppServiceName) #App Service's unique name
    package: '$(System.ArtifactsDirectory)/$(webJobName).zip'
    deploymentMethod: 'zipDeploy'

标签: azureazure-pipelinesazure-webjobsazure-pipelines-yamlazure-webjobs-continuous

解决方案


变量为您提供了一种将关键数据位获取到管道的各个部分的便捷方法。问题是由于管道中的硬编码值造成的。因此,每当我们运行管道时,总是会部署相同的 WebJob。

解决这个问题的方法是用管道变量替换硬编码的值,如下所示。

webJobName: $(webJobName)
azureAppServiceName: $(azureAppServiceName)
azureSPNName: $(azureSPNName)

我们需要创建管道变量并分配值。在运行 .yml 管道之前,需要为变量分配 WebJob 所需的值。

您可以查看此定义变量文档以获取更多信息。

您还可以查看Azure Pipeline文档的此变量组。


推荐阅读