How to Auto-Deploy Source Code to Web Server via Webhook
Tags: webdev, programming, github, tutorial
Published: 2024-01-29 05:41:49

If you use GitHub to manage the source code of your website, you can utilize webhooks to transmit GitHub events to third-party services. In this article, we will explore how to deploy a website to IIS, as well as how to pull the source code and automatically update the site when a push event occurs. 

## IIS Configuration in Windows 10
 
To activate IIS, go to the "Windows features" menu. By default, all "Application Development Features" are turned off. Choose the necessary features. 

![Configure IIS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pvk57ipomdk5t4091hxx.png)

## How to Use Visual C# to Handle HTTP Request
 
In Visual Studio, create an empty ASP.NET web application. 

![ASP.net application](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k6nnqy27yhn9pfhxrszj.png)

Create an HTML file called index.htm. 
Webhook.ashx should have a generic handler. 

![Webhook generic handler](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/63qz349s0pinyhzqcm8h.png)

Publish the project. 

![Publish Project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhok0qwlfqkfe2oa15ld.png)

Select IIS > File System. 

![Selecting IIS file system](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jb3he0fn8j7j9dun503u.png)

To add the website, launch IIS. 

![Launch IIS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iggf96a3b8alkjiibeul.png)

To see if the URL works in your web browser, go to [http://localhost/Webhook.ashx](http://localhost/Webhook.ashx). 

![Webhook browser URL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qjhmj42cc6stdjbumjn0.png)

The following step is to execute the git pull command in C#. We can refer to the CodeProject sample code. 

![Codeproject sample code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5umkptezs5t7ysgjm5mj.png)

A batch file to execute required commands.: 

`batchFile = Path.Combine(context.Server.MapPath("."), "github.bat"); 
objThread.Start(batchFile);`

The physical path of the project is **d:\iis**, so the **github.bat** is as follows: 

`d: 
cd d:\\iis 
git pull `

## How to Expose the Local Web Server to the Internet
 
We must give a proper URL to use GitHub webhooks. Ngrok is an excellent tool for connecting a local webserver to the internet. 

`ngrok http 80` 

![Connect local server to internet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eayc591ifalxn3gfv53t.png)
The webhook URL is now [https://048dab0c.ngrok.io/Webhook.ashx](https://048dab0c.ngrok.io/Webhook.ashx). 

## How to Use GitHub Webhook to Trigger the Auto-Deployment
 
Once the URL is complete, we may create a new GitHub repository. 

![Create github repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9pbwdsqyfltzph3ael04.png)

Add `d:\iis` to the remote repository and push it. 

To add the payload URL, go to `Settings > Webhooks and enter`: 

![Add payload URL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dwnn8cnlmfegbais2d2z.png)

Clone the repository to a new location or another computer. Make some changes to the index.html file and commit them to GitHub. 

The web server will immediately receive an HTTP request and execute the 'git pull' operation. 

To see the update, refresh the website [https://048dab0c.ngrok.io/](https://048dab0c.ngrok.io/).