CI/CD with GitHub Actions

লেসন ৪৮ / ৫০

CI/CD কী?

CI (Continuous Integration) — push হলে auto build + test।
CD (Continuous Deployment) — pass হলে production-এ deploy।

Folder Structure

.github/
  workflows/
    ci.yml           # build + test
    cd.yml           # deploy

CI Workflow — ci.yml

name: CI

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

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - 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: Build
        run: dotnet build --no-restore --configuration Release

      - name: Test
        run: dotnet test --no-build --configuration Release --verbosity normal --collect:"XPlat Code Coverage"

      - name: Upload coverage
        uses: codecov/codecov-action@v3
        with:
          files: ./**/coverage.cobertura.xml

CD Workflow — Deploy to Azure

name: Deploy to Azure

on:
  push:
    branches: [ main ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

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

      - name: Publish
        run: dotnet publish MyApp.Api/MyApp.Api.csproj -c Release -o publish

      - name: Login to Azure
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: Deploy to App Service
        uses: azure/webapps-deploy@v3
        with:
          app-name: myapp-api
          package: publish

Build & Push Docker Image

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: |
            myorg/myapp:latest
            myorg/myapp:${{ github.sha }}

Secrets Management

GitHub repo → Settings → Secrets and variables → Actions:

  • AZURE_CREDENTIALS
  • DOCKERHUB_TOKEN
  • DB_CONNECTION_STRING
env:
  ConnectionStrings__Default: ${{ secrets.DB_CONNECTION_STRING }}

Matrix Build (Multi-version)

strategy:
  matrix:
    dotnet: [ '6.0.x', '7.0.x', '8.0.x' ]
    os: [ ubuntu-latest, windows-latest ]
runs-on: ${{ matrix.os }}
steps:
  - uses: actions/setup-dotnet@v4
    with:
      dotnet-version: ${{ matrix.dotnet }}

Environments & Approvals

jobs:
  deploy-prod:
    environment:
      name: production
      url: https://myapp.com
    runs-on: ubuntu-latest
    # GitHub UI-এ manual approval require করতে পারে

Other Platforms

  • Azure DevOps — azure-pipelines.yml
  • GitLab CI — .gitlab-ci.yml
  • Jenkins — Jenkinsfile
  • CircleCI — .circleci/config.yml