How to enable Continuous Integration with Azure Pipelines
Tags: github, devops, development, webdev
Published: 2024-02-21 19:07:17

## How to include build validation to validate a Pull Request
- Firstly, we need to import a Git repository to work with using this link: https://github.com/MicrosoftLearning/eShopOnWeb.
- Import a build pipeline called eshoponweb-ci-pr.yml by clicking pipelines in your project
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ur11uin9a6inu1yjyi6e.png)
- Click Create pipeline
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/24nmls5vv4xg3ijn8aq0.png)
- Choose Azure Repos Git (YAML)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8yy5w28dh6j29wa7dx2f.png)
- Choose your repository
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1sryi2lpv5ks0sx6zwod.png)
- Choose Existing Azure Pipelines YAML File
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uif4znrig4wq1lusoz83.png)
- Choose /.ado/eshoponweb-ci-pr.yml
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fcngj03tvr1vhpbhcf3l.png)
- Save the pipeline definition by clicking save
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g54idvuiextuf8htvzi6.png)
- Go back to your newly created pipeline by clicking pipeline under Pipelines section and rename the pipeline. Note that the new pipeline will assume the name of the project
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/87sl82rrj5kpv5a181j8.png)

## How to Add Branch policies
- Click Repos and select Branches
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1rfk5gfec2966r0h6tk.png)
- Click the ellipsis button in front of the main branch among the list of branches and select Branch policies
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/81hveowjd4sgs3heq8le.png)
- Enable Require minimum number of reviewers, change Minimum number of reviewers to 1 and tick Allow requestors to approve their own changes in the Branch policies pane.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kcs6t0dcv8s651hapyvq.png)
- Scroll to Build validation section, click +, select the pipeline created earlier, leave other settings as default and click save.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s40s63tvvtr0zqpwd7hd.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8okzn038fq5ixm0m3xy.png)

## How to work with Pull Requests
- Click on Repos and select branches
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6k862kjksex81dzozkj3.png)
- Click New branch to create a branch based on the main branch
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1z4q8ffog1w5y3cgpstc.png)
- Click on the newly created branch to open it
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8npvuhemzik9o474yhkt.png)
- Look for /eShopOnWeb/src/Web/Program.cs and click it to open it
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uz9khkgidmfdkc3srfvo.png)
- Click Edit
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f9j16i4poya79tnh8hq8.png)
- Add this  // Testing my PR to the first line and select Commit
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/netfey4gdkzwtw8rt09y.png)
- Click commit
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t83gq8gpmq68bt79n0v6.png)
- Click Create a Pull Request.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jcor4kzu8p8aci02w685.png)
- Provide a title for the pull request and leave other settings as default.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ik9f0kxpy3ef79u3rtpj.png)
- Click Approve to approve the pull request.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xu5lypzcmm25yg8b6o6h.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2syfpvzjudwef5z0yd4s.png)
- Click on complete
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v3mp3xhvwcn5eutyndsr.png)
Click complete merge
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/57alkvo1kg82u23npwvc.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ydhf5tlsq4kkafrep0ka.png)

## How configure CI Pipeline as Code with YAML
- We need to create a new pipeline by importing the CI pipeline named eshoponweb-ci.yml
- Import a build pipeline called eshoponweb-ci.yml by clicking pipelines in your project
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ur11uin9a6inu1yjyi6e.png)
- Click Create pipeline
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/24nmls5vv4xg3ijn8aq0.png)
- Choose Azure Repos Git (YAML)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8yy5w28dh6j29wa7dx2f.png)
- Choose your repository
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1sryi2lpv5ks0sx6zwod.png)
- Choose Existing Azure Pipelines YAML File
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uif4znrig4wq1lusoz83.png)
- Choose /.ado/eshoponweb-ci.yml and click continue
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/slsxw51mkidggehj2mzk.png)
- Save the pipeline definition by clicking save
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g54idvuiextuf8htvzi6.png)

## How to enable Continuous Integration
- Click Edit to edit the pipeline
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/riwdy5bvz1i8prmx1pjb.png)
- Replace the first 3 comments with:  
trigger:
   branches:
     include:
     - main
   paths:
     include:
     - src/web/*
- Click save
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9vzhynek3ona6cw6cxr.png)
- Tick Create a new branch for this commit, leave the default branch name and tick Start a pull request. Click save after
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4b1efq0uvdnf007amnh.png) 
- Rename the pipeline by going to pipelines, click the ellipsis button in front of pipeline and click rename.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rzeuit40wj5raf7jvzcb.png)
- Provide a new name and click save
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h28sm2ou9iqifkys8ep1.png)
- Click Repos, select pull request and click Update eshoponweb-ci.yml for Azure Pipelines
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vvvgi8hw371w42riw6f4.png)
- Click Approve to approve the pull request
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/349mt8956ztqql7bwd46.png)
- After 1 reviewer has approved, click complete
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jd2q50zkcydhqlmayvdd.png)
- Click complete merge in the Complete pull request pane.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0bmiias0jwf2gog077ku.png)

## Testing the CI pipeline
- Create a new branch on the main branch by clicking Repos and selecting branches
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5lk98bd2inl9xtd2fk8x.png)
- Click New branch
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kcdbqdek39gmbpnyhpkh.png)
- Provide a name for the branch and click create.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/obzam25oqfe71ipaw26h.png)
- Click on the new branch to open it and look for /eShopOnWeb/src/Web/Program.cs file
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y8tbt9y0ioodfrzytwq1.png)
- Click Program.cs to open it and click Edit
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ap8uftz411y3xyqv1tzq.png)
- Delete  // Testing my PR on the first line we added before and click commit
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iqjlnbzedqx9ldo2jv3y.png)
- Click Commit
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qaw7of1mehiksf6op52p.png)
- Click create a pull request
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j58l9w4ame2rps3avfvw.png)
- In the New pull request pane, leave all settings as default and click create
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gf3urzf3p52jlbmcq63n.png)
- A pane showing at least one reviewer must approve will be displayed because of branch policies applied to the main branch.
- Click approve
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vm7pxd6sgha3t4xxehgp.png)
- Click complete after 1 reviewer has approved
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rqly0u0b6ezwb77df3z6.png)
- Select complete merge
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/klm8oix860jmgtl8u2ps.png)
- Going back to our pipelines, we will see that the renamed pipeline was automatically triggered after the code was merged
- Select the pipeline and click build under Jobs
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/07ivtgjow4x4myzv781b.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/haxzg2xo2tr74w4bvuik.png)