Github actions for CI/CD of .net core app

Github actions for CI/CD of .net core app

Hi guys,

I was wondering if there was any confusions on github workflow. So I decided to add some details on it step by step

Breaking Down of the workflow

Starting with the top section of the workflow

name: Build and Deploy Our Web Application

This above block is to give name of the workflow, you can give any name here.

on: 
  push:
    branches:
      - master
      - main

This section defines what trigger would start this workflow. In this case, we have trigger push on branch master or main. This means that any push on either master or main will start the workflow. There are many triggers that would start the workflow, like, issue created, updated or PR created, updated, closed, etc List of github action triggers

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

Jobs here has the name/id as build-and-deploy. We can have multiple jobs for the workflow as per our requirement. But, In this case, we just have 1 job that is defined as build-and-deploy which runs on ubuntu-latest.

- name: Step 1 => Checkout Branch
  uses: actions/checkout@master

Steps 1

is to checkout the master branch. It used the master branch of the github repo actions/checkout. The repository in the actions are github actions. So if you want to make yours actions, you are free to do it. There are certain guidelines in order to create our own actions. Create your own github actions

- name: Step 2 => Setup nuget cli
  uses: nuget/setup-nuget@v1.0.5

Step 2

It is all about setting up nuget so that we can install the nuget package for our .net projects.

- name: Step 3 => Steup .net sdk
  uses: actions/setup-dotnet@v1.4.0
  continue-on-error: false

Step 3

It is about setting up .net sdk so that we can run the cli version of dotnet to build and publish the project. Here we have continue-on-error set to false, which basically means that if there is any error in this step the workflow will not proceed further.

- name: Step 4 => publish 
  run: dotnet publish ./Demo-WebApp-2021/ -c Release
  continue-on-error: false

Step 4

It is about publishing the release version of our web project. Here we are not using the uses, instead we are using run, which basically helps us to run the shell script command. The command here creates a release version of the project.

- name: Step 5 => Upload Artifcat
  uses: actions/upload-artifact@v1.0.0
  with: 
    name: v1
    path: ./Demo-WebApp-2021/bin/Release/net5.0/publish
  continue-on-error: false

Step 5

It is about uploading the published content to the github artifacts. The content in the location provided is zipped and published in the artifacts sections.

- name: Step 6 => publish the app
  uses: azure/webapps-deploy@v2
  with:
    app-name: azurebootcamp2021
    publish-profile: ${{ secrets.APPPUBLISH }}
    package: './Demo-WebApp-2021/bin/Release/net5.0/publish'

Step 6

It is about publishing the published contents to the azure web app service. All we need is App Name, contents of published profile stored as a secrets in github and the published packages.

Compiling together

So finally combining the files all together we achieve this workflow

name: Build and Deploy Our Web Application

on: 
  push:
    branches:
      - master
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
    - name: Step 1 => Checkout Branch
      uses: actions/checkout@master
      
    - name: Step 2 => Setup nuget cli
      uses: nuget/setup-nuget@v1.0.5
      
    - name: Step 3 => Steup .net sdk
      uses: actions/setup-dotnet@v1.4.0
      continue-on-error: false
      
    - name: Step 4 => publish 
      run: dotnet publish ./Demo-WebApp-2021/ -c Release
      continue-on-error: false
      
    - name: Step 5 => Upload Artifcat
      uses: actions/upload-artifact@v1.0.0
      with: 
        name: v1
        path: ./Demo-WebApp-2021/bin/Release/net5.0/publish
      continue-on-error: false
    
    - name: Step 6 => publish the app
      uses: azure/webapps-deploy@v2
      with:
        app-name: azurebootcamp2021
        publish-profile: ${{ secrets.APPPUBLISH }}
        package: './Demo-WebApp-2021/bin/Release/net5.0/publish'

I hope this helps you creating your first workflow. There are many things in github workflow which I will be adding in future as per demand. Let me know what type of workflow are you expecting. I will be happy to help you out.

Good day folks and Stay Safe.

Leave a Reply

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

Please disable your adblocker or whitelist this site!

%d bloggers like this: