Canarys | IT Services

Blogs

Deploy .NET application on IIS using GitHub actions

Share

In this blog I will be showing how to deploy a .net application on IIS server (self-hosted runner). Once the web application is developed and ready to be made available to the user. To enable the user to access the application, the web application needs to be deployed on a Web server, such as IIS.

What is IIS server??

IIS is a Web server that provides a complete platform, which helps you to host and manage web applications. IIS is a proprietary by Microsoft and is available as a package along with the Windows operating system. By default, IIS feature is turned off when Windows is installed. Hence, before deploying an application on IIS, you need to enable IIS on the Windows operating system.

Deployment using GitHub Action

Before we start with the deployment process, we must complete the build process and store the build artifact. Once we are done with the CI part, we will start with CD process.
There are 2 pre-requisites on the deployment machine
•    IIS should be enabled 
•    webadministration module should be imported using powershell
•    Make sure that the GitHub Action Runner agent uses Log on method as “local system account” for the agent service

 

To deploy the application, we will be using PowerShell script by passing certain values as argument to the script. The script required to deploy the application is stored as a .ps1 file in the repository and is made available as a part of the generated artifact in the CI process.

Below is a sample snippet which shows how to download the generated artifact using actions from marketplace and the execution of the PowerShell script along with the arguments.

jobs:
  Build:
    runs-on: windows-selfhosted
    needs: Build
    steps:
    
    - name: Download a Build Artifact
      uses: actions/download-artifact@v2.0.5
      with:
        name: Parts
        path: ${{github.workspace}}
   - name: Run Powershell
      run: |
        & '${{github.workspace}}PartsUnlimited-aspnet45app_pool.ps1' -pool_name ${{env.app_pool_name}} -site_name ${{env.site_name}} - packagepath ${{env.contentpath}}

In the above snippet I will be passing app_pool_name, site_name and contentpath as parameters to the PowerShell script. All the values to the parameters will be set using environment variables in workflow file.

Below are the contents of the powershell file

param ($pool_name, $site_name, $packagepath)
Reset-IISServerManager  -Confirm: $false 
#check if the app pool exists
if  ((Get-IISAppPool).name -eq $pool_name )
{
    echo "----------------------------"
    echo "|     Pool exists          |"
    echo "----------------------------"
}
else  {
    New-WebAppPool -Name $pool_name -Force
}

Reset-IISServerManager -Confirm:$false
#check if the site exists
if ((Get-IISSite).name -eq $site_name)
{
    echo "----------------------------"
    echo "|     Site exists          |"
    echo "----------------------------"
}
else {
    New-Website -Name $site_name -ApplicationPool $pool_name -Force -PhysicalPath $packagepath -Port 8083
}

Second line of the script accepts the values that we had passed from the RUN POWERSHEL action from the workflow file. Next, script will check whether the application pool and site exist and based on the condition it will create the pool and site respectively. Here I have hardcoded the port number instead you can also pass this value as parameter by adding one more argument.

Leave a Reply

Your email address will not be published. Required fields are marked *

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.