Setup Own/Custom Agent Pool And Run as Service Using Virtual Machine

(Neither Default Nor Microsoft Agent Pools)

 

Create Virtual Machine

To set up a self-hosted agent, we need a VM for that, a virtual machine 

Open Portal.Azure.com

  • Create a Virtual Machine
    • Create Resource Group Name (devops-rg)
    • Give VM name (devops-vm)
    • Credentials
    • Check both HTTP and SSH as inbound ports
    • Other settings as
    • IMP: So whatever resources you're creating on cloud, as soon as you're done with your practice, make sure you delete it.

 

 

 

Create Custom Agent Pool

  • Agent pool can be created at the organization level and at the project level also
  • If you create an organization level, the agent pool will be available to all the projects. If you create a project level it will be available to this project. Later, if you want to share it with some other project, even that is possible.
  • We are creating at project level
  • Go to Project Setting
    • Show two type of agents
      • Azure Pipelines - MS Hosted Pipelines
      • Default- All Self Hosted Agent will be listed **

 

Add/Insert Custom Application Pool

  • Don't click either option Azure pipelines and Default. lets create our own Agent Pool
  • Click ‘New Pool’ Button
    • Click Create button
    • A new Pool is created and listed in Agent Pool
      • Click linux-AP
        • A pool is nothing, but it's a bucket in which you will be creating your agents. all your VMs will be added to these. These pool.
        • The benefit of using this pool is you get multiple options. You can have your pool details, you can manage the security of your agent. From here you can add approvals directly at your agent. So if any any task is executing using this pool, you can have approvals added that needs to be approved. You can look for the analytics, how the pool is getting consumed and things like that.

 

 

Check your VM at your local

  • Go to VM just created in protal.azure. copy Public Ip Address
    • For Mac user SSH
    • For windows use Git Shell
      • Open Git shell (Use Public IP of your VM)
        • ssh <vm_username>@<vm-public_ip>
        • Ask for finger print type yes
        • you are connected nos
      • Check Host name

 

 

Add Agent

 

Run your local agent

 

 

Cross check Agent Pool Connection in DevOps

 


Problem with Self Hosted Agent

Now, the problem with this agent is this agent is running on the terminal right now. If I cancel this, for example, if I do this cancel, My agent will also get stopped.

 

Solution (Run as Service using your Virtual Machine)

we want this agent to run in the background as a service so that even even If we log out from this terminal, my agent should run behind the scene.

If a server restarts, our agent should execute automatically. That requires running this agent as a service.

Benefit: go back to DevOps, you can see your agent running. Now, even though if I exit out of this terminal, my agent will still keep on running in behind the scene.

  • in Mac
    • Create a symlink 
      • Create a symlink for the service we are supposed to create.
        .svc.sh install <VM Username>
    • Run the Agent as a service
      • ./svc.sh start
    • Check Status
      • ./svc.sh status
    • Stop your self hosted agent
      • ./svc.sh stop
    • Uninstall self hosted agent
      • ./svc.sh uninstall
    • Remove Config
      • ./config.sh remove
        it will ask you for your path token. (regenerate from DevOps)
    • This way the complete cleanup will be done. Go to DevOps > Agent and refresh this page, your agent will be gone. Also delete your VM and related resources from portal.azure.com

 

  • In Windows
    • Running Agent as service in Windows (No VM)
    • In Windows use .cmd instead of .sh in all commands
    • Download Agent (from DevOps), follow instruction, create folder, extract file 
    • Uninstall self hosted agent
      • Cleanup is also easy in windows. Just run the last command. Everything will be removed.
        • ./config.cmd remove
        • Remove Agent folder from your local

 


Related Question