Tuesday, August 31, 2021

Building a .Net Core project with GitHub Actions

GitHub is one of the well-known version control tool which supports collaborative work among teams. Further, GitHub actions added as latest feature to GitHub to support automation of software delivery life cycle. This blog explains how to setup a build pipeline for .Net core project.

Pre- requisites:

  • .Net Core Project pushed to GitHub Repository
  • GitHub account

Let's get started.

Go to Actions tab in GitHub and select the existing workflow to setup a pipeline or setup work flow from the scratch. This blog explains how to setup a pipeline by utilizing .NET workflow. So, Select .NET workflow.


Once you select the workflow, build YAML file will be added. Now you can edit the build YAML file as required. Further, let's try to understand each section of the code.

Name of the workflow

name: .NET Core

This section defined triggers of the workflow. The sample workflow triggers when there is a direct push to main branch or  do changes to main branch via pull request.

on:

  push:

    branches: [ main ]

  pull_request:

    branches: [ main ]

Workflow can have one or multiple jobs which has multiple tasks in it. First provide a name for the job and define a runner going to use in the workflow. You can use GitHub runners or self-hosted runner based on your requirements.

jobs:

  dotNetCoreBuild:

    runs-on: ubuntu-latest

Next, pipeline steps can be added. As the first step, checkout source of the project by using checkout action

steps:

    - name: Checkout GitHub action

      uses: actions/checkout@v2

After that, the required .NET core components should be downloaded 

    - name: Setup .NET Core

      uses: actions/setup-dotnet@v1

      with:

        dotnet-version: 5.0.x

There can be several dependencies for the project. Those dependencies should be installed.

    - name: Restore dependencies

      run: dotnet restore "./coreSampleProject/coreSampleProject.csproj"

We have added steps to install components and dependencies above. Now, we can add a step to build the project.

    - name: Build

      run: dotnet build "./coreSampleProject/coreSampleProject.csproj" --no-restore

   After building the project, we need to add a step to publish  it as deployable package.

 - name: Publish

      run: dotnet publish "./coreSampleProject/coreSampleProject.csproj" -c Release -o "coreSamplepublished"

Finally upload artifacts to directory

    - name: Upload Artifacts

      uses: actions/upload-artifact@v2

      with:

       name: dotnetBuildArtifacts

       path: "./coreSamplepublished"


Complete code is as following

name: .NET Core

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  dotNetCoreBuild:

    runs-on: ubuntu-latest

    steps:
    - name: Checkout GitHub action
      uses: actions/checkout@v2
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 5.0.x
    - name: Restore dependencies
      run: dotnet restore "./coreSampleProject/coreSampleProject.csproj"
    - name: Build
      run: dotnet build "./coreSampleProject/coreSampleProject.csproj" --no-restore
    - name: Publish
      run: dotnet publish "./coreSampleProject/coreSampleProject.csproj" -c Release -o "coreSamplepublished"
    - name: Upload Artifacts
      uses: actions/upload-artifact@v2
      with:
       name: dotnetBuildArtifacts
       path: "./coreSamplepublished"

Once the pipeline is pushed, GitHub actions will execute the build pipeline .



No comments:

Post a Comment