πŸš€ Set Up CI/CD Pipelines For Build & Testing

by ADMIN 47 views

Description

In this article, we will explore the process of setting up automated build and test pipelines using GitHub Actions to ensure that all changes are validated before merging. This workflow should include build and unit test workflow, integration test workflow, and status checks to maintain project stability.

1. Build & Unit Test Workflow

The build and unit test workflow is a crucial part of the CI/CD pipeline. It ensures that the solution builds successfully and runs all unit tests. This workflow should run on every pull request and push to the main and develop branches.

File to Create:

.github/workflows/ci-pipeline.yml

File Contents:

name: "CI Pipeline"
on:
  pull_request:
    branches:
      - main
      - develop
    paths:
      - 'src/**/*.cs'
      - 'tests/**/*.cs'
      - 'database/**'
      - '.github/workflows/ci-pipeline.yml'
  push:
    branches:
      - main
jobs:
  build-and-test:
    name: "Build & Run Unit Tests"
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v3
      - name: Setup .NET
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: 8.0.x
      - name: Restore Dependencies
        run: dotnet restore
      - name: Build Solution
        run: dotnet build --no-restore --configuration Release
      - name: Run Unit Tests
        run: dotnet test --no-restore --verbosity normal --collect:"XPlat Code Coverage"
      - name: Upload Coverage Report
        uses: codecov/codecov-action@v3
        with:
          file: ./coverage.cobertura.xml
          fail_ci_if_error: true

2. Integration Test Workflow

The integration test workflow is designed to run only when database-related files change. It spins up Docker containers for MSSQL, MongoDB, and Redis, and runs integration tests in a temporary test environment. After execution, it cleans up all services.

File to Create:

.github/workflows/integration-tests.yml

File Contents:

name: "Integration Tests"
on:
  pull_request:
    branches:
      - main
    paths:
      - 'database/**'
      - 'src/**/Repositories/**'
jobs:
  integration-tests:
    name: "Run Integration Tests"
    runs-on: ubuntu-latest
    services:
      mssql:
        image: mcr.microsoft.com/mssql/server:2022-latest
        env:
          SA_PASSWORD: "YourStrong!Passw0rd"
          ACCEPT_EULA: "Y"
        ports:
          - 1433:1433
        options:
          --health-cmd "exit 0"
      mongodb:
        image: mongo:latest
        ports:
          - 27017:27017
      redis:
        image: redis:latest
        ports:
          - 6379:6379
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v3
      - name: Setup .NET
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: 8.0.x
      - name: Restore Dependencies
        run: dotnet restore
      - name: Run Integration Tests
        env:
          ConnectionStrings__MSSQL: "Server=localhost,1433;Database=TestDb;User Id=sa;Password=YourStrong!Passw0rd"
          ConnectionStrings__MongoDB: "mongodb://localhost:27017"
          Redis__Host: "localhost"
        run: dotnet test --no-restore --verbosity normal

3. Status Checks

To maintain project stability, PRs must pass all tests before merging. Failing tests should block merges to prevent broken code.

Configuration:

Enable GitHub status checks for the main and develop branches.

Tasks

  1. Create .github/workflows/ci-pipeline.yml for build & unit tests.
  2. Create .github/workflows/integration-tests.yml for integration tests.
  3. Ensure GitHub status checks are enabled to enforce validation.
  4. Monitor first test runs and refine workflows if needed.

Reference

  • Codecov Docs
  • GitHub Actions for .NET

Frequently Asked Questions

In this article, we will address some of the most common questions related to setting up CI/CD pipelines for build and testing using GitHub Actions.

Q: What is a CI/CD pipeline?

A CI/CD pipeline is a series of automated processes that are triggered by specific events, such as code commits or pull requests. The pipeline consists of multiple stages, including build, test, and deployment, which are executed in a specific order to ensure that the code is validated and deployed to production.

Q: Why do I need a CI/CD pipeline?

A CI/CD pipeline helps to ensure that your code is validated and tested before it is deployed to production. This helps to prevent bugs and errors from making it into production, which can save time and resources in the long run.

Q: What is GitHub Actions?

GitHub Actions is a CI/CD platform that allows you to automate your build, test, and deployment processes. It provides a simple and intuitive way to create and manage your CI/CD pipelines, and integrates seamlessly with GitHub.

Q: How do I set up a CI/CD pipeline using GitHub Actions?

To set up a CI/CD pipeline using GitHub Actions, you will need to create a YAML file that defines the pipeline. This file will specify the stages, jobs, and steps that make up the pipeline. You can then trigger the pipeline by committing code or creating a pull request.

Q: What are the different stages of a CI/CD pipeline?

The different stages of a CI/CD pipeline include:

  • Build: This stage involves compiling and packaging the code.
  • Test: This stage involves running automated tests to validate the code.
  • Deployment: This stage involves deploying the code to production.

Q: How do I configure the CI/CD pipeline to run on specific branches?

You can configure the CI/CD pipeline to run on specific branches by specifying the branch names in the YAML file. For example, you can specify that the pipeline should run on the main and develop branches.

Q: How do I configure the CI/CD pipeline to run on specific events?

You can configure the CI/CD pipeline to run on specific events by specifying the event types in the YAML file. For example, you can specify that the pipeline should run on pull requests and pushes.

Q: How do I troubleshoot issues with the CI/CD pipeline?

If you encounter issues with the CI/CD pipeline, you can troubleshoot them by checking the pipeline logs and error messages. You can also use the GitHub Actions UI to view the pipeline execution history and identify any issues.

Q: Can I customize the CI/CD pipeline to fit my specific needs?

Yes, you can customize the CI/CD pipeline to fit your specific needs. You can add or remove stages, jobs, and steps as needed, and configure the pipeline to run on specific branches and events.

Q: How do I integrate the CI/CD pipeline with other tools and services?

You can integrate the CI/CD pipeline with other tools and services by using GitHub Actions' built-in integrations or by creating custom integrations using GitHub Actions' API.

Conclusion

In this article, we have addressed some of the most common questions related to setting up CI/CD pipelines for build and testing using GitHub Actions. By following the steps outlined in this article, you can create a CI/CD pipeline that helps to ensure the quality and reliability of your code.