GitHub Actions with CI CD pipeline for .Net app on Shared Hosting

In this article we will deploy our .Net app to shared hosting like hostgator windows service via Github actions and FTP credentials of the shared hosting server.

We will create and push our application to GitHub repository and we have to create an actions on the same repository. Create a file named “dev.yml” and provide the following context –

name: Dev - Build and deploy .Net Core app to Common Shared Servie

on:
  push:
    branches: [ "develop" ]
  workflow_dispatch:

permissions:
  contents: read

env:
  DOTNET_VERSION: '6.0.x' 
  

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Set up .NET Core
        uses: actions/setup-dotnet@v2
        with:
          dotnet-version: ${{ env.DOTNET_VERSION }}

      - name: Set up dependency caching for faster builds
        uses: actions/cache@v3
        with:
          path: ~/.nuget/packages
          key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
          restore-keys: |
            ${{ runner.os }}-nuget-

      - name: Build with dotnet
        run: dotnet build --configuration Release

      - name: dotnet publish
        run: dotnet publish -c Release -o ${{env.DOTNET_ROOT}}/myapp

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v3
        with:
          name: .net-app
          path: ${{env.DOTNET_ROOT}}/myapp
    

  deploy:
    permissions:
      contents: none
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: 'Development'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v3
        with:
          name: .net-app

      - name: Sync files
        uses: SamKirkland/FTP-Deploy-Action@v4.3.4
        with:
          server: ${{ secrets.FTP_SERVER }}
          protocol: ftp
          port: 21
          username: ${{ secrets.FTP_USER }}
          password: ${{ secrets.FTP_PWD }}
          exclude: |
            **/.git*
            **/.git*/**
            **/node_modules/**
            *.exe
            appsettings.json
            appsettings.development.json
            web.config

We also have to create few Environment variable on GitHub for FTP credentials.

FTP_SERVER 
FTP_USER 
FTP_PWD 

Now onward whenever you push your code on “develop” branch it will trigger the build and after completing successful build of your app it will push the build to your shared hosting server.

Related posts:

Leave a Reply

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