Skip to main content

Lesson 3 of 9

GitHub Actions CI/CD Pipeline

GitHub Actions automates build, test, and deployment on every push. The pipeline: (1) Build and test with dotnet test, (2) Build Docker image, (3) Push to Azure Container Registry, (4) Deploy to Azure App Service or AKS. Secrets are stored in GitHub repository secrets, never in code.

30 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

GitHub Actions - Build, Test, Deploy

GitHub Actions - Build, Test, Deploy
# .github/workflows/ci-cd.yml

name: CI/CD - Build, Test & Deploy

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

env:
  DOTNET_VERSION: '8.0.x'
  REGISTRY: myappregistry.azurecr.io
  IMAGE_NAME: myapp-api

jobs:
  # ━━ Job 1: Build & Test ━━
  build-and-test:
    name: Build & Test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '${{ env.DOTNET_VERSION }}'

      - name: Cache NuGet packages
        uses: actions/cache@v4
        with:
          path: ~/.nuget/packages
          key: '${{ runner.os }}-nuget-${{ hashFiles("**/*.csproj") }}'
          restore-keys: '${{ runner.os }}-nuget-'

      - name: Restore dependencies
        run: dotnet restore

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

      - name: Run unit tests
        run: |
          dotnet test tests/MyApp.Tests/ \
            --no-build \
            --configuration Release \
            --collect:"XPlat Code Coverage" \
            --results-directory ./TestResults \
            --logger "trx;LogFileName=unit-tests.trx"

      - name: Run integration tests
        run: |
          dotnet test tests/MyApp.IntegrationTests/ \
            --no-build \
            --configuration Release \
            --logger "trx;LogFileName=integration-tests.trx"

      - name: Code coverage report
        uses: codecov/codecov-action@v4
        with:
          files: ./TestResults/**/coverage.cobertura.xml
          fail_ci_if_error: true

      - name: Upload test results
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: test-results
          path: ./TestResults

  # ━━ Job 2: Build & Push Docker Image ━━
  docker-build-push:
    name: Build & Push Docker Image
    runs-on: ubuntu-latest
    needs: build-and-test              # only run if tests pass
    if: github.ref == 'refs/heads/main'
    outputs:
      image-tag: '${{ steps.docker_meta.outputs.tags }}'
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Docker metadata
        id: docker_meta
        uses: docker/metadata-action@v5
        with:
          images: '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}'
          tags: |
            type=ref,event=branch
            type=sha,prefix=sha-,format=short
            type=semver,pattern={{version}},event=tag

      - name: Login to Azure Container Registry
        uses: azure/docker-login@v2
        with:
          login-server: '${{ env.REGISTRY }}'
          username: '${{ secrets.ACR_USERNAME }}'
          password: '${{ secrets.ACR_PASSWORD }}'

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: '${{ steps.docker_meta.outputs.tags }}'
          cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:cache
          cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:cache,mode=max

  # ━━ Job 3: Deploy to Azure App Service ━━
  deploy-staging:
    name: Deploy to Staging
    runs-on: ubuntu-latest
    needs: docker-build-push
    environment: staging              # GitHub environment with approval rules
    steps:
      - name: Login to Azure
        uses: azure/login@v2
        with:
          creds: '${{ secrets.AZURE_CREDENTIALS }}'

      - name: Deploy to Azure App Service
        uses: azure/webapps-deploy@v3
        with:
          app-name: myapp-api-staging
          images: '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:sha-${{ github.sha }}'

      - name: Run smoke tests
        run: |
          sleep 30  # wait for container to start
          curl -f https://myapp-staging.azurewebsites.net/health || exit 1

Tip

Tip

Practice GitHub Actions CICD Pipeline in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

GitHub Actions CI/CD Pipeline

Practice Task

Note

Practice Task - (1) Write a working example of GitHub Actions CICD Pipeline from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with GitHub Actions CICD Pipeline is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready dotnet code.

Topics in This Module

0/9 done