setting-up-a-cicd-pipeline-for-net-core-applications-using-github-actions.html

Setting Up a CI/CD Pipeline for .NET Core Applications Using GitHub Actions

In today's fast-paced development environment, Continuous Integration (CI) and Continuous Deployment (CD) have become essential practices for delivering high-quality software efficiently. For .NET Core applications, GitHub Actions provides a powerful, flexible platform to automate the build and deployment processes. This article will guide you through setting up a CI/CD pipeline for your .NET Core applications using GitHub Actions step-by-step, complete with code examples and actionable insights.

What is CI/CD?

Continuous Integration (CI) is the practice of automatically testing and merging code changes into a shared repository. This ensures that new code integrates seamlessly with existing code, reducing bugs and improving software quality.

Continuous Deployment (CD) is the next step, where code changes are automatically deployed to production after passing all tests. This allows teams to deliver software updates more frequently and reliably.

Why Use GitHub Actions for CI/CD?

GitHub Actions is a powerful automation tool that allows you to define workflows directly within your GitHub repository. Here are some compelling reasons to use GitHub Actions for your CI/CD pipeline:

  • Integrated with GitHub: No need for third-party services; everything is managed in one place.
  • Customizable Workflows: You can create workflows tailored to your development needs.
  • Community Support: A vast library of pre-built actions is available for integration.

Setting Up Your .NET Core CI/CD Pipeline

Prerequisites

Before getting started, ensure you have the following:

  • A GitHub account.
  • A .NET Core application repository on GitHub.
  • Basic knowledge of Git and GitHub.

Step 1: Create a GitHub Workflow File

  1. In your .NET Core repository, navigate to the Actions tab.
  2. Click on New workflow.
  3. Choose set up a workflow yourself or select a template that fits your needs.

For this example, we’ll create a new file named .github/workflows/dotnet-core-ci-cd.yml.

Step 2: Define Your Workflow

Here’s a sample YAML configuration for your CI/CD pipeline:

name: .NET Core CI/CD

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up .NET
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '6.0.x'

      - name: Restore dependencies
        run: dotnet restore

      - name: Build
        run: dotnet build --configuration Release --no-restore

      - name: Run tests
        run: dotnet test --no-build --verbosity normal

      - name: Publish
        run: dotnet publish --configuration Release --output ./output

Step 3: Explanation of Steps

  • on: This section defines the triggers for the workflow. Here, the workflow runs on pushes and pull requests to the main branch.
  • jobs: A job contains a series of steps.
  • runs-on: Specifies the environment. We’re using the latest version of Ubuntu.
  • steps: Each step in the job.
    • Checkout code: Clones your repository into the runner.
    • Set up .NET: Installs the required .NET SDK.
    • Restore dependencies: Retrieves the necessary NuGet packages.
    • Build: Compiles the application in Release mode.
    • Run tests: Executes the unit tests you’ve written.
    • Publish: Packages the application for deployment.

Step 4: Deploying Your Application

To deploy your application to a host, such as Azure, you need to add deployment steps. Here’s an example of how to add Azure deployment to your pipeline:

      - name: Deploy to Azure
        uses: azure/webapps-deploy@v2
        with:
          app-name: 'YOUR_APP_NAME'
          publish-profile: ${{ secrets.AZURE_PUBLISH_PROFILE }}
          package: './output'

Step 5: Setting Up Secrets

  1. In your GitHub repository, go to Settings > Secrets and variables > Actions.
  2. Click on New repository secret.
  3. Add AZURE_PUBLISH_PROFILE and paste your Azure publish profile XML content.

Step 6: Testing Your CI/CD Pipeline

Now that everything is set up, push a change to your main branch or create a pull request. Navigate to the Actions tab in your repository to see the workflow in action. Each step will be logged, allowing you to troubleshoot any issues that arise.

Troubleshooting Common Issues

  • Build Failures: Ensure that all dependencies are properly referenced in your .csproj file.
  • Test Failures: Check the test output logs for errors and fix them accordingly.
  • Deployment Issues: Verify your Azure credentials and that the app name matches in your workflow.

Conclusion

Setting up a CI/CD pipeline for your .NET Core applications using GitHub Actions enhances your development workflow and ensures that your applications are consistently tested and deployed. By following the steps outlined in this article, you can automate your development process, reduce manual errors, and deliver software updates more efficiently.

By embracing CI/CD practices, your team can focus on what truly matters: writing great code, knowing that your deployment process is smooth and reliable. Happy coding!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.