DEV Community

Cover image for How to Auto-Deploy Source Code to Web Server via Webhook
Eric Parker πŸ₯‚
Eric Parker πŸ₯‚

Posted on

How to Auto-Deploy Source Code to Web Server via Webhook

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

How to Use Visual C# to Handle HTTP Request

In Visual Studio, create an empty ASP.NET web application.

ASP.net application

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

Webhook generic handler

Publish the project.

Publish Project

Select IIS > File System.

Selecting IIS file system

To add the website, launch IIS.

Launch IIS

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

Webhook browser URL

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

Codeproject sample code

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
The webhook URL is now 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

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

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

Add payload URL

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

Top comments (0)