如何在VSTS构建定义中通过单个任务构建所有Azure WebJobs?
Absolutely! You don’t have to manually set up individual tasks for every new WebJob—there’s a much cleaner way to handle all of them in one go. Since you’re using .NET Core 1.1.1, here are two reliable approaches to unify your WebJobs build/deployment process in VSTS (now Azure DevOps):
1. Use Wildcards in DotNet Core Tasks
If all your WebJob projects follow a consistent naming pattern (e.g., ending with WebJob), you can leverage wildcard paths in the standard DotNet Core Publish task to target all of them at once:
- Add a new
DotNet Coretask to your build definition. - Set the Command to
publish. - For the Project(s) field, use a wildcard like
**/*WebJob*.csproj(adjust the pattern to match your project naming convention). - Configure the Output directory to a shared staging location (e.g.,
$(Build.ArtifactStagingDirectory)/WebJobs).
This will automatically publish every WebJob project that matches the wildcard, no extra tasks needed per WebJob.
2. Automate with a PowerShell Script (For Fine-Grained Control)
If you need more control over how each WebJob is packaged or deployed (like sorting them into Continuous vs. Triggered job directories for Azure Web Apps), a PowerShell script task is perfect. Here’s a sample script you can adapt:
# Set the root folder where your WebJob projects live $webJobsSourceDir = "$(Build.SourcesDirectory)/YourWebJobsRootFolder" # Grab all .csproj files for WebJobs $webJobProjects = Get-ChildItem -Path $webJobsSourceDir -Filter *.csproj -Recurse foreach ($project in $webJobProjects) { # Get the WebJob name from the project file $webJobName = $project.BaseName # Publish the project to a temp folder $publishOutput = "$(Build.ArtifactStagingDirectory)/TempWebJobs/$webJobName" dotnet publish $project.FullName -c $(BuildConfiguration) -o $publishOutput # Copy to the correct Azure WebJobs directory structure # Swap "Triggered" with "Continuous" if you're dealing with continuous WebJobs $targetDir = "$(Build.ArtifactStagingDirectory)/WebAppContent/site/wwwroot/App_Data/Jobs/Triggered/$webJobName" New-Item -ItemType Directory -Path $targetDir -Force | Out-Null Copy-Item -Path "$publishOutput/*" -Destination $targetDir -Recurse -Force }
To use this:
- Add a PowerShell task to your build definition.
- Paste the script into the Script field (or point it to a script file in your repo).
- Adjust the paths and trigger type to match your project setup.
Key Notes
- Make sure your build agent has the .NET Core 1.1.1 SDK installed (you can check this in the agent capabilities or add a tool installer task if needed).
- If your WebJobs have different configurations, you can add logic to the script to handle exceptions (e.g., specific projects that need unique publish settings).
内容的提问来源于stack exchange,提问作者kristofferandreasen

