I'm able to register my existing Azure VM as build agent in VSTS. Build works fine but I would like to turn the machine on and off only if there is any work for it. Is there any way how I can turn on the VM if I see there is some work for it in a queue and then turn it off if the queue is idle for let's say 5 minutes?
The Start/Stop VMs during off-hours feature start or stops enabled Azure VMs. It starts or stops machines on user-defined schedules, provides insights through Azure Monitor logs, and sends optional emails by using action groups.
Schedule VMs to start and stop in ascending order by using Azure Tags. This activity is not supported for classic VMs. Autostop VMs based on low CPU usage. It manages VMs in any region, but can only be used in the same subscription as your Azure Automation account.
For start action, use External_Start_ResourceGroupNames, and use External_Stop_ResourceGroupNames for stop action. VMs are automatically added to the start and stop schedules.
Create tags named sequencestart and sequencestop on each VM for which you want to sequence start/stop activity. These tag names are case-sensitive. The value of the tag should be a positive integer (1, 2, 3) that corresponds to the order in which you want to start or stop. VMList: Comma-separated list of VMs.
Add a hosted agent job to the start to start the VM and add an agent job or additional step to the end to shut down the VM when finished.
Add an Agent job to run this as hosted to start the VM. Then use all build steps as self-hosted(private).
Add your Azure subscription
Choose Inline Script to add the following to start it with VM name and resource group
start-AzureRmVM -Name ""-ResourceGroupName ""
Add another Agent Job or additional step as the last Step To shut down the VM when the build has finished. With the following Script
stop-AzureRmVM -Name ""-ResourceGroupName "" -Force
I'm wondering if this could be helpful for you because this is not exactly what you asked for. I used REST API calls before the build and after the build to start and then stop a specific VM. See how to start and power off a VM in the API documentation.
I created an agentless job as the first step, with a task entitled Invoke REST API. The task lets you authenticate to your Azure account, so you don't have to handle that manually. All you have to do is specify the URL suffix. For instance, to start a VM named MyVm, you add a suffix similar to the following one:
/subscriptions/subscription-id/resourceGroups/resource-group-name/providers/Microsoft.Compute/virtualMachines/MyVm/start?api-version=2018-06-01
where the subscription ID and resource group can be verified in the Overview page of your virtual machine in Azure.
After the build you can add another agentless job, but this time with a REST call of the powerOff endpoint:
/subscriptions/subscription-id/resourceGroups/resource-group-name/providers/Microsoft.Compute/virtualMachines/MyVm/powerOff?api-version=2018-06-01
There isn’t the good way to start/stop azure VM when build in VSTS. Regarding WebHook, there isn’t the event for queue build, for other events, they aren’t meet many requirements (e.g. non-CI build, queue build manually)
I recommend you use Hosted agent, with free Hosted Pipeline, you get 4 hours (240 minutes) per month and maximum duration of 30 minutes per build or deployment in Team Services.
How to buy more pipeline capacity for builds and releases in Visual Studio Team Services
Workaround:
More information, you can refer to: How to Create a Monster Build Agent in Azure for Cheap
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With