Configuring Self-Hosted Agent and understanding pipeline styles the easiest way.
Tags: devops, azure, programming, github
Published: 2024-02-20 16:45:20

## How to create an Azure DevOps YAML pipeline
We will learn how to create a template based YAML pipeline in Azure DevOps.
- Open your project by clicking on it.
- Click on Pipelines
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qz9kx7tm2cl53l36zj5d.png)
- Click New pipeline
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdac5c6pdommowik15fp.png)
- click Azure Repos Git on where is your code? pane
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ts240n5kzwl5m1cgmrbp.png)
- click on the project name under select a repository pane
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3yrsu5wob1zwctezvnvo.png)
- Select Existing Azure Pipelines YAML File under Configure your pipeline pane.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5htjr8d17xsiag2qsk1.png)
- Change the branch name to main and choose a path for your template e.g /.ado/eshoponweb-ci-pr.yml
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qrbju9gfn7xn2t37wlqj.png)
- Click continue
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ghesvntv0j5xmmiosw1.png)
- Examine the YAML file and click the down arrow beside Run button to save the YAML file.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u6zsysaizm848kvcrmgb.png)
Note that this will only create pipeline without running it.

## How to set up an Azure DevOps agent pool.
- in your project project, click User settings at the upper right corner and select personal access tokens.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2d30xvt8p7dao8twbarl.png)
- Select +New Token
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onbh1xhniz7gdsjp8kfx.png)
- Give the name to the token, select Agent pools scope and choose Read and manage permission, click create after.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gf53eqwvv9m1rcrjra2o.png)
- Copy the token in the success pane for future use.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5lpdgpdf0hcm3kis8vo1.png)
- Go back to the organization by clicking Azure DevOps symbol in the upper left corner and then click organization settings in the lower left corner.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f4cckt6sf9ly37ftwp2b.png)
- Select Agent pools under pipeline section and click Add pool.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4hc5iodsozx4fdpy7sa.png)
- Select Self-hosted in the pool type dropdown, give a name to the agent pool and click create
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ghwglqvm3b2v0p24c4y.png)
- Click on the the newly created agent pool in the Agent pools pane to open it.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0qcgdn7ucy681y3b3o4i.png)
- Select New agent under Job tab
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wbhx6mquj85y21xiyznn.png)
- Make sure that Windows and x64 tabs are chosen in the Get the agent pane and select Download. A zip archive containing the agent binaries will be downloaded your local download folder.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qm0lvbu0qo95lpv798n.png)
- Open the powershell console by clicking start button and search for powershell. Select the powershell.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f4b0j8songqkp9mmhjxo.png)
- Run this command to create a directory called agent, change the directory to agent and also extract the archive file downloaded   
 
cd \mkdir agent ; cd agent
 
Add-Type -AssemblyName System.IO.Compression.FileSystem ; [System.IO.Compression.ZipFile]::ExtractToDirectory("$HOME\Downloads\vsts-agent-win-x64-3.234.0.zip", "$PWD")
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/12lnfi9ovdk6qhf235e6.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oe5eo0ztqulzywfg7eq7.png)
- We are to configure the agent with this command in the powershell console  
 .\config.cmd

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kw3rumyvek09l8q8c7cy.png)
- Enter server URL: the URL of your Azure DevOps organization, in the format https://dev.azure.com/organization_name, where organization_name represents the name of your Azure DevOps organization.  
- Enter authentication type (press enter for PAT): Enter.  
- Enter personal access token: The access token you recorded earlier in this task.  
- Enter agent pool (press enter for default): az400m03l03a-pool.  
- Enter agent name: az400m03-vm0.  
- Enter work folder (press enter for _work): Enter.  
- (Only if shown) Enter Perform an unzip for tasks for each step. 
- (press enter for N) WARNING: only press Enter if the message is shown.  
- Enter run agent as service? (Y/N) (press enter for N): Y.  
- enter enable SERVICE_SID_TYPE_UNRESTRICTED (Y/N) (press enter for N): Y.  
- Enter User account to use for the service (press enter for NT AUTHORITY\NETWORK SERVICE): Enter.  
- Enter whether to prevent service starting immediately after configuration is finished? (Y/N) (press enter for N): Enter.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u8dt3ebmh796wy2go3u9.png)
- After configuring the agent, go back to your DevOps portal, change the tab to Agent and you will see your newly configured agent with online status.
 Note that if your Agent is displaying offline, you can change the status to online with this command:  .\run.cmd in the Agent directory of the powershell.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x81itv3u8i9dg69aatrh.png)
- Go back to your project by clicking DevOps symbol and select the project.
- Click pipelines in the pipelines section.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uiyp0wkl0qvwa3t9h5dq.png)
- Click on your pipeline to open it
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/opavokmekm1db615l523.png)
- Click Edit
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p32cpfq0rthpf2kqfs50.png)
- Replace vmImage: windows-latest under pool with the agent pool created.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j3qclcljnc754m9nms8b.png)
- Click save 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tfuqtsfpefobrwenzow4.png)
Click save in the save pane
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5c35ixa75td7tqwvm0ao.png)
- Click Run
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fym13wu95g37x0mhkt8o.png)
- Click Run in the Run pipeline pane
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8kg1x4sep30wyw4n60p.png)
- Click pipelines to see the the pipeline that is running and click view to grant permission
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zykwl62xnap2rw9z6r8p.png)
- Click permit in waiting for review pane
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y7410jf8i6477p3bikxc.png)
- Select permit
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jt6cqp4gq1e8sw78s1yh.png)
- Monitor the job until it is successful
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/68h3d5f7qhancqn3m5gn.png)
- If you click on the job to open the view raw log, You will see it run with the the pool and Agent we created.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kit0kuz870plob5fqr53.png)