Continuous Integration (CI) and Continuous Deployment (CD) are the backbone of modern agile engineering. A solid CI/CD pipeline ensures that bugs are caught by automated tests before merging and releases happen automatically without stressful manual deployments.

1. Complete GitHub Actions Workflow

Here is an end-to-end GitHub Actions workflow that executes linting, runs test suites, builds a Docker image, and triggers production deployment upon pushing to the main branch.

.github/workflows/deploy.yml
name: CI/CD Pipeline

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

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: '8.0.x'

      - name: Restore dependencies
        run: dotnet restore

      - name: Run Unit & Integration Tests
        run: dotnet test --no-restore --verbosity normal --collect:"XPlat Code Coverage"

  build-and-deploy:
    needs: test
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and Push Docker Image
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: ghcr.io/${{ github.repository }}:latest
🛡️
Security Best Practice: Never hardcode production API keys or credentials in repository files. Store them under GitHub Settings > Secrets and Variables > Actions and reference them with ${{ secrets.SECRET_NAME }}.
← Previous Guide Building AI Features into Web Apps Tutorials Hub → Browse All Tutorials