How to use GitHub Actions to deploy code in Window Virtual Machine

How to use GitHub Actions to deploy code in Window Virtual Machine

Deploy source code to IIS via GitHub Action in Virtual Machine

If you are using GitHub to manage your source code, you can use GitHub self-hosted runners to automate. In this article, I will share how to deploy the website to IIS so we can set up auto deployment for any private virtual machine or virtual private server. It's pretty easy. Let's start.


  • Open Project repository in GitHub
  • Go to the Settings tab


  • Click on the Runners menu from the Action menu. so we can set up the self-hosted runner.


  • Click on the New self-hosted runner button, Which will open another page


  • Select Windows operating system and appropriate Architecture from below dropdown


  • Now time to open Virtual Machine, Go to your Window Virtual Machine and open PowerShell and go to the root directory in C: drive, and follow the commands displayed on your current GitHub page step by step.


  • It will create actions-runner folder in C: drive
  • While executing it may ask to set "Label", Which will be required later on. so please set it. Later on, it will display on the GitHub Runners page.

Note: If you setting up multiple hosted runners then you need to set unique folder each time

  • After completing the step, reload the page and you will get a runner as "Idle". that means we are ready for the next step


  • Now open the “Actions” tab and create a new workflow here is the sample code for the .yml file (You need to change env variables only if you are using for .NET for another tech you need to change in the build step also)

name: "Build and Deploy - Self Hosted"

      - master

  GITHUB_WORKING_DIRECTORY: 'IISAutoDeploy'             # If you have a root folder in the GitHub code set otherwise leave it blank
  IIS_WEBAPP_PATH: 'C:\HostingSpaces\admin\AutoDeployTest'   # Web App Path from Virtual Machine         
  DOTNET_VERSION: '5.0.x'           # set this to the dot net version to use

    runs-on: self-hosted 

      - uses: actions/checkout@v2

      - name: Setup .NET Core 
        uses: actions/setup-dotnet@v1
          dotnet-version: ${{ env.DOTNET_VERSION }}

      - name: Install dependencies
        working-directory: ${{ env.GITHUB_WORKING_DIRECTORY }}
        run: dotnet restore

      - name: Build
        working-directory: ${{ env.GITHUB_WORKING_DIRECTORY }}
        run: dotnet build --configuration Release --no-restore 

      - name: Publish
        working-directory: ${{ env.GITHUB_WORKING_DIRECTORY }}
        run: dotnet publish --configuration Release --output 'dotnetcorewebapp'

      - name: Upload a Build Artifact
        uses: actions/upload-artifact@v2.2.1
          name: application
          path: '${{ env.GITHUB_WORKING_DIRECTORY }}/dotnetcorewebapp'

    needs: build
    runs-on: self-hosted

    - name: Take application offline
      run: New-Item -Type File -Name app_offline.htm -Path ${{ env.IIS_WEBAPP_PATH }} -Force

    - name: Set Offline HTML file
      run: Set-Content ${{ env.IIS_WEBAPP_PATH }}\app_offline.htm 'Site Under Maintenance'

    - name: Download new binaries over the top of the app
      uses: actions/download-artifact@v2
        name: application
        path: ${{ env.IIS_WEBAPP_PATH }}

    - name: Bring the app back online
      run: Remove-Item ${{ env.IIS_WEBAPP_PATH }}\app_offline.htm
  • Now the setup is done. You can run GitHub Action Manually from the Actions tab from GitHub. If you want to set up based on the trigger like on push or on pull request merge please refer to the below link. It is quite easy.