Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically start/stop Existing Azure VM as build agent in VSTS

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?

like image 440
milanio Avatar asked Sep 04 '17 06:09

milanio


People also ask

What is start/stop VMS during off-hours in azure?

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.

How do I schedule a VM in azure automation?

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.

How to add a VM to the start and stop schedule?

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.

How do I start and stop a VM activity in vmlist?

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.


3 Answers

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).

enter image description here

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
like image 191
Inkey Avatar answered Oct 18 '22 00:10

Inkey


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

like image 23
Mariusz Schimke Avatar answered Oct 17 '22 23:10

Mariusz Schimke


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:

  1. Queue build at the specify time, for example, 7:00 am on Monday and Thursday, then you can auto-start and auto-shutdown the VM
  2. Run on Hosted agent (Execute Powershell to start VM)=> Run on private agent

More information, you can refer to: How to Create a Monster Build Agent in Azure for Cheap

like image 1
starian chen-MSFT Avatar answered Oct 17 '22 22:10

starian chen-MSFT