在YAML管道中将多个Azure webjob部署到单个Azure AppService



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

在Azure pipeline中,我有以下脚本,可以成功地为单个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'

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

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

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

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

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

您还可以为Azure Pipeline文档检查此变量组。

最新更新