Github actions for CI/CD of .net core app

Github actions for CI/CD of .net core app

Intoduction to Github Actions

GitHub Actions is a powerful automation and workflow tool provided by GitHub, a popular platform for version control and collaboration on software development projects. It allows developers to automate various tasks, build workflows, and streamline their software development processes directly within the GitHub platform.

With GitHub Actions, developers can create custom workflows that are triggered by events such as code commits, pull requests, issue updates, or scheduled intervals. These workflows are defined using YAML syntax and can be tailored to suit specific project requirements.

GitHub Actions provides a wide range of pre-built actions and tools that can be combined to create powerful automation workflows. Actions are reusable units of code that perform specific tasks or actions, such as running tests, deploying applications, sending notifications, or interacting with external services.

Some key features of GitHub Actions include:

  1. Event-driven automation: Actions can be triggered by a variety of events, allowing developers to automate processes based on code changes or other events within a repository.
  2. Workflow customization: Workflows can be customized to fit specific project needs. They can include sequential or parallel steps, conditionals, and triggers based on various events.
  3. Marketplace of actions: GitHub provides a marketplace where users can discover and share actions created by the community. This allows developers to leverage existing actions or contribute their own to help streamline their workflows.
  4. Integrated environment: Actions run in a virtual environment provided by GitHub, eliminating the need for developers to set up their own infrastructure. This simplifies the process of running tests, building applications, and deploying code.
  5. Continuous Integration and Deployment (CI/CD): GitHub Actions can be used for implementing CI/CD pipelines, automating the building, testing, and deployment of applications, and enabling faster and more reliable software releases.

GitHub Actions provides a seamless integration with the rest of the GitHub ecosystem, making it easy to incorporate automation into your development workflow. Whether you want to automate code testing, perform code reviews, generate documentation, or deploy applications, GitHub Actions can help you automate these processes and improve the efficiency of your software development lifecycle.

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

github actions

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 *

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site
%d bloggers like this: