Skip to content

Instantly share code, notes, and snippets.

@rfennell
Last active August 30, 2024 09:12
Show Gist options
  • Save rfennell/f2f691e130f5ecfab0ae006f4d6c3ae2 to your computer and use it in GitHub Desktop.
Save rfennell/f2f691e130f5ecfab0ae006f4d6c3ae2 to your computer and use it in GitHub Desktop.
Azure DevOps Custom Maintenance Jobs

These blocks of Azure DevOps Pipeline YAML can be used to scan for all the active build agents on an Azure DevOps instance and trigger a build on each.

The setup of the permissions as to how to setup these pipeline is detailed in this post which is an updated version of this post

# This is the pipeline that runs any scheduled maintainance jobs
# we wish to run in addition to the built in Azure DevOps Maintainance jobs
# The parameters to target each pool and agent
parameters:
- name: pool
- name: agent
- name: nvdapikey
# We cannot use to the parameters directly else we get a 'A template expression is not allowed in this context'
# However, if we alias them with a variable they work
variables:
- name: pool
value: ${{parameters.pool}}
- name: agent
value: ${{parameters.agent}}
- name: nvdapikey
value: ${{parameters.nvdapikey}}
trigger: none
pool:
name: "$(pool)"
demands: Agent.Name -equals $(agent)
steps:
# wherever steps up wish to run on each agent
# See https://blogs.blackmarble.co.uk/rfennell/how-to-run-your-own-maintainance-job-on-azure-devops-pipelines/
variables:
# filter to limit the scope of agent pools to consider
- name: PoolNamePrefix
value: "BM-"
# the pipeline to run on each agent
- name: BuildDefintion
value: "ScheduledMaintenanceBuild"
trigger: none
schedules:
- cron: '0 0 * * *'
displayName: Daily midnight build
always: true
branches:
include:
- main
pool:
name: BM-VS2022
steps:
- task: AzureCLI@2
inputs:
# The Azure Subscription is the link to a Service Principle
# with permission to access the agent pools and queue builds
azureSubscription: 'ScheduledMaintainanceJobs'
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: |
write-host "Find the agent pools with the prefix '$(PoolNamePrefix)'"
$Pools = $(az pipelines pool list --organization $(System.TeamFoundationCollectionUri) --query "[? (starts_with(name,'$(PoolNamePrefix)'))].[id,name]" --output tsv)
write-host "$($Pools.count) found"
foreach ($pool in $pools) {
# ugly but works with tsv format data
$poolSplit = $pool.Split("`t")
$poolID = $poolSplit[0]
$poolName = $poolSplit[1]
write-host "Find the agents the pool '$poolName'"
$Agents = az pipelines agent list --organization $(System.TeamFoundationCollectionUri) --pool-id $PoolID --query "[?enabled].name" --output tsv
foreach ($Agent in $Agents) {
# trigger a build that uses agent-scheduled-job-yml
$buildNameid = $(az pipelines run --organization $(System.TeamFoundationCollectionUri) --name $(BuildDefintion) --project $(System.TeamProject) --parameters "pool=$PoolName" "agent=$Agent" "nvdapikey=$(nvdapikey)" --query "name" --output tsv )
Write-host "Queued build $buildNameid on $agent"
}
}
displayName: 'Trigger maintainance builds on all active agents'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment